Creating an action based on a mocked controller - asp.net-mvc

I have the following test method that is being used to test a controller.
[TestMethod()]
public void ResetTest()
{
var target = new Mock<PROJECT.Reset.Controllers.ResetController>();
//var target = new PROJECT.Reset.Controllers.ResetController();
FormCollection form = new FormCollection();
form.Set("username","domain\\username");
form.Set("password","testpass123");
form.Set("confirmpassword","testpass123");
ViewResult expected = new ViewResult();
expected.ViewName = "Reset";
expected.ViewData.Model = rm;
ActionResult action = target.Object.;
action = target.Object.Reset(form);
ViewResult actual = (ViewResult)action;
Assert.AreEqual(expected, actual);
}
If I use the:
var target = new Mock<PROJECT.Reset.Controllers.ResetController>();
Then the action is being evaluated as null. If I change this to the line of code below that which is uncommented then the action is being set. Any ideas what might be causing this behaviour?

Why are you mocking this? It seems that this is your actual SUT (solution under test). So, you should not mock it at all.

Your understanding about Mocking framework is may be incorrect. You should not mock the object which is you are going to test (controller object in your case). You can/do mock only for dependency objects.
Ex : Repository objects and etc.

Related

How to pass Model value from Test class to Action Method in Controller class

I am creating some Nunit tests for an MVC application. I am writing a test case for a methods in my Controller class. I am using Nsubstitute for Mocking the object.
I am learning about Nunit and Nsubstitue and I don't know, how to pass my Model value which was mocked in the testcase method to my controller method.
Below is my method in the controller class:
public ActionResult Manage(string id)
{
var clusterCollections = ReadXml();
int clusterIndex = clusterCollections.ClusterCollectionList.FindIndex(a => a.ClusterId == id);
var model = new ClusterManagementModel()
{
ClusterNodeDetailsList = BindClusterDetailsToGrid(id),
DropDownListClusterName = BindClusterNameToDropDown(),
CurrentClusterId = clusterIndex,
CurrentClusterName = id,
HStatus = Hstatus(id),
IStatus = Istatus(id)
};
return View(model);
}
This is the TestCase I have written:
[TestCase]
public void TestManage()
{
var ManagementController = Substitute.ForPartsOf<ClusterManagementController>();
var ManagementModel = Substitute.ForPartsOf<ClusterManagementModel>();
ClusterCollections clusterCollection = new ClusterCollections();
List<ClusterNodeDetails> ClusterNodes = new List<ClusterNodeDetails>();
List<DDL_ClusterName> DropDownListClusterName = new List<DDL_ClusterName>();
ManagementController.ReadXml().Returns(clusterCollection);
ManagementModel = new ClusterManagementModel()
{
ClusterNodeDetailsList = ClusterNodes,
DropDownListClusterName = DropDownListClusterName,
CurrentClusterId = 1,
CurrentClusterName = "UnitTesting",
HStatus = "True",
IStatus = "Success"
};
var result = ManagementController.Manage("1") as ActionResult;
Assert.AreEqual(ManagementModel, result);
}
If I have made mistakes in the TestCase Method please correct me.
If my TestCase is wrong, can you give me a suggestion how to write a TestCase for the above method (public ActionResult Manage(string id))
I don't know, how to pass Model value
The short answer is that in its current form, you can't pass the model you've created in your test to your controller. This is a common problem that people run into when they first start trying to unit test their code. The way out of the hole is to start writing your production code with testing in mind.
One approach that is common is to extract dependencies from your class and to inject these dependencies through the constructor for the class. So, you might extract your model creation logic into a ModelFactory and modify your controller to have a constructor like this:
public ManagementController(IModelFactory modelFactory) {
_modelFactory = modelFactory;
}
There are various libraries that can help to inject these dependencies (Ninject, AutoFac etc). But if you don't want to / can't use them, then you will also need to add a default constructor that sets up your dependencies to default concrete implementations:
public ManagementController() {
_modelFactory = new ModelFactory();
}
This allows you to create Stubbed / Mocked / Substituted implementations of IModelFactory and inject it into your controller / other class under test. So, your test might start off something like this:
List<ClusterNodeDetails> ClusterNodes = new List<ClusterNodeDetails>();
List<DDL_ClusterName> DropDownListClusterName = new List<DDL_ClusterName>();
var model = new ClusterManagementModel()
{
ClusterNodeDetailsList = ClusterNodes,
DropDownListClusterName = DropDownListClusterName,
CurrentClusterId = 1,
CurrentClusterName = "UnitTesting",
HStatus = "True",
IStatus = "Success"
};
var modelFactory = Substitute.For<IModelFactory>();
modelFactory.CreateClusterManagementModel( /* args for model creation */).Returns(model);
var sut = new ManagementController(modelFactory);
var result = sut.Manage("1") as ActionResult;
You also need to think about what it is you're trying to test. By extracting the dependencies you're able to focus on the logic in your controller and focus your tests on that logic. It's very easy when you start using Mocks to get into a situation where you're not actually testing anything at all, other than whether or not you've set your Mocks up correctly. Remember, they are there to help you reproduce specific scenarios you need to force your production code to follow a particular flow, not to replace the logic contained in your production code.

