How to check a call to a call View () does not throw an exception?

If we click SomeController.SomeAction(), but the SomeAction.cshtml file is not found, we will get a System.InvalidOperationException and an output error message ...

The view "SomeAction" or its wizard was not found or the viewer does not support the locations found. The following locations were searched: ... etc.

However, even if this file does not skip the test below, it will be passed as ViewResult .

What is the best way to test an action call for View()not throwing an exception and calmly return to ViewResult content ?


public class SomeController: Controller
{
    public ActionResult SomeAction()
    {
        var viewModel = new SomeModel();
        return View(viewModel);
    }
}

[Test]
public void TestIndex(Type clientType)
{
    var controller = new SomeController();
    var result = (ViewResult)controller.SomeAction();

    Assert.That(result, Is.TypeOf<ViewResult>());
}

. , , .

+5
2

-, , unit test . , , , . , :

  • ;
  • .

, , , ViewResult, . , , , , , , , , assert .

+3

, , , - , . FindView. ControllerContext, Http, .

Resharper, , , .

+1

All Articles