Page view counter

Testing Model, Controller and View

Last post 08-28-2008 10:40 AM by TATWORTH. 16 replies.

Sort Posts:

  • Testing Model, Controller and View

    03-25-2008, 10:16 AM
    • Loading...
    • Zhou
    • Joined on 04-29-2006, 8:12 AM
    • Germany
    • Posts 264

    Hi,

    I have implemented a small web application with the MVC Framework and now I'm trying to test the solution with Unit tests and Rhino.Mocks (I know that is not the TDD approach ... Stick out tongue ). Everything is fine, I have test cases for my model and for my controller with mock objects. But how can I test the ViewPage? I have tried to do this and to mock the necessary objects, but I have always this exception:

    Test method MVC_Framework_ExampleTests.DetailTest.Page_LoadTest threw exception:  System.NullReferenceException:
    System.Web.UI.Page.SetIntrinsics(HttpContext context, Boolean allowAsync)
    System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)
    System.Web.UI.Page.ProcessRequest(HttpContext context)
    System.Web.Mvc.ViewPage.RenderView(ViewContext viewContext)


    And this is the content of the test method:

    Detail target = new Detail();
    using (_mockRepository.Playback())
    {
      ViewContext testViewContext = new ViewContext(_customerCtrl.ControllerContext, "Detail", string.Empty, TestCustomer, new TempDataDictionary(_customerCtrl.ControllerContext.HttpContext));
      target.RenderView(testViewContext);
    }

    I'm using the extension methods from Scott to fake my controllerContext. 

    It is possible to test the view and to check if the fields have the correct values from the data model?

    Bye,
    Marc André

  • Re: Testing Model, Controller and View

    03-25-2008, 4:08 PM
    • Loading...
    • tgmdbm
    • Joined on 12-17-2007, 2:08 PM
    • Posts 815
    • ASPInsiders

    Ok. I've been trying for a while now to render a View using a Mocked HttpContext and I've come to a sticky end.

    ViewPage.cs:
    71:  ProcessRequest(HttpContext.Current);
     

    It's HttpContext.Current that's null, and I don't see a way of mocking it.

     

    Also noticed something funny in System.Web.UI.Page.SetIntrinsics(HttpContext, bool). This exerpt taken from Reflector

    ...
    HttpCapabilitiesBase browser = this._request.Browser;
    this._response.ContentType = browser.PreferredRenderingMime;
    if (browser != null)
    {
      ...
    }

    If browser might be null, you're going to blow up before you test for it!

  • Re: Testing Model, Controller and View

    03-25-2008, 4:19 PM
    • Loading...
    • TATWORTH
    • Joined on 02-04-2003, 8:34 AM
    • England
    • Posts 7,825

     Zhou

       If you find the answer yourself, please post the solution. NUnit tests of an MVC web site seem unnecessarily difficult.
     

    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Testing Model, Controller and View

    03-25-2008, 7:09 PM
    • Loading...
    • csainty
    • Joined on 03-12-2008, 2:41 AM
    • Newcastle, Australia
    • Posts 49

    What exactly about the View are you trying to test?

    For me, the only useful Unit test of a view would be that it compiles.
    Once you start wanting to know if the correct HTML was rendered and the correct values placed into fields etc, you might want to look at Acceptance Testing.
    Take a look at something like Selenium (http://selenium.openqa.org/) It allow you to create and execute scripts that run in the actual browser and can check for text in the page etc. There is also a cool FF plugin to help you create the scripts.

    As a side note, I am finding that subclassing the BaseHttp* classes with my own versions and using them for testing is far easier than Mocks for most cases.

  • Re: Testing Model, Controller and View

    03-25-2008, 7:25 PM
    • Loading...
    • tgmdbm
    • Joined on 12-17-2007, 2:08 PM
    • Posts 815
    • ASPInsiders

    The more you can do automated, the better.

    For instance, I've written a class that uses the w3.org xhtml validator to check a fragment (or a complete page) of html. I'd like to run that automatically.

    I don't want my acceptance testing to include "check that the html is xhtml strict compliant"!!

    But yes, at a certain point you need to stop unit testing and start UATs.

  • Re: Testing Model, Controller and View

    03-25-2008, 7:29 PM
    • Loading...
    • TATWORTH
    • Joined on 02-04-2003, 8:34 AM
    • England
    • Posts 7,825

    >For instance, I've written a class that uses the w3.org xhtml validator to check a fragment (or a complete page) of html. I'd like to run that automatically.
    Have you published your class for doing the XHTML tests?

    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Testing Model, Controller and View

    03-25-2008, 7:33 PM
    • Loading...
    • csainty
    • Joined on 03-12-2008, 2:41 AM
    • Newcastle, Australia
    • Posts 49

    XHTML is a good idea for a View unit test. Nice.

    Selenium is automated, and will integrate into CI tools etc.
    So no worries there.

  • Re: Testing Model, Controller and View

    03-25-2008, 7:43 PM
    • Loading...
    • TATWORTH
    • Joined on 02-04-2003, 8:34 AM
    • England
    • Posts 7,825

     For Unit testing of a web site,  NUnitAsp can be used.

    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Testing Model, Controller and View

    03-25-2008, 7:44 PM
    • Loading...
    • tgmdbm
    • Joined on 12-17-2007, 2:08 PM
    • Posts 815
    • ASPInsiders

    http://validator.w3.org/check?output=soap12&prefill=1&prefill_doctype=xhtml&fragment=<html goes here>

    simply build the Url, putting the html where the html goes ;)

    then use:

    var reader = XmlReader.Create( Url )
    var ds = new DataSet();
    ds.ReadXml( reader ) 

    The data set then contains all the Errors and Warnings. If they're empty then it's compliant. Of course you need to be connected to the Wired for this to work. 

  • Re: Testing Model, Controller and View

    03-25-2008, 7:49 PM
    • Loading...
    • TATWORTH
    • Joined on 02-04-2003, 8:34 AM
    • England
    • Posts 7,825

     Thank you for your tip.

    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Testing Model, Controller and View

    03-26-2008, 1:47 AM
    • Loading...
    • Zhou
    • Joined on 04-29-2006, 8:12 AM
    • Germany
    • Posts 264

    Hi,

    thank you for all your responses! The main aim of my view test should be to test if all the controls (i.e. textboxes, dropdown ...) contains the corret values from the model. To do this with an automated test would be a great advantage. I think after this "basic" view test the UAT can start. I will try some other workarounds to solve this issue ...

    BTW: The main goal of the MVC-Framework is to enable easy unit tests, isn't it? Or is this statement only valid for the Model and the Controller??

    @tgmdbm
    I noticed the the same "wrong" order in the method SetIntrinsics(HttpContext, bool) and I mock the Browser property, but without success.

    Bye,
    Marc André

    Filed under: ,
  • Re: Testing Model, Controller and View

    03-26-2008, 1:58 AM
    • Loading...
    • tgmdbm
    • Joined on 12-17-2007, 2:08 PM
    • Posts 815
    • ASPInsiders

    Yeah, testing the view is still difficult in MVC.

    MVC *has* made one third of our web applications very easily testable, the part which "handles" our data: the Controller. I would argue that (if you were doing things right) the Model was already testable with WebForms.

    Zhou:
    I noticed the the same "wrong" order in the method SetIntrinsics(HttpContext, bool) and I mock the Browser property, but without success.

    That method fails on the second line where it tries to dot into context, because context is null.

  • Re: Testing Model, Controller and View

    03-26-2008, 9:28 AM
    • Loading...
    • Zhou
    • Joined on 04-29-2006, 8:12 AM
    • Germany
    • Posts 264

    Yes, that is correct. The context comes from HTTPContext.Current and HTTPContext is a sealed class and is not mockable.... very bad.

  • Re: Testing Model, Controller and View

    03-26-2008, 9:50 AM
    • Loading...
    • katokay
    • Joined on 12-03-2006, 4:37 PM
    • Posts 50

     We have been using WatiN for automating the testing of our view. It makes testing the view quite easy.

    http://blog.bluecog.co.nz/archives/2007/06/07/flow-testing-with-watin/ Example of automating W3C validation

    http://watin.sourceforge.net/index.html Project home page

  • Re: Testing Model, Controller and View

    03-27-2008, 12:10 PM

     

    Zhou:
    The context comes from HTTPContext.Current and HTTPContext is a sealed class and is not mockable.... very bad

    It does. That's because the WebForms view engine *is* plain old ASP.NET WebForms, without any changes under the hood. Of course, plain old WebForms was created long before HttpContextBase existed, so it's not surprising that it references HttpContext.Current. I guess a future version of ASP.NET could reverse the dependency but for now we're stuck.

Page 1 of 2 (17 items) 1 2 Next >