Unit Testing MVC Controllers

A controller in my ASP.NET MVC application pre-populates form data displayed by my view according to a couple of fairly straight forward rules.
It seems like this would be a good thing to cover in my unit testing. But the only way I could see to verify the correct data is placed in the form, would be to extract the logic from the controller in what feels like an unnatural way.
Can someone suggest ways to approach this?
All the examples I've found of unit testing controllers seemed very trivial, such as verifying it returned the expected type of view. I'm not sure I even see the value in that.
You can test by casting the returned object to the appropriate class, instead of using their base class (which is returned by default)
For example, to test the default AccountController you'd so something like this:
var controller = new AccountController();
var result = controller.LogOn() as ViewResult;
var model = result.Model as LogOnModel;
Assert.IsTrue(model.RememberMe); // assuming you "pre-populated" enabled the checkbox
Checking if the returned object is filled with the right data does not seem "unnatural" to me, or did you meant it differently?
I would agree that testing the type of view returned would be somewhat pointless. However, testing that the expected "view" was returned along with its correct data would be a valid test case IMO.
For example here is a singular edit test case for an edit controller. Note, that this example is making use of Moq and Nunit but that aside it's fairly straight forward.
Note, that that ViewResult is cast to the expected view model and the assertions are then made against the expected contact.
Test:
[Test]
public void Edit_Get_Should_Lookup_Contact_From_Repository_And_Return_Edit_View()
{
// arrange
var _repository = new Mock<IContactRepository>();
var expectedContact = new Contact
{
First = "first",
Last = "last",
Email = "mail#test.com"
};
var mockContext = new Mock<ControllerContext>();
_repository.Setup(x => x.GetById(It.IsAny<int>())).Returns(expectedContact);
var controller = new ContactController(_repository.Object)
{
ControllerContext = mockContext.Object
};
// act
var result = controller.Edit(1) as ViewResult;
var resultData = (Contact)result.ViewData.Model;
// assert
Assert.AreEqual("Edit", result.ViewName);
Assert.AreEqual(expectedContact.First, resultData.First);
Assert.AreEqual(expectedContact.Last, resultData.Last);
Assert.AreEqual(expectedContact.Email, resultData.Email);
}
Controller:
[HttpGet]
public ActionResult Edit(int id)
{
var contact = _repository.GetById(id);
return View("Edit", contact);
}

Response Object is a null reference in my Controller's action method

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;
}

asp.net mvc How to test controllers correctly

