1

I wonder if it's possible to test ASP.net MVC views without pre-compiling the views. I found many example on the internet but none of them could test the view. They could only test the model of the view.

Update
The views are xml template. I have some logic in controller and I want that to get views from the controller.

Anonymous
  • 9,366
  • 22
  • 83
  • 133
  • 2
    Razor or ASPX views? And why do you want to unit test views given that views *normally* don't contain any logic? – Tim Rogers Nov 18 '14 at 10:19
  • It's Razor. The views are just file templates, all I need is to pass in the model to the view. – Anonymous Nov 18 '14 at 10:29
  • Agree with @TimRogers it is not useful to Unit Test Views. Normally people have UI Testing frameworks to test the View. For example size of the button and location etc which is in the View itself. – Spock Nov 18 '14 at 10:51
  • @Spock In my case, I use view to render xml files and I want the tests to be as fast as possible. – Anonymous Nov 18 '14 at 11:56

1 Answers1

2

You can render Razor views standalone using RazorEngine or one of the solutions here. This will give you HTML which you will have to parse.

However, the reason you haven't found any examples of testing views is that few people do it. Views are declarative; they don't have logic. It takes time and effort to develop a test strategy for views. A better strategy might be to move any logic contained in your view into the models where it can be tested easily.

Community
  • 1
  • 1
Tim Rogers
  • 21,297
  • 6
  • 52
  • 68
  • I've tried RazorEngine but it lacks the interaction between Controller and View. I want to get views from controllers then do the unit-tests on views. – Anonymous Nov 18 '14 at 11:57
  • 1
    I'm not sure what you mean, but you should really reconsider your approach. You can't "get views from controllers": controllers just provide `ActionResult`s not views. And there shouldn't be any interaction between controller and view - a controller just provides a model for the view to consume and that's it. – Tim Rogers Nov 18 '14 at 13:59