How do you unit test using RazorGenerator.MsBuild?

Apr 19, 2013 at 4:18 AM
Ok, I have RazorGenerator.MsBuild set up on my MVC project. My views build. I got errors and fixed the errors...

I started following the unit test example here: http://blog.davidebbo.com/2011/06/unit-test-your-mvc-views-using-razor.html

Then I created a new project for my unit tests. Referenced my MVC project and the HtmlAgilityPack. Starting to create a test, I was able to instantiate my view. Ok, good. But then I tried to reference the ViewBag. It didn't recognize that. Ok. Didn't really want to use the ViewBag anyways...

How about RenderAsHtml? I really want that. Nope. No go.

Am I missing something? A reference maybe? Does the unit test project need to reference Razor Generator as well?

Thanks
Matt
Coordinator
Apr 19, 2013 at 6:03 PM
I have a sample project here that has a basic test example. However, it's not using the MsBuild task.

Also, are you referencing the RazorGenerator.Testing package? RenderAsHtml comes from there.
Apr 19, 2013 at 7:04 PM
AHA! So I was missing RazorGenerator.Testing.

I can now set the Model or the ViewBag properties.

RenderAsHtml still doesn't show. Now, I see your sample just has Render. That's not there either. I do see RenderBody, RenderPage, and RenderSection. Is RenderPage what I want now?

I have strongly typed models, so can I just set the Model insteadf of using the Viewbag before calling whatever render method I need?

Thanks
Matt
Coordinator
Apr 19, 2013 at 7:56 PM
It's an extension method, so you're likely missing the using RazorGenerator.Testing;
Apr 19, 2013 at 9:26 PM
That looks to be it. Thanks.
Apr 22, 2013 at 5:45 PM
OK, back to my model question. How do I set the model for the view? I'm using a strongly typed model, but the Model property is read only.
Apr 22, 2013 at 10:54 PM
Some docs I read implied I could pass the model into the constructor of the view, but that doesn't seem to work.
Coordinator
Apr 22, 2013 at 10:59 PM
Try setting view.ViewData.Model