Nunit MVC Site Test

I ran into a problem related to the unit test MVC site that I have: I need most of the ASP.NET environment to run (generating httpcontexts, sessions, cookies, memberships, etc.).) To fully check everything.

Even in order to verify that part of the less complex material for the front part needs memberships in order to work correctly, and it would be very difficult to get all this by fake manually.

Is there a way to deploy an application pool inside NUnit tests? This seems like the easiest way.

+6
source share
4 answers

I do not know how to do this, since your code is not in this process and requires a host that is also not in aspnet. (I was wrong before haha)

Theres an older HttpSimulator from Phil Haack, did you give that a whirlwind?

http://haacked.com/archive/2007/06/19/unit-tests-web-code-without-a-web-server-using-httpsimulator.aspx

+2
source

If you are spelled correctly, you do not need to have a real context, a real session, cookies, etc. The MVC framework by default provides an HttpContext that can be mocked / drowned out. I would recommend using a mocking framework like Moq or Rhino Mocks and creating a MockHttpContext class that will create a mock context with all the properties you need to test against customization. Here's a mock HttpContext that uses Moq

/// <summary> /// Mocks an entire HttpContext for use in unit tests /// </summary> public class MockHttpContextBase { /// <summary> /// Initializes a new instance of the <see cref="MockHttpContextBase"/> class. /// </summary> public MockHttpContextBase() : this(new Mock<Controller>().Object, "~/") { } /// <summary> /// Initializes a new instance of the <see cref="MockHttpContextBase"/> class. /// </summary> /// <param name="controller">The controller.</param> public MockHttpContextBase(Controller controller) : this(controller, "~/") { } /// <summary> /// Initializes a new instance of the <see cref="MockHttpContextBase"/> class. /// </summary> /// <param name="url">The URL.</param> public MockHttpContextBase(string url) : this(new Mock<Controller>().Object, url) { } /// <summary> /// Initializes a new instance of the <see cref="MockHttpContextBase"/> class. /// </summary> /// <param name="controller">The controller.</param> /// <param name="url">The URL.</param> public MockHttpContextBase(ControllerBase controller, string url) { HttpContext = new Mock<HttpContextBase>(); Request = new Mock<HttpRequestBase>(); Response = new Mock<HttpResponseBase>(); Output = new StringBuilder(); HttpContext.Setup(x => x.Request).Returns(Request.Object); HttpContext.Setup(x => x.Response).Returns(Response.Object); HttpContext.Setup(x => x.Session).Returns(new FakeSessionState()); Request.Setup(x => x.Cookies).Returns(new HttpCookieCollection()); Request.Setup(x => x.QueryString).Returns(new NameValueCollection()); Request.Setup(x => x.Form).Returns(new NameValueCollection()); Request.Setup(x => x.ApplicationPath).Returns("~/"); Request.Setup(x => x.AppRelativeCurrentExecutionFilePath).Returns(url); Request.Setup(x => x.PathInfo).Returns(string.Empty); Response.Setup(x => x.Cookies).Returns(new HttpCookieCollection()); Response.Setup(x => x.ApplyAppPathModifier(It.IsAny<string>())).Returns((string path) => path); Response.Setup(x => x.Write(It.IsAny<string>())).Callback<string>(s => Output.Append(s)); var requestContext = new RequestContext(HttpContext.Object, new RouteData()); controller.ControllerContext = new ControllerContext(requestContext, controller); } /// <summary> /// Gets the HTTP context. /// </summary> /// <value>The HTTP context.</value> public Mock<HttpContextBase> HttpContext { get; private set; } /// <summary> /// Gets the request. /// </summary> /// <value>The request.</value> public Mock<HttpRequestBase> Request { get; private set; } /// <summary> /// Gets the response. /// </summary> /// <value>The response.</value> public Mock<HttpResponseBase> Response { get; private set; } /// <summary> /// Gets the output. /// </summary> /// <value>The output.</value> public StringBuilder Output { get; private set; } } /// <summary> /// Provides Fake Session for use in unit tests /// </summary> public class FakeSessionState : HttpSessionStateBase { /// <summary> /// backing field for the items in session /// </summary> private readonly Dictionary<string, object> _items = new Dictionary<string, object>(); /// <summary> /// Gets or sets the <see cref="System.Object"/> with the specified name. /// </summary> /// <param name="name">the key</param> /// <returns>the value in session</returns> public override object this[string name] { get { return _items.ContainsKey(name) ? _items[name] : null; } set { _items[name] = value; } } } 

There are a few things you could add, for example, to the HTTP Headers collection, but hopefully it demonstrates what you can do.

To use

 var controllerToTest = new HomeController(); var context = new MockHttpContextBase(controllerToTest); // do stuff that you want to test eg something goes into session Assert.IsTrue(context.HttpContext.Session.Count > 0); 

Regarding membership providers or other suppliers, you have come across something that can be difficult to verify. I would be distracted from using the provider for the interface, so that you can provide fake for the interface when testing a component that relies on it. You will still have a problem testing the specific implementation of the interface that the provider uses, but your mileage may vary depending on how far you want / should go in terms of unit testing and code coverage.

+4
source

You need to create wrapper interfaces for these services. The original MVC2 and MV3 starter project templates did this by default, but for some reason they dropped it in recent versions.

You can try to find AccountController source code samples to give you the source. They used IMembershipService and IFormsAuthenticationService

It is relatively simple to make fun of a session, context, etc.

0
source

Take a look at the MVCContrib project (http://mvccontrib.codeplex.com/), as they have a helper for creating controllers that have all the various context objects (like HttpContext).

0
source

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


All Articles