David Hayden [MVP C#]

Sponsors

The Lounge

Wicked Cool Jobs

News

  • CodeBetter.Com Home

Other Links

Teas

Patterns & Practices

Florida .NET Developer

Book Reviews

Tampa ASP.NET MVC Developer Group

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
More On Unit Testing Controllers in the ASP.NET MVC Framework....

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 Mon, Dec 17 2007 11:35 AM by David Hayden

[Advertisement]

Comments

Trumpi's blog wrote Our Daily Link (2007-12-17)
on Mon, Dec 17 2007 1:25 PM

Today was another public holiday. It was boring and I tried to hack NAnt to build with multiple threads

Nikola Malovic wrote re: More On Unit Testing Controllers in the ASP.NET MVC Framework....
on Tue, Dec 18 2007 1:30 AM

You are not the only one asking the same question MVP vs MVC

blog.vuscode.com/.../model-view-presenter-mvp-vs-model-view-controller-mvc.aspx

Thanks for all the great blog posts!

» Daily Bits - December 18, 2007 Alvin Ashcraft’s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats. wrote &raquo; Daily Bits - December 18, 2007 Alvin Ashcraft&#8217;s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats.
on Tue, Dec 18 2007 9:21 AM

Pingback from  &raquo; Daily Bits - December 18, 2007 Alvin Ashcraft&#8217;s Daily Geek Bits: Daily links plus random ramblings about development, gadgets and raising rugrats.

The .NET MVC Framework - Part 2 - Testing Complex Routes and Controllers « Ramblings of the Sleepy… wrote The .NET MVC Framework - Part 2 - Testing Complex Routes and Controllers &laquo; Ramblings of the Sleepy&#8230;
on Tue, Dec 18 2007 4:49 PM

Pingback from  The .NET MVC Framework - Part 2 - Testing Complex Routes and Controllers &laquo; Ramblings of the Sleepy&#8230;

Brian Donahue wrote re: More On Unit Testing Controllers in the ASP.NET MVC Framework....
on Mon, Dec 24 2007 11:05 AM

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!!

http://tableadapter.codebetter.com/blogs/david.hayden/archive/2007/12/17/more-on-unit-testing-controllers-in-the-asp-net-mvc-framework.aspx wrote http://tableadapter.codebetter.com/blogs/david.hayden/archive/2007/12/17/more-on-unit-testing-controllers-in-the-asp-net-mvc-framework.aspx
on Wed, Apr 9 2008 11:55 AM
Devlicio.us