我有測試類,它使用UpdateModel()方法的問題。 我得到System.NullReferenceException。 我使用NUnit。 這是我從HomeController的方法:NUnit - UpdateModel問題:NullReferenceException
public ActionResult ProjectsEdit(Projects model)
{
var projects = db.Projects.First();
projects.Content = model.Content;
UpdateModel(projects);
db.SaveChanges();
return RedirectToAction("Projects");
}
下面是測試類:
[Test]
public void ProjectsEditPostTest()
{
var routeData = new RouteData();
var httpContext = MockRepository.GenerateStub<HttpContextBase>();
//var httpContext = new FakeHttpContext("Edit");
FormCollection formParameters = new FormCollection();
ControllerContext controllerContext =
MockRepository.GenerateStub<ControllerContext>(httpContext,
routeData,
controller);
controller.ControllerContext = controllerContext;
// Act
string newContent = "new content";
Projects projects = new Projects { ID = 1, Content = newContent };
controller.ProjectsEdit(projects);
// Assert
Assert.AreEqual(newContent, controller.db.Projects.First().Content);
}
我應該怎麼做才能讓它的工作原理?
什麼行代碼會拋出異常? – esskar
UpdateModel(projects); //從HomeController 和 controller.ProjectsEdit(projects); //從HomeControllerTest –