I mentioned in my earlier post:
Model-View-Presenter Seems Easier to Test Than ASP.NET MVC
that my experience to date for doing view testing has been with the Model-View-Presenter Pattern and mocking the View Interface with a mocking framework. I have been very happy with the experience and it has been pretty easy-to-do.
Some examples from Phil Haack showing examples of tests without using what he refers to as the Specific SubClass Pattern looked really ugly and a hell of a lot of work. It wasn’t fair to Phil for just copying and pasting his example and comparing it to a simple one I had written doing something different, my apologies on that, but my goal wasn’t to stack the deck in favor of Model-View-Presenter, but really just a call for help. I am treading on new ground with testing controllers in the ASP.NET MVC Framework and I could use some help in making it easier
Jeffrey provided an example of a test in the comments on the previous post. It looks to use the same Specific SubClass Pattern that Phil talks about in his post. I am now taking this pattern as somewhat of a best practice for testing controllers in the ASP.NET MVC Framework, because it came from such experts.
After playing with it all morning, I rather like the pattern and find the tests comparable to what I have experienced with MVP. Here are a couple I have been playing with modeled after what I have seen from Jeffrey and Phil:
[TestFixture]
public class CustomersControllerTextFixture
{
[Test]
public void ShouldReturnListOfCustomers()
{
MockRepository mocks = new MockRepository();
ICustomersDataSource mockDataSource = mocks.CreateMock<ICustomersDataSource>();TestCustomersController controller
= new TestCustomersController(mockDataSource);List
<Customer> customers = new List<Customer>
{
new Customer(),
new Customer(),
new Customer()
};Expect.Call(mockDataSource.SelectAll()).Return(customers);
mocks.ReplayAll();
controller.Index();
mocks.VerifyAll();Assert.That(controller.ActualViewData, Is.EquivalentTo(customers));
“Customers“));
Assert.IsTrue(controller.ActualViewName.Equals(
}[Test]
public void SaveActionShouldRedirectToIndexAction()
{
MockRepository mocks = new MockRepository();
ICustomersDataSource mockDataSource = mocks.CreateMock<ICustomersDataSource>();TestCustomersController controller
= new TestCustomersController(mockDataSource);mocks.ReplayAll();
controller.Save(
“who“, “cares“);Assert.AreEqual(controller.RedirectToActionValues,
“{ Action = Index }“);
}
}
with a TestCustomersController-
public class TestCustomersController : CustomersController
{
public string ActualViewName;
public string ActualMasterName;
public object ActualViewData;
public string RedirectToActionValues;public TestCustomersController(ICustomersDataSource dataSource)
: base(dataSource) { }protected override void RenderView(string viewName, string masterName, object viewData)
{
ActualViewName = viewName;
ActualMasterName = masterName;
ActualViewData = viewData;
}protected override void RedirectToAction(object values)
{
RedirectToActionValues = values.ToString();
}
}
Again, I am just trying to get my arms around how to test this new web development environment, so don’t confuse this as expert testimonial. I would love to see more authorities on the subject provide some tutorials.
Hi David,
I have been playing with ways to simplify my controller tests, too. I posted some tweaks to some of Phil Haack’s stuff, which I felt were a step in the right direction. I am not a fan of the test-specific subclass approach. Check them out at persistall.com and I’d love to hear your feedback. I’d post direct links, but I’m on my iPhone and they don’t do cut & paste!!
You are not the only one asking the same question MVP vs MVC
http://blog.vuscode.com/malovicn/archive/2007/12/18/model-view-presenter-mvp-vs-model-view-controller-mvc.aspx
Thanks for all the great blog posts!