Reputation: 4749
The topic says it all.
I'm guessing this is because of some missing setup related to MVC, but I'm very new to the world of http, asp.net and mvc, so I'm not quite sure what's wrong.
public class MyController : Controller {
public ActionResult MyAction(MyModel model) {
return View(model);
}
}
var controllerMock = new Mock<MyController>() {
CallBase = true // without this, the call to View(model) returns null
};
/*
* I've also tried this before calling the action:
*
* controllerMock.SetFakeControllerContext();
*
* from http://www.hanselman.com/blog/ASPNETMVCSessionAtMix08TDDAndMvcMockHelpers.aspx
* But the same applies.
**/
ViewResult result = controllerMock.Object.MyAction(new MyModel()) as ViewResult;
Assert.AreEqual("MyAction", result.ViewName); // ViewName etc is blank
Upvotes: 1
Views: 665
Reputation: 5194
If you use mvccontrib for your tests you can try something like this:
var controller = new MyController();
var builder = new TestControllerBuilder();
builder.InitializeController(controller);
var actionResult = controller.MyAction(new MyModel());
ViewResult viewResult = actionResult.AssertViewRendered().ForView("");
//or
ViewResult viewResult = actionResult.AssertViewRendered().ForViewOrItself("MyAction");
Upvotes: 2