Created
March 2, 2018 18:01
-
-
Save lorddev/25592a57c18d310e39f952c8c9fffb11 to your computer and use it in GitHub Desktop.
Mock context stuff for unit testing controllers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Collections.Specialized; | |
using System.Web; | |
using System.Web.Mvc; | |
using System.Web.Routing; | |
using Moq; | |
namespace UnitTests | |
{ | |
/// <summary> | |
/// http://www.hanselman.com/blog/ASPNETMVCSessionAtMix08TDDAndMvcMockHelpers.aspx | |
/// </summary> | |
public static class MvcMockHelpers | |
{ | |
public static HttpContextBase FakeHttpContext() | |
{ | |
var session = new Mock<HttpSessionStateBase>(); | |
return FakeHttpContext(session.Object); | |
} | |
public static HttpContextBase FakeHttpContext(string url) | |
{ | |
var context = FakeHttpContext(); | |
context.Request.SetupRequestUrl(url); | |
return context; | |
} | |
public static void SetFakeControllerContext(this Controller controller) | |
{ | |
var httpContext = FakeHttpContext(); | |
var context = new ControllerContext(new RequestContext(httpContext, new RouteData()), controller); | |
controller.ControllerContext = context; | |
} | |
private static string GetUrlFileName(string url) | |
{ | |
if (url.Contains("?")) | |
{ | |
return url.Substring(0, url.IndexOf("?")); | |
} | |
return url; | |
} | |
private static NameValueCollection GetQueryStringParameters(string url) | |
{ | |
if (url.Contains("?")) | |
{ | |
var parameters = new NameValueCollection(); | |
var parts = url.Split("?".ToCharArray()); | |
var keys = parts[1].Split("&".ToCharArray()); | |
foreach (var key in keys) | |
{ | |
var part = key.Split("=".ToCharArray()); | |
parameters.Add(part[0], part[1]); | |
} | |
return parameters; | |
} | |
return null; | |
} | |
public static void SetHttpMethodResult(this HttpRequestBase request, string httpMethod) | |
{ | |
Mock.Get(request) | |
.Setup(req => req.HttpMethod) | |
.Returns(httpMethod); | |
} | |
public static void SetupRequestUrl(this HttpRequestBase request, string url) | |
{ | |
if (url == null) | |
{ | |
throw new ArgumentNullException("url"); | |
} | |
if (!url.StartsWith("~/")) | |
{ | |
throw new ArgumentException("Sorry, we expect a virtual url starting with \"~/\"."); | |
} | |
var mock = Mock.Get(request); | |
mock.Setup(req => req.QueryString) | |
.Returns(GetQueryStringParameters(url)); | |
mock.Setup(req => req.AppRelativeCurrentExecutionFilePath) | |
.Returns(GetUrlFileName(url)); | |
mock.Setup(req => req.PathInfo) | |
.Returns(string.Empty); | |
} | |
/// <inheritdoc /> | |
/// <summary> | |
/// A Class to allow simulation of SessionObject | |
/// </summary> | |
public class MockHttpSession : HttpSessionStateBase | |
{ | |
readonly Dictionary<string, object> _sessionStorage = new Dictionary<string, object>(); | |
public override object this[string name] | |
{ | |
get => _sessionStorage[name]; | |
set => _sessionStorage[name] = value; | |
} | |
} | |
// https://stackoverflow.com/questions/524457/how-do-you-mock-the-session-object-collection-using-moq | |
public static HttpContextBase FakeHttpContext(HttpSessionStateBase session) | |
{ | |
var context = new Mock<HttpContextBase>(); | |
var request = new Mock<HttpRequestBase>(); | |
var response = new Mock<HttpResponseBase>(); | |
var server = new Mock<HttpServerUtilityBase>(); | |
context.Setup(ctx => ctx.Request).Returns(request.Object); | |
context.Setup(ctx => ctx.Response).Returns(response.Object); | |
context.Setup(ctx => ctx.Session).Returns(session); | |
context.Setup(ctx => ctx.Server).Returns(server.Object); | |
return context.Object; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment