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));
Assert.IsTrue(controller.ActualViewName.Equals("Customers"));
}
[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.
Posted
12-17-2007 11:35 AM
by
David Hayden