Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The MVC4 team have made <code>HttpContext</code> related properties abstract so that they can be mocked, so <code>Response</code> is now of type <code>HttpResponseBase</code> so has already been abstracted away. You can just mock calls to it.</p> <p>Below is a standard method that I've used in the past to initialise my controllers in a unit test scenario. It's with respect to MOQ. I create a fake http context that mocks out the various related properties as required. You can modify this to fit your exact scenario.</p> <p>After instantiating the controller I pass it to this method (perhaps in a base class - I use NBehave for my unit testing but I won't muddy the waters with anything related to that specifically here):</p> <pre><code>protected void InitialiseController(T controller, NameValueCollection collection, params string[] routePaths) { Controller = controller; var routes = new RouteCollection(); RouteConfig.RegisterRoutes(routes); var httpContext = ContextHelper.FakeHttpContext(RelativePath, AbsolutePath, routePaths); var context = new ControllerContext(new RequestContext(httpContext, new RouteData()), Controller); var urlHelper = new UrlHelper(new RequestContext(httpContext, new RouteData()), routes); Controller.ControllerContext = context; Controller.ValueProvider = new NameValueCollectionValueProvider(collection, CultureInfo.CurrentCulture); Controller.Url = urlHelper; } </code></pre> <p>The <code>ContextHelper</code> is where the mocks are all set up:</p> <pre><code>public static class ContextHelper { public static HttpContextBase FakeHttpContext(string relativePath, string absolutePath, params string[] routePaths) { var httpContext = new Mock&lt;HttpContextBase&gt;(); var request = new Mock&lt;HttpRequestBase&gt;(); var response = new Mock&lt;HttpResponseBase&gt;(); var session = new Mock&lt;HttpSessionStateBase&gt;(); var server = new Mock&lt;HttpServerUtilityBase&gt;(); var cookies = new HttpCookieCollection(); httpContext.Setup(x =&gt; x.Server).Returns(server.Object); httpContext.Setup(x =&gt; x.Session).Returns(session.Object); httpContext.Setup(x =&gt; x.Request).Returns(request.Object); httpContext.Setup(x =&gt; x.Response).Returns(response.Object); response.Setup(x =&gt; x.Cookies).Returns(cookies); httpContext.SetupGet(x =&gt; x.Request.Url).Returns(new Uri("http://localhost:300")); httpContext.SetupGet(x =&gt; x.Request.UserHostAddress).Returns("127.0.0.1"); if (!String.IsNullOrEmpty(relativePath)) { server.Setup(x =&gt; x.MapPath(relativePath)).Returns(absolutePath); } // used for matching routes within calls to Url.Action foreach (var path in routePaths) { var localPath = path; response.Setup(x =&gt; x.ApplyAppPathModifier(localPath)).Returns(localPath); } var writer = new StringWriter(); var wr = new SimpleWorkerRequest("", "", "", "", writer); HttpContext.Current = new HttpContext(wr); return httpContext.Object; } } </code></pre> <p>I've recently written a blog post covering this approach but using <code>Nsubstitute</code> as the mocking framework instead of <code>MOQ</code>:</p> <p><a href="http://levelnis.co.uk/blog/unit-testing-controllers-using-nunit-and-nsubstitute" rel="nofollow">Unit testing Controllers using NUnit and NSubstitute</a></p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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