title: Mock view returned by FindView
body: <p>I want to mock (using MOQ) view returned by Findview method as shown below, so that view is not null:</p>
  
  <pre><code>if ((ViewEngines.Engines.FindView(ControllerContext, viewName, masterName)).View == null)
              {
                  viewName = SomeViewName;
              }
  </code></pre>
  
  <p>i have mocked viewengine like this following an e.g. online:</p>
  
  <pre><code>var mockViewEngine = new Mock&lt;IViewEngine&gt;();
          // Depending on what result you expect you could set the searched locations
          // and the view if you want it to be found
          Mock&lt;IView&gt; view = new Mock&lt;IView&gt;();
          var result = new ViewEngineResult(new[] { "location1", "location2" });
  
          // Stub the FindView method
          mockViewEngine
              .Setup(x =&gt; x.FindView(It.IsAny&lt;ControllerContext&gt;(), It.IsAny&lt;string&gt;(), It.IsAny&lt;string&gt;(), false))
              .Returns(result);
  
          // Use the mocked view engine instead of WebForms
          ViewEngines.Engines.Clear();
          ViewEngines.Engines.Add(mockViewEngine.Object);
  </code></pre>
  
  <p>but when i run test it gives me this error:</p>
  
  <pre><code>System.NullReferenceException : Object reference not set to an instance of an object.
  at System.Web.Mvc.ViewEngineCollection.Find(Func`2 cacheLocator, Func`2 locator)
  </code></pre>