Using / Mocking Request.Url when using MvcContrib TestHelper - asp.net-mvc

I'm using MvcContrib.TestHelper, and initialize my controller like this:
var accountController = new AccountController();
var builder = new TestControllerBuilder();
builder.InitializeController(accountController);
My problem is, that inside my AccountController I have:
Request.Url.GetLeftPart(UriPartial.Authority);
However, this comes back as null. Request is a proxy.
How can I set this up from my unit test?

This has probably already been solved, but what worked for me was to use a mocking framework (like RhinoMocks):
var contextMock = MockRepository.GenerateMock<HttpContextBase>();
contextMock.Expect(x => x.Request).Return(new FakeHttpRequest("SOME_RELATIVE_URL", new Uri("http://to.somewhere.com"), new Uri("http://from.somewhere.com")));
controller.ControllerContext.HttpContext = contextMock;

Related

Testing Account/Logon Action

I'm testing the Account/Loggon action using the built-in testing tool of Visual Studio 2010 and the class library from this article to create a fake controller context.
When I run the test method, this code line:
FormsAuthentication.SetAuthCookie(username, false);
throws an exception: Object reference not set to an instance of an object
To test the loggon action, I think I should create a controller with a fake controller context that has a cookie collection. Here is my testing code block:
AccountController controller = new AccountController();
var cookies = new HttpCookieCollection();
controller.ControllerContext = new FakeControllerContext(controller, cookies);
ActionResult result = controller.RemoteLogOn(username, password);
I'm not sure if this is the right way, but this is what we do, and it works.
Instead of directly using FormsAuthentication.SetAuthCookie, abstract it into an interface, e.g IFormsAuthenticationService, and implement as per regular.
Accept that in your MVC controllers where required, e.g:
public AccountController(IFormsAuthenticationService formsAuthenticationService)
{
_formsAuthenticationService = formsAuthenticationService; // should use DI here
}
public ActionResult LogOn(string username, string pw)
{
if (yourLogicWhichChecksPw)
_formsAuthenticationService.SetAuthCookie(username, false);
return RedirectToAction("Index");
}
Then in your unit-test, use something like Moq to fake out the interface.
var username = "blah";
var pw = "blah";
var fakesFormsAuth = new Mock<IFormsAuthenticationService>();
fakeFormsAuth.Verify(x => x.SetAuthCookie(username, false), Times.AtLeastOnce());
var controller = new AccountController(fakedFormsAuth.Object);
controller.LogOn(username, pw);
The reason for mocking this is because there is absolutely no need to unit-test Forms Authentication. It's a built-in, well tested and stable part of the ASP.NET framework. That's why we mock things where we don't care about the underlying implementation, instead we only test that certain conditions were met (it was called, exception was thrown, some variable was set, etc).
Test your own code, not the mechanics of .NET.
As for Stephen Walther's article, that's more for faking the RequestContext when certain code your testing expects data in the Request. Such as the User.Identity, Request.IsAuthenticated, Form variables, etc. That's where you need to fake the context, such as the following code:
public ActionResult Save(SomeModel)
{
var user = Request.User.Identity; // this will be null, unless you fake the context.
}

How do I unit test a custom ActionFilter in ASP.Net MVC

So I'm creating a custom ActionFilter that's based mostly on this project http://www.codeproject.com/KB/aspnet/aspnet_mvc_restapi.aspx.
I want a custom action filter that uses the http accept headers to return either JSON or Xml. A typical controller action will look like this:
[AcceptVerbs(HttpVerbs.Get)]
[AcceptTypesAttribute(HttpContentTypes.Json, HttpContentTypes.Xml)]
public ActionResult Index()
{
var articles = Service.GetRecentArticles();
return View(articles);
}
The custom filter overrides the OnActionExecuted and will serialize the object (in this example articles) as either JSON or Xml.
My question is: how do I test this?
What tests do I write? I'm a TDD novice and am not 100% sure what I should be testing and what not to test. I came up with AcceptsTypeFilterJson_RequestHeaderAcceptsJson_ReturnsJson(), AcceptsTypeFilterXml_RequestHeaderAcceptsXml_ReturnsXml() and AcceptsTypeFilter_AcceptsHeaderMismatch_ReturnsError406().
How do I test an ActionFilter in MVC that is testing the Http Accept Headers?
Thanks.
You just need to test the filter itself. Just create an instance and call the OnActionExecuted() method with test data then check the result. It helps to pull the code apart as much as possible. Most of the heavy lifting is done inside the CsvResult class which can be tested individually. You don't need to test the filter on an actual controller. Making that work is the MVC framework's responsibility.
public void AcceptsTypeFilterJson_RequestHeaderAcceptsJson_ReturnsJson()
{
var context = new ActionExecutedContext();
context.HttpContext = // mock an http context and set the accept-type. I don't know how to do this, but there are many questions about it.
context.Result = new ViewResult(...); // What your controller would return
var filter = new AcceptTypesAttribute(HttpContentTypes.Json);
filter.OnActionExecuted(context);
Assert.True(context.Result is JsonResult);
}
I just stumbled upon this blog post which seems the right way to me. He uses Moq.
What this chap is doing is mocking the HTTPContext, but also we need to set up a ContentType in the request:
// Mock out the context to run the action filter.
var request = new Mock<HttpRequestBase>();
request.SetupGet(r => r.ContentType).Returns("application/json");
var httpContext = new Mock<HttpContextBase>();
httpContext.SetupGet(c => c.Request).Returns(request.Object);
var routeData = new RouteData(); //
routeData.Values.Add("employeeId", "123");
var actionExecutedContext = new Mock<ActionExecutedContext>();
actionExecutedContext.SetupGet(r => r.RouteData).Returns(routeData);
actionExecutedContext.SetupGet(c => c.HttpContext).Returns(httpContext.Object);
var filter = new EmployeeGroupRestrictedActionFilterAttribute();
filter.OnActionExecuted(actionExecutedContext.Object);
Note - I have not tested this myself.

MVC view unit test visibility of element based on authentication

I'm writing an MVC app.
If I have a piece of code:
#if (User.IsInRole("Administrator")) {
#Html.DropDownListFor(...)
}
So the dropdownlistfor is only visible or even there for administrators.
How do you unit test that this is happening?
This is what I would do:
Create a stub for the User object (of type IPrincipal)
Create a stub for the Request object (of type HttpRequestBase) with the stubbed User object
Inject the stubbed request object to your request context.
See example below (using Rhino mocks)
var CurrentUser = MockRepository.GenerateStub<IPrincipal>();
CurrentUser.Stub(u => u.IsInRole("Administrator")).Return(true);
var context = MockRepository.GenerateStub<HttpContextBase>();
var requestContext = new RequestContext(context, new RouteData());
var request = MockRepository.GenerateStub<HttpRequestBase>();
context.User = CurrentUser;
context.Stub(c => c.Request).Return(request);
controller = new YourController();
controller.ControllerContext = new ControllerContext(requestContext, controller);
var view = controller.DoAction() as ViewResult;
That would help you setup the controller and the current user with all required roles.
I personally wouldn't unit test the view as it is supposed to be thin and dump. Most of your stuff should happen in the controller/business layer.
If you want to unit test your views, I would suggest checking out the Razor Single File Generator. This will allow you to pre-compile your views, and to create unit tests as well.

MVC3 Moq account controller c# nUnit

I am trying to learn Moq but it is proving somewhat difficult.
If I want to implement some basic tests using nUnit and Moq for the account controller in a new MVC3 project, how would I go about it?
Im used to the entity framework. but not building interfaces for it.
edit:
I understand the theory of it all and the need to do it, but implementing it is confusing me
I have been using Entity Code generator (dbContext) to generate code I can use for interfaces
Ok, here is a good test: When you register a new user, you want to make sure that he will be automatically signed in the site, so he doest not need to type his username and password again.
The test would be something like this:
public void AutomaticallySignedInAfterRegistering()
{
var membershipService = new Mock<IMembershipService>();
var formsService = new Mock<IFormsAuthenticationService>();
RegisterModel newUser = new RegisterModel();
newUser.UserName = "John"
newUser.Email = "john#somewhere.com"
newUser.Password = "p#ss";
newUser.ConfirmPassword = "p#ss";
membershipService.Setup(x => x.CreateUser("John", "p#ss", "john#somewhere.com")).Returns(MembershipCreateStatus.Success);
AccountController controller = new AccountController();
controller.FormsService = formsService.Object;
controller.MembershipService = membershipService.Object;
controller.Register(newUser);
formsService.Verify(x => x.SignIn("John", false), Times.Once());
}
The key here is the Verify method. It works just like an Assert. In this case you are verifing that the method SignIn was called exactly once. This is an example of how to use mocks to check if the Account Controller is working as expected.

How to use Rhino Mocks to Mock an HttpContext.Application

I'm new to Mocking frameworks and have started using RhinoMocks to assist with my MVC App Unit Testing.
I'm using Scott Hanselmanns MVC Mock Helper to assist in mocking the HttpContext.
I've succesfully (after some time) mocked some of what I need but have come unstuck when it comes to the Application property of the HttpContext.
In my application I store an object in the Application and retrieve it within a Controller like:
SomeObj foo = (SomeObj)Application["fooKey"];
This gets created on Application_Start in my MVC App.
UPDATED FOLLOWING FIRST ANSWER (additional code for clarity)
Currently in the test setup I do:
HttpContextBase mockHttpBase = mocks.FakeHttpContext();
controllerToTest = new SomeController();
mocks.SetFakeControllerContext(controllerToTest);
HttpApplicationStateBase appState =
MockRepository.GenerateStub<HttpApplicationStateBase>();
Globals tmpAppGlobals =
new Globals();
mockHttpBase.Expect(ctx => ctx.Application).Return(appState);
mockHttpBase.Expect(ctx => ctx.Application[Globals.GlobalsKey]).
Return(tmpAppGlobals);
In my unit test setup I do:
Globals tmpAppGlobals = new Globals();
controllerToTest.ControllerContext.HttpContext.
Expect(ctx => ctx.Application[Globals.GlobalsKey]).
Return(tmpAppGlobals);
This call throws a NullReference Exception, for the Application object.
My question is two fold:
1) Is this the right approach or have I done something wrong from a design / architecture perspective?
2) Why doesn't this work?!
Thanks, in advance.
Without delving too deeply, this looks mostly correct.
The Application property is virtual on HttpContextBase, so you should be able to set up a return value for it from Rhino -- Assuming you're mocking HttpContextBase as Scott Hanselmanns post does.
Some possible causes, which are really just guesses from lack of information:
Did you set up returns for
controllerToTest.ControllerContext?
Did you set up a return for that
objects HttpContext property?
Did you set up a return for that
objects Application property?
The reason I ask is that typically when you do expectation setups, you already have references to the objects that will be called as part of your test, so you wouldn't do a property chain like you do with your controllerToTest.ControllerContext.HttpContext.
Expect() call.
Edit:
I think I see the problem, and I think it's with this part:
Expect(ctx => ctx.Application[Globals.GlobalsKey])
I think you're assuming that indexers work the same as properties, when they don't. What you really need to do is set up an expectation on your appState object to receive a call to the Item property, like this:
// setup expectations -- assumes some of the expectations and mocks
// the from original question
mockHttpBase.Expect(ctx => ctx.Application).Return(appState);
appState.Expect(ctx => ctx.Item(Globals.GlobalsKey)).Return(tmpAppGlobals);
// run the test
you could use the below for Moq. It took me awhile how to mock the HttpApplication, and the appState.Object is the return method duh!
public static HttpContextBase FakeHttpContext()
{
var context = new Mock<HttpContextBase>();
var request = new Mock<HttpRequestBase>();
var response = new Mock<HttpResponseBase>();
var session = new FakeHttpSessionState();
var server = new Mock<HttpServerUtilityBase>();
var appState = new Mock<HttpApplicationStateBase>();
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);
context.Setup(ctx => ctx.Application).Returns(appState.Object);
//emulate session (HttpContext.Current.Session)
var contx = new HttpContext(new MyApp.NUnit.Tests.Fakes.FakeHttpWorkerRequest());
contx.Items["AspSession"] = CreateSession();
HttpContext.Current = contx;
return context.Object;
}

Resources