I have a unit test fixture in which I'm trying to test a ControllerAction on an ASP.NET MVC controller that's used for membership functions on a web app. I'm trying to mock the HttpContext for the tests. The ControllerAction under test actually sets properties on the HttpContext, such as Session values, Response.Cookies values, etc. This isn't all of the code, but here is a rough sample of the test that I'm trying to get to run:
[Test]
public void ValidRegistrationDataSuccessfullyCreatesAndRegistersUser()
{
var context = new Mock<HttpContextBase>() {DefaultValue = DefaultValue.Mock};
context.SetupAllProperties();
var provider = new Mock<MembershipProvider>(new object[] {context.Object});
var controller = new AccountController(context.Object, provider.Object);
// This just sets up a local FormCollection object with valid user data
// in it to use to attempt the registration
InitializeValidFormData();
ActionResult result = controller.Register(_registrationData);
Assert.IsInstanceOfType(typeof(ViewResult), result);
// Here is where I'd like to attempt to do Assertions against properties
// of the HttpContext, like ensuring that a Session object called "User"
// exists, and new auth cookie exists on the Response.Cookies collection.
// So far I've been unable to successfully check the values of those properties.
// I've been unsuccessful in getting those properties setup correctly on my
// mock object so that my ControllerAction can actually *set* their values,
// and that I can make assertions on them afterwards. The above code actually
// generates a StackOverflowException (which I've reported) on the
// context.SetupAllProperties() call. What am I doing wrong, or what do I need
// to do to be able to set and assert on those context properties?
}
Not sure what I'm doing wrong, but I'd love it if someone could point me in the right direction and show me how to setup this mock HttpContextBase object such that my controller can actually set values on its properties, and I can make assertions on those properties to ensure that my ControllerAction is doing what I need it to.
Am I approaching this the wrong way? I know that MVC Controllers have a ControllerContext that I can use to set values for Session, etc, but I can't figure out how something like that could be mocked without injecting it. Is there some way of doing that instead? (I also need to be able to pass the context in to my MembershipProvider too) Would that be a better approach?
Thanks.
I'm using a version of some code Steve Sanderson included in his Pro Asp.NET MVC book... and I'm currently having a moral dilemma whether it's okay to post the code here. How about I compromise with a highly stripped down version? ;)
So this can easily be reused, create a class similar to the one below that you will pass your controller. This will set up your mocks and set them to your controller's ControllerContext
public class ContextMocks
{
public Moq.Mock<HttpContextBase> HttpContext { get; set; }
public Moq.Mock<HttpRequestBase> Request { get; set; }
public RouteData RouteData { get; set; }
public ContextMocks(Controller controller)
{
//define context objects
HttpContext = new Moq.Mock<HttpContextBase>();
HttpContext.Setup(x => x.Request).Returns(Request.Object);
//you would setup Response, Session, etc similarly with either mocks or fakes
//apply context to controller
RequestContext rc = new RequestContext(HttpContext.Object, new RouteData());
controller.ControllerContext = new ControllerContext(rc, controller);
}
}
And then in your test method you'd just create an instance of ContextMocks and pass in the controller object you're testing:
[Test]
Public void test()
{
var mocks = new ContextMocks(controller);
var req = controller.Request;
//do some asserts on Request object
}
Seems very similar to Craig's examples, but this is with Moq v3. I have to give props to Steve Sanderson for this - I'm using this as a basis for testing all kinds of otherwise traditionally hard-to-test stuff: cookies, session, request method, querystring and more!
Here's how I do it.
public static HttpContextBase FakeHttpContext()
{
var context = new Mock<HttpContextBase>();
var request = new Mock<HttpRequestBase>();
var response = new Mock<HttpResponseBase>();
var session = new Mock<HttpSessionStateBase>();
var server = new Mock<HttpServerUtilityBase>();
var user = new Mock<IPrincipal>();
var identity = new Mock<IIdentity>();
request.Expect(req => req.ApplicationPath).Returns("~/");
request.Expect(req => req.AppRelativeCurrentExecutionFilePath).Returns("~/");
request.Expect(req => req.PathInfo).Returns(string.Empty);
response.Expect(res => res.ApplyAppPathModifier(It.IsAny<string>()))
.Returns((string virtualPath) => virtualPath);
user.Expect(usr => usr.Identity).Returns(identity.Object);
identity.ExpectGet(ident => ident.IsAuthenticated).Returns(true);
context.Expect(ctx => ctx.Request).Returns(request.Object);
context.Expect(ctx => ctx.Response).Returns(response.Object);
context.Expect(ctx => ctx.Session).Returns(session.Object);
context.Expect(ctx => ctx.Server).Returns(server.Object);
context.Expect(ctx => ctx.User).Returns(user.Object);
return context.Object;
}
This is an enhanced version of the MvcMockHelpers library released by Scott Hanselman. This is Moq 2.0 code; the syntax is slightly different in 3.
Related
I have ASP.NET MVC project. Now I am learning how to use UnitTesting. For this purpose I created UnitTest project.
I have below code in my Testclass :
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
Controller defaultpage = new Controller();
var result = defaultpage.defaultpage("43", "Y", "Y");
}
}
public ActionResult defaultpage(string uid, string access, string add)
{
Session["uid"] = uid;
Session["access"] = access;
Session["add"] = add;
}
Here when it comes to defaultpage controller method, it is giving Object reference not set to an instance of an object. .
How do I take session in controller when i call from UnitTest?
You have to mock session since it's not defined in the context of a unit test;
Here is a brief summary of how to mock session using c# and MOQ
http://www.syntaxsuccess.com/viewarticle/how-to-mock-httpcontext
The basics of it can be summed up with this block:
Mock<HttpContextBase> httpContext = new Mock<HttpContextBase>();
Mock<HttpSessionStateBase> session = new Mock<HttpSessionStateBase>();
session.Setup(s => s["SomeSessionKey"]).Returns(someObject);
httpContext.Setup(c => c.Session).Returns(session.Object);
Another alternative is to use NSubstitute which is a library with an api very similar to MOQ
You can't.
Your controller is working independent of the HTTP Context and HTTP pipeline that exists when visiting your ASP.NET MVC application in a hosted environment running IIS.
In this instance you'll need to look at mocking your session using a library such as Moq
Then you're able to create a mocked implementation of the HTTP context object which contains the session like so (code sample taken from this tutorial that I would recommend you take a look at):
// create the mock http context
var fakeHttpContext = new Mock();
// create a mock session object and hook it up to the mock http context
var sessionMock = new HttpSessionMock {{"selectedYear", 2013}, {"selectedMonth", 10}};
var mockSessionState = new Mock();
fakeHttpContext.Setup(ctx => ctx.Session).Returns(sessionMock);
// ... and here's how to attach a http context identity, just in case you'll come to need that, too
var fakeIdentity = new GenericIdentity("mno#ucsj.dk");
var principal = new GenericPrincipal(fakeIdentity, null);
fakeHttpContext.Setup(t => t.User).Returns(principal);
// we'll need to hook our http context up to a controller context mock - because we can't provide our controller with the http context mock directly
var homeControllerMock = new Mock();
homeControllerMock.Setup(foo => foo.HttpContext).Returns(fakeHttpContext.Object);
I know the difference between HttpContext and HttpContextWrapper is below...
HttpContext
This is the vintage asp.net context. The problem with this is that it has no base class and isn't virtual, and hence is unusable for testing (cannot mock it). It's recommended to not pass it around as function arguments, instead pass around variables of type HttpContextBase.
HttpContextBase
This is the (new to c# 3.5) replacement to HttpContext. Since it is abstract, it is now mockable. The idea is that your functions that expect to be passed a context should expect to receive one of these. It is concretely implemented by HttpContextWrapper
HttpContextWrapper
Also new in C# 3.5 - this is the concrete implementation of HttpContextBase. To create one of these in a normal webpage, use new HttpContextWrapper(HttpContext.Current).
The idea is that to make your code unit-testable, you declare all your variables and function parameters to be of type HttpContextBase, and use an IOC framework eg Castle Windsor to get it injected. In normal code, castle is to inject the equivalent of 'new HttpContextWrapper
(HttpContext.Current)', whereas in test code you're to be given a mock of HttpContextBase.
But I am not aware about its real use. I heard that it's useful in Unit Testing in comparing with Web Forms. but how it's useful ?
I also know that we can use it to execute the controller and Action as mentioned here
I heard that it's useful in Unit Testing in comparing with Web Forms. but how it's useful ?
Let's take an example of an ASP.NET MVC controller action which is adding a cookie to the response:
public class HomeController : Controller
{
public ActionResult Index()
{
var cookie = new HttpCookie("foo", "bar");
this.Response.Cookies.Add(cookie);
return View();
}
}
Notice the Response property over there. It's an HttpResponseBase. So we can mock it in a unit test:
public class HttpResponseMock: HttpResponseBase
{
private HttpCookieCollection cookies;
public override HttpCookieCollection Cookies
{
get
{
if (this.cookies == null)
{
this.cookies = new HttpCookieCollection();
}
return this.cookies;
}
}
}
public class HttpContextMock: HttpContextBase
{
private HttpResponseBase response;
public override HttpResponseBase Response
{
get
{
if (this.response == null)
{
this.response = new HttpResponseMock();
}
return this.response;
}
}
}
and now we could write a unit test:
// arrange
var sut = new HomeController();
var httpContext = new HttpContextMock();
sut.ControllerContext = new ControllerContext(httpContext, new RouteData(), sut);
// act
var actual = sut.Index();
// assert
Assert.AreEqual("bar", sut.Response.Cookies["foo"].Value);
And since all members are virtual we could use a mocking framework which would avoid us the need to write those mock classes for the unit test. For example with NSubstitute here's how the test might look:
// arrange
var sut = new HomeController();
var context = Substitute.For<HttpContextBase>();
var response = Substitute.For<HttpResponseBase>();
var cookies = new HttpCookieCollection();
context.Response.Returns(response);
context.Response.Cookies.Returns(cookies);
sut.ControllerContext = new ControllerContext(context, new RouteData(), sut);
// act
var actual = sut.Index();
// assert
Assert.AreEqual("bar", sut.Response.Cookies["foo"].Value);
Now let's take a WebForm:
protected void Page_Load(object sender, EventArgs)
{
var cookie = new HttpCookie("foo", "bar");
this.Response.Cookies.Add(cookie);
}
In this case the Response property is the concrete HttpResponse. So you are busted. Impossible to unit test in isolation.
I'm developing a webapp using ASP.NET MVC and C#. And I'm creating a unit test for this webapp using NUnit and Rhino Mock. My problem is that I have a Response object in my controller's action method and when I execute my unit test my test is failing because the Response object is a null reference.
Do I need to separate this Response object call in my actions or there is a better way to resolve this?
public ActionResult Login( string user, string password )
{
Response.Cookies[ "cookie" ].Value = "ck";
...
return View();
}
Please advise.
Many thanks.
What the controller really lacks is its HttpContext. In a test method it should be added explicitly if needed:
[Test]
public void TestMethod()
{
// Assume the controller is created once for all tests in a setup method
_controller.ControllerContext.HttpContext = new DefaultHttpContext();
var result = _controller.Login("username", "verySaf3Passw0rd");
// Asserts here
}
This is one of the annoying points where ASP.NET MVC is not as testable and loosely coupled as it could be. See this question for some suggestions how to mock the HTTP context objects.
I ended up creating a real response that my mock context returns like this...
Mock<HttpSessionStateBase> mockSession;
Mock<ControllerContext> mockContext;
Mock<ISessionProvider> mockSessionProvider;
HttpResponse testResponse;
MyController controller;
[TestInitialize]
public void Initialize()
{
testResponse = new HttpResponse(TextWriter.Null);
mockContext = new Mock<ControllerContext>();
mockSession = new Mock<HttpSessionStateBase>();
mockContext.Setup(x => x.HttpContext.Session).Returns(mockSession.Object);
mockContext.Setup(x => x.HttpContext.Response).Returns(new HttpResponseWrapper(testResponse));
controller = new MyController();
controller.ControllerContext = mockContext.Object;
}
I am new in unit test and MVC development.
I have a question for using moq for unit testing in asp.net mvc. I have a controller which accepts an ajax action:
[HttpPost,Authorize]
public ActionResult GrabLink()
{
string username = HttpContext.User.Identity.Name;
string rssUrl = Request.Params["Grablink"].ToString();
...}
This action deals with the http request which I generate from the view:
var mockRequest = new Moq.Mock<HttpRequestBase>();
but I can not find a way to define the parameters I used in the class. Also, is there any way to use the value binding provider directly to pass the value to the controller if I would like to do an ajax post?
I am a newbie in handling web request. If you have some good tutorial for better understanding the Http request (as well as the Httpcontext and related classes in asp.net) please post here. Thank you very much!
This works very well for me:
var controller = new HomeController();
var context = new Mock<HttpContextBase>(MockBehavior.Strict);
var controllerContext = new Mock<ControllerContext>();
controllerContext.SetupGet(x => x.HttpContext.User.Identity.Name)
.Returns("TestUser");
controllerContext.SetupGet(x => x.HttpContext.User.Identity.IsAuthenticated)
.Returns(true);
controllerContext.SetupGet(x => x.HttpContext.Request.IsAuthenticated)
.Returns(true);
controller.ControllerContext = controllerContext.Object;
// As a bonus, instantiate the Url helper to allow creating links
controller.Url = new UrlHelper(
new RequestContext(context.Object, new RouteData()), new RouteCollection());
This will allow you to initialize any user you want as an authenticated user, and the last line will allow you to user the Url helper within the controller even though you're calling it from a unit test.
As Scott said HttpContext makes Controllers hard to test. Anyway he's got a pretty solution at here.
BTW why didn't you make rssUrl a parameter if it is assigning by POST or GET?
e.g.
//POST: /GrabLink?rssUrl=bla bla...
[HttpPost,Authorize]
public ActionResult GrabLink(IPrincipal user, string rssUrl) {
string userName = user.Name;
}
Ok, #cem covered your second question very well.
For your first, nerddinner, and If I'm not mistaken, when you create a new Internet Application with Unit test, in Visual Studio, have the following mock classes for HttpContext. Its at the bottom of this file.
You could use these (or create a new Internet App +Tests with VS) and copy all the fake classes for your tests. (I wrote a Moq example below)
It looks like this:
public class MockHttpContext : HttpContextBase {
private IPrincipal _user;
public override IPrincipal User {
get {
if (_user == null) {
_user = new MockPrincipal();
}
return _user;
}
set {
_user = value;
}
}
public override HttpResponseBase Response
{
get
{
return new MockHttpResponse();
}
}
}
public class MockHttpResponse : HttpResponseBase {
public override HttpCookieCollection Cookies
{
get
{
return new HttpCookieCollection();
}
}
}
Not tested, but to Use mock it would look like this:
var fakeReqBase = new Mock<HttpRequestBase>();
fakeReqBase.Setup(f => f.User).Returns(new GenericIdentity("FakeUser"));
//generic identity implements IIdentity
fakeUserRepo.Object;//this returns fake object of type HttpRequestBase
Checkout the Moq Quickstart. Its quite easy to get used to, and the fluent interface really helps.
I'm trying to unit test a custom model binder - specifically, I want to see how it responds to various (possibly conflicting) values being submitted in the Request.Form and Request.QueryString collections - i.e. if I submit one value in the form and another in the querystring (yeah, yeah, I know, this is evil, but I want test coverage in case it happens) I can validate exactly which one will be bound to the model.
In order to do this, I'd like to mock/fake the HTTP context, and then invoke the model binder and see what is actually returned. I've seen several posts about testing ModelBinders, but all of them use a custom ValueProvider, whereas I actually want to test the way MVC is interacting with Form/Request collections.
Any ideas how I can mock these collections and then cause my model binder to use the 'default' ValueProvider based on this mocked HTTP context in my units tests? This is on ASP.NET MVC 1.0. Thanks.
Nailed it - the solution is to mock the ControllerContext, and then construct a new System.Web.Mvc.ValueProviderDictionary and pass your mocked controller context into the constructor, as follows:
[Test]
public void WorkFolder_Id_Is_Parsed_From_QueryString() {
var fakeControllerContext = GetControllerContext(null, "folder=10");
var bindingContext = new ModelBindingContext() {
ValueProvider = new System.Web.Mvc.ValueProviderDictionary(fakeControllerContext),
ModelName = "menu",
FallbackToEmptyPrefix = true
};
var binder = new RenewalMenuPostModelBinder();
var model = binder.BindModel(fakeControllerContext, bindingContext) as RenewalMenuPostModel;
Assert.That(model is RenewalMenuPostModel);
Assert.That(model.WorkFolderId.HasValue);
Assert.That(model.WorkFolderId.Value == 10);
}
private static ControllerContext GetControllerContext(NameValueCollection form, string queryString) {
Mock<HttpRequestBase> mockRequest = new Mock<HttpRequestBase>();
mockRequest.Expect(r => r.Form).Returns(form);
var queryStringCollection = HttpUtility.ParseQueryString(queryString);
mockRequest.Expect(r => r.QueryString).Returns(queryStringCollection);
Mock<HttpContextBase> mockHttpContext = new Mock<HttpContextBase>();
mockHttpContext.Expect(c => c.Request).Returns(mockRequest.Object);
return new ControllerContext(mockHttpContext.Object, new RouteData(), new Mock<ControllerBase>().Object);
}