Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the correct way of Unit testing ViewEngines.Engines.FindView?
    primarykey
    data
    text
    <p>I recently did some refactoring of my mvc application and realized that there are alot of static views returned. Instead of having multiple controllers with action results that only return a view I decided to create one controller that returns the static views if they exists and throws a 404 error if the view doesn't exist.</p> <pre><code>public ActionResult Index(string name) { ViewEngineResult result = ViewEngines.Engines.FindView(ControllerContext, name, null); if (result.View == null) ThrowNotFound("Page does not exists."); return View(name); } </code></pre> <p>My question is what is the right way of unit testing this? I tried the following code but the error I get is "The RouteData must contain an item named 'controller' with a non-empty string value".</p> <pre><code>[Theory] [InlineData("ContactUs")] public void Index_should_return_view_if_view_exists(string name) { controller = new ContentController(); httpContext = controller.MockHttpContext("/", "~/Content/Index", "GET"); ; var result = (ViewResult)controller.Index(name); Assert.NotNull(result.View); } </code></pre> <p>My intention was for the unit test to go out and fetch the real view. Then I started to wonder if I should mock the ViewEngines with SetupGet for FindView and create two tests, where the second one tests that the not found exception is thrown if the view is null.</p> <p>What is the correct way of testing this functionality? Any pointers, sample code or blog posts would be helpful.</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload