Testing MVC Controllers in 3 lines of code with Typemock Isolator

Here’s a much simpler version of how you’d test an ASP.NET MVC Controller using Typemock, without needing any helper class to do it:

[TestMethod,VerifyMocks]
  public void Hello_ShowsTheHelloView2()
  {
      Mock mock = MockManager.Mock<HomeController>();
      mock.ExpectCall(“RenderView”).Args(“Index”);

      HomeController controller = new HomeController();
      controller.Index();
  }

This is possible because we can mock out the protected (non virtual ) method “RenderView” on the Controller itself, which saves us the trouble of even touching anything underneath it.

With this, you don’t need to have any of the helper code we wrote about here. If your controller uses the request context however, you may want to still mock that out using the previously mentioned links .

Tags: