ASP.NETMVC3のコントローラーで編集アクションを単体テストしようとしています。
nuget経由でMvcontrib.MVC3.TestHelperをインストールして、コントローラーコンテキストをモックアウトしましたが、それでもNullReferenceExceptionが発生します
私のコードは次のようになります:
[TestMethod]
public void it_should_redirect_to_index_after_editing_a_something_successfully()
{
var something= new SomeThing
{
ID = Guid.NewGuid(),
CreatedAt = DateTime.Now,
LastModified = DateTime.Now,
Owner = "Me",
Status = "new",
Title = "my Title",
Attachments = new List<Attachment>()
};
var repo = new FakeRepository();
var controller = new SomethingsController(repo);
new TestControllerBuilder().InitializeController(controller);
var result = controller.Edit(something) as RedirectToRouteResult;
result.AssertActionRedirect().ToAction<SomethingsController>(x => x.Index());
}
プロダクションコードは次のようになります...
[HttpPost]
public ActionResult Edit(SomeThing something)
{
if (ModelState.IsValid)
{
var _something = _repository.GetDocumentByID(something.ID);
TryUpdateModel(_something);
_something.LastModified = DateTime.Now;
_repository.SaveChanges();
return RedirectToAction("Index","Somethings");
}
return View(something);
}
また、UpdateModelまたはTryUpdateModelを使用しても、NullReferenceExceptionでクラッシュします...
どんな助けでも、ポインタは素晴らしいでしょう...