So I'm trying to unit-test a controller method. I'm using MSTest in VS 2010, and Moq 3.1
Test method:
[TestMethod]
public void TestAccountSignup()
{
var request = new Mock<HttpRequestBase>();
var context = new Mock<HttpContextBase>();
AccountController controller = new AccountController();
controller.ControllerContext = new System.Web.Mvc.ControllerContext(context.Object, new RouteData(), controller);
request.Setup(x => x.Cookies).Returns(new HttpCookieCollection());
context.Setup(x => x.Request).Returns(request.Object);
string username = StringHelper.GenerateRandomAlpha(10);
var res = controller.Register(username, "foozbaaa+" + username + "@example.com", null, true, "Testing!", null);
}
My controller method:
[CaptchaValidator]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Register(string userName, string email,string existingUsername, bool captchaValid, string heardAbout, string heardAboutOther)
{
//Loads of stuff here....
//cool - all registered
//This line gives the problem
return new RedirectResult(this.BuildUrlFromExpression<AccountController>(x => x.AccountCreated()));
}
The controller method works just fine when not unit testing.
When mocking and calling in this way, I get a System.Security.VerificationException on that last line:
Method Microsoft.Web.Mvc.LinkBuilder.BuildUrlFromExpression: type argument 'TController' violates the constraint of type parameter 'TController'.
Now clearly AccountController is of type TController, otherwise it wouldn't work when not unit-testing. It inherits from my BaseController, which inherits from the regular Controller.
I get the feeling this error is a red-herring, due to the mocking - any ideas why?
Many thanks.