I'm having difficulty testing controllers. Original my controller for testing looked something like this:
SomethingController CreateSomethingController()
{
var somethingData = FakeSomethingData.CreateFakeData();
var fakeRepository = FakeRepository.Create();
var controller = new SomethingController(fakeRepository);
return controller;
}
This works fine for the majority of testing until I got the Request.IsAjaxRequest() part of code. So then I had to mock up the HttpContext and HttpRequestBase. So my code then changed to look like:
public class FakeHttpContext : HttpContextBase
{
bool _isAjaxRequest;
public FakeHttpContext( bool isAjaxRequest = false )
{
_isAjaxRequest = isAjaxRequest;
}
public override HttpRequestBase Request
{
get
{
string ajaxRequestHeader = "";
if ( _isAjaxRequest )
ajaxRequestHeader = "XMLHttpRequest";
var request = new Mock<HttpRequestBase>();
request.SetupGet( x => x.Headers ).Returns( new WebHeaderCollection
{
{"X-Requested-With", ajaxRequestHeader}
} );
request.SetupGet( x => x["X-Requested-With"] ).Returns( ajaxRequestHeader );
return request.Object;
}
}
private IPrincipal _user;
public override IPrincipal User
{
get
{
if ( _user == null )
{
_user = new FakePrincipal();
}
return _user;
}
set
{
_user = value;
}
}
}
SomethingController CreateSomethingController()
{
var somethingData = FakeSomethingData.CreateFakeData();
var fakeRepository = FakeRepository.Create();
var controller = new SomethingController(fakeRepository);
ControllerContext controllerContext = new ControllerContext( new FakeHttpContext( isAjaxRequest ), new RouteData(), controller );
controller.ControllerContext = controllerContext;
return controller;
}
Now its got to that stage in my controller where I call Url.Route and Url is null. So it looks like I need to start mocking up routes for my controller.
I seem to be spending more time googling on how to fake/mock objects and then debugging to make sure my fakes are correct than actual writing the test code. Is there an easier way in to test a controller? I've looked at the TestControllerBuilder from MvcContrib which helps with some of the issues but doesn't seem to do everything. Is there anything else available that will do the job and will let me concentrate on writing the tests rather than writing mocks?
Thanks
You can use some of the libraries that give you out of the box some of these objects. For example RhinoMock, NMock ... etc. I personally use Moq - it's good enough and free. What i like most in Moq is the linq expressions.
Most mocking engine will do all this for you. I use RhinoMocks but there are a lot more available. Also Moles is very new and interesting mocking engine (this generally comes with Pex which is yet more ammo in your unit testing arsenal)
MvcContrib + RhinoMocks. Check out the TestControllerBuilder in the MvcContrib.TestHelper library. Here's the official write-up: http://mvccontrib.codeplex.com/wikipage?title=TestHelper#Examples.
Here's an example of mocking a controller out for testing a UrlHelper: ASP.NET MVC: Mock controller.Url.Action
Here's a short explanation of how to use the TestControllerBuilder: http://codebetter.com/blogs/kyle.baley/archive/2008/03/19/testcontrollerbuilder-in-mvccontrib.aspx
Instead of mocking stuff, you can pass IAjaxRequest to constructor. Or make it base constructor class property (and use property injection). Or you can make your constructor implement IAjaxRequest and then apply global action filter on base constructor class that will setup IAjaxRequest.
This will help to abstract many things, including HttpContext stuff. Just don't abstract IHttpContext, abstract IUserContext, ISessionStorage, IAuthentication, IRequestDetails...
Another way is to use model binder directly on methods where you need specific information. See this post for example. You can make binder that will give you IsAjaxRequest, then you just make action to accept this parameter. Works very well because information is provided exactly to the method that needs it, not to the whole controller.

Testing the User.IsInRole in MVC.NET

I am trying to test the User.IsInRole("Administrator") in my application, and actually trying to assign the User Role to ("Administrator") so that my test will pass. I am using Scott Hanselman's MvcMockHelpers to do this, and at the moment I have the following test.
[Test]
public void Create_CanInsertNewArticleView_IsNotNull()
{
// Arrange
var controller = new ArticleController();
MockRepository mockRepo = new MockRepository();
var fakeContext = MvcMockHelpers.FakeHttpContext(mockRepo, "~/Article/Create");
fakeContext.User.IsInRole("Administrator");
// Act
Article fakeArticle = FakeObjects.ReturnFakeArticle();
var result = controller.Create(fakeArticle) as ViewResult;
// Assert
Assert.IsNotNull(result);
}
However the actual controller's User is null at the moment.
Can anyone help me out and tell me what the correct test should be, to User.IsInRole("Administrator")
Thanks for your help and time
Johann
See this related answer for details.
Here's the code snippet from the other answer converted to Rhino Mocks:
var user = new GenericPrincipal(new GenericIdentity(string.Empty), null);
var httpCtx = MockRepository.GenerateStub<HttpContextBase>();
httpCtx.User = user;
var controllerCtx = new ControllerContext();
controllerCtx.HttpContext = httpCtx;
sut.ControllerContext = controllerCtx;
You'll have to mock an IPrincipal for your user object, set it up so that .IsInRole("Administrator") returns true, and then set your fakeContext up to return that IPrincipal for the.User property. It would be something like this:
EDIT: It turns out the OP is using Rhino Mocks, and the code I provided was for Moq. Here's an attempt att writing Rhino code, although I have never actually used Rhino myself. The original Moq code can be found below.
In Rhino Mocks, you'll want to add another helper method, or change the current one, so you have the following:
public static HttpContextBase FakeHttpContext(this MockRepository mocks, string url, IPrincipal user)
{
// Do the same setup as Scott does...
// ...and add this:
SetupResult.For(context.User).Return(user);
mocks.Replay(context);
return context,
}
Then you declare and set up your IPrincipal mock object like this before the call to FakeHttpContext, and send the mock object in as the third parameter.
In Moq:
fakeContext = MvcMockHelpers.FakeHttpContext("~/Article/Create");
fakeUser = new Mock<IPrincipal>();
fakeUser.Expect(usr => usr.IsInRole(It.IsAny<String>())).Returns(true);
fakeContext.Expect(context => context.User).Returns(fakeUser.Object);
(Disclaimer: It was a while since I wrote a unit test, and this code has not been tested even for compilation errors. Hence, there might be some wrinkles that need to be ironed out before you can use this code, but you get the general idea...)

Resources