How to test views?

Last post 05-09-2008 3:33 AM by nagir. 5 replies.

Sort Posts:

  • How to test views?

    05-08-2008, 8:01 AM
    • Loading...
    • slieser
    • Joined on 05-08-2008, 11:57 AM
    • Posts 3

    Hi,

    any suggestions how to unit test the views?

    Regards,

    Stefan Lieser

    --

    http://www.lieser-online.de

     

  • Re: How to test views?

    05-08-2008, 11:05 PM
    • Loading...
    • nagir
    • Joined on 01-09-2006, 7:01 PM
    • Australia
    • Posts 84

    If you want to do acceptance tests you can simulate user actions from a browser(s) using tools like:

    1. Selenium
    2. WatIN

    While these tools are great and allow to simulate actual users behaviour there are disadvantages:

    • Require live we-bserver to be run.
    • Require browser to be run.
    • Requires interprocess communication between Unit tests and browser or executing tests separately from browser (Selenium).
    • Often (relatively) harder to write and maintain.

    Hope this helps.


    But what I actually need is to make sure the views are not broken and don't give an error. So my question is How to render the page (view) from unit test the same way it is rendered on a real server?

    Cheers,
    Dmitriy.
     

  • Re: How to test views?

    05-09-2008, 2:36 AM
    • Loading...
    • slieser
    • Joined on 05-08-2008, 11:57 AM
    • Posts 3

    Hello Dmitriy,

    thanks for your answer. I would like to do unit tests and thought that the whole ASP.NET MVC thing was made to support this ;-)

    So I second your question: how can I render the view in a unit test to assert that the embedded code runs as expected? (Note: yes there is very little code in the views, but I need to access the ViewData at least and thats what I would like to test).

    Regards,
    Stefan Lieser

     

  • Re: How to test views?

    05-09-2008, 3:07 AM
    • Loading...
    • nagir
    • Joined on 01-09-2006, 7:01 PM
    • Australia
    • Posts 84

    Hi,

    The MVC allows you, out of the box, to test controllers' logic, but not the client part. 

    I believe you can access your ViewData from unit tests (I assume via return value, but let other guys tell how). You don't have to render HTML to do so.

    If you haven't watched video series from Rob Conery I'd recommend you to do it. All 9 videos (as for now).

     

    What do you really want to test?

    1. Client's experience? Use tools I mentioned.
    2. Data passed to View? It should easy, just look around it. (I'm not using MVC - just follow what's happening around).
    3. Just make sure pages are not broken? I don't know a solution for now.

    Why do you want to render the HTML in unit tests? You should understand that it might not always be possible. For example, how can HTML be rendered if the page accesses HttpContext ("classic" one, that lives in System.Web) that is not available in unit tests.

    Cheers,
    Dmitriy.

  • Re: How to test views?

    05-09-2008, 3:17 AM
    • Loading...
    • slieser
    • Joined on 05-08-2008, 11:57 AM
    • Posts 3

    Hi,

    I do test the controllers and I do test that the controller hands the correct ViewData to the RenderView call.

    Next step would be to test that the view accesses the ViewData in the expected way. If for example I have a textbox and the content should be filled from a property of the ViewData I would like to test that. I understand that the view has some dependencies on infrastructure and I'm looking for a clean way to support this infrastructure, maybe with the option to mock it.

    So to answer your question: I would like to verify point 3, make sure that the view isn't broken.

    Regards,
    Stefan Lieser
    --
    http://www.lieser-online.de

     

  • Re: How to test views?

    05-09-2008, 3:33 AM
    • Loading...
    • nagir
    • Joined on 01-09-2006, 7:01 PM
    • Australia
    • Posts 84
    Hi,

    slieser:
    So to answer your question: I would like to verify point 3, make sure that the view isn't broken.
     

    Saying view is not broken I mean it just doesn't give an exception.
    Do you mean the same?

    Anyway I think the best way for now is to use WatIn tool. Just because of it is impossible to render ASPX page without infrastructure (or is it?).
     

    Cheers. 

Page 1 of 1 (6 items)