Asp.Net MVC How to verify that the view is displayed

Hi there

I was wondering if there is a better testing method that visualized a view in MVC.

I thought that maybe I need to display the view in a string, but maybe there are simpler methods?

Basically, what I want to know if the view for this action was displayed without errors I'm already testing the view model, but I want to see that rendering the view gives the correct ViewData.Model works

+4
source share
2 answers

Use the TestHelpers MvcContrib library to make statements that a specific view is returned from your action:

var sampleController = new SampleController(); sampleController.New().AssertViewRendered().ForView("New").WithViewData<SomeModel>(); 

To make statements so that you return the correct data to the view, pull the model from ActionResult :

 var result = (ViewResult)sampleController.New(); ((SomeModel)result.ViewData.Model).SomeProperty.ShouldNotBeNull(); 

This is until the device is tested.

For end-to-end automated function / GUI testing, you might consider using a tool like Selenium .

+3
source

You can enable compilation of views for your project. Thus, the project will not compile unless you fix the problem. Edit this line in the .csproj file:

<MvcBuildViews>false</MvcBuildViews> , using true instead of false

I can also use ELMAH (install it on NuGet) and you can receive detailed errors by email (including YSOD).

A source

0
source

Source: https://habr.com/ru/post/1311416/


All Articles