My HomeController.Index() action works (in normal operation), but under NUnit testing, the ActionResult (ViewResult) that is returned always has a null View and ViewName.
Here are the tests I am running (condensed into a single method for ease of reading).
- I am using Moq, NUnit, Castle.Windsor
- The result's Model is correct, but there is no view associated with the result.
- All assertions pass except the final one, which refers to the result.View.
Repeating for clarity - the correct view is returned in normal operation.
[Test]
public void WhenHomeControllerIsInstantiated()
{
Moch mochRepository = new Mock<IRepository>();
mochRepository.Setup(s => s.Staff.GetStaffByLogonName("twehr"))
.Returns(new Staff { StaffID = 5, LogonName = @"healthcare\twehr" });
IController controller = new HomeController(mochRepository.Object);
IPrincipal FakeUser = new GenericPrincipal(new GenericIdentity("twehr", "Basic"), null);
var result = ((HomeController)controller).Index(FakeUser) as ViewResult;
Assert.IsNotNull(controller);
Assert.IsInstanceOf(typeof(HomeController), controller);
Assert.IsInstanceOf(typeof(HomeViewModel), ((ViewResult)result).Model);
// result.View and result.ViewName are always null
Assert.AreEqual("Index", result.ViewName);
}
Obviously, I am overlooking something in the test setup, but can't find it. Any help is appreciated.