Hi,
Having played around with JustMock, I couldn't figure out how to test a controller action properly. I can't seem to figure out how to properly instantiate the controller (with request, session etc).
Here's a controller with a simple action (which renders a custom login form):
public class LoginController : Controller
{
public ActionResult LogOn(string company)
{
ViewData["showCompany"] = company == null;
ViewData["company"] = company;
return View();
}
}
I'd like to write a test on this method, but have no idea how to correctly instantiate the controller object. Having created the following test attempt, it doesn't work. When debugging it the action is not fully executed and the actionResult is null:
// arrange
var outputString = new StringWriter();
var context = new HttpContextWrapper(new HttpContext(new HttpRequest("/", "http://localhost/mft/Login/LogOn", ""), new HttpResponse(outputString)));
var loginController = new LoginController();
Mock.Arrange(() => loginController.HttpContext).Returns(context);
Mock.Arrange(() => loginController.LogOn("mft")).MustBeCalled();
// act
var actionResult = loginController.LogOn("mft");
// assert
Mock.Assert(loginController);
I realized the ViewData is null so I modified the test like so:
loginController.ViewData = new ViewDataDictionary();
However, this doesn't seem to be solving the problem. The actionResult keeps being null and I feel like I'm going the wrong way. There should be a simple way to instantiate a controller and call an action. Later on I will have to use a session cookie that I can get by calling something like:
[AcceptVerbs(HttpVerbs.Post)] public ActionResult LogOn(string userName, string password, string company, string returnUrl){}
I've been looking for examples all over but just couldn't find one that works.
Thanks,
Mihai
Having played around with JustMock, I couldn't figure out how to test a controller action properly. I can't seem to figure out how to properly instantiate the controller (with request, session etc).
Here's a controller with a simple action (which renders a custom login form):
public class LoginController : Controller
{
public ActionResult LogOn(string company)
{
ViewData["showCompany"] = company == null;
ViewData["company"] = company;
return View();
}
}
I'd like to write a test on this method, but have no idea how to correctly instantiate the controller object. Having created the following test attempt, it doesn't work. When debugging it the action is not fully executed and the actionResult is null:
// arrange
var outputString = new StringWriter();
var context = new HttpContextWrapper(new HttpContext(new HttpRequest("/", "http://localhost/mft/Login/LogOn", ""), new HttpResponse(outputString)));
var loginController = new LoginController();
Mock.Arrange(() => loginController.HttpContext).Returns(context);
Mock.Arrange(() => loginController.LogOn("mft")).MustBeCalled();
// act
var actionResult = loginController.LogOn("mft");
// assert
Mock.Assert(loginController);
I realized the ViewData is null so I modified the test like so:
loginController.ViewData = new ViewDataDictionary();
However, this doesn't seem to be solving the problem. The actionResult keeps being null and I feel like I'm going the wrong way. There should be a simple way to instantiate a controller and call an action. Later on I will have to use a session cookie that I can get by calling something like:
[AcceptVerbs(HttpVerbs.Post)] public ActionResult LogOn(string userName, string password, string company, string returnUrl){}
I've been looking for examples all over but just couldn't find one that works.
Thanks,
Mihai