开发者

Unit Test Url.Action

I am trying to unit test the code I got from an DotNetOpenAuth example but I have a hard time getting the UrlHelper to work in my tests.

Somewhere on the LogOn Ac开发者_如何学PythontionResult on my controller it calls the following UrlHelper. The following example is a simplified version of that ActionResult.

public ActionResult TestUrlHelper()
{
    var test = Url.ActionFull("LogOnReturnTo");
    return View();
}

My test looks something like this:

[Test]
public void TestTest()
{
    AccountController controller = GetAccountController();
    var result = controller.TestUrlHelper();
}

This is the extension method for the UrlHelper:

internal static Uri ActionFull(this UrlHelper urlHelper, string actionName)
{
    return new Uri(urlHelper.RequestContext.HttpContext.Request.Url, 
                   urlHelper.Action(actionName));

}

The GetAccountController method I got from the following question. I tried to adjust the settings a little to my needs but I have to admit I don't understand it all completely.

private static AccountController GetAccountController()
{
    var MockIFormsAuthentication = new Mock<IFormsAuthentication>();
    var MockIOpenIdRelyingParty = new Mock<IOpenIdRelyingParty>();
    var MockRealm = new Realm("http://www.google.be");

    var routes = new RouteCollection();
    MvcApplication.RegisterRoutes(routes);

    var request = new Mock<HttpRequestBase>(MockBehavior.Strict);
    request.SetupGet(x => x.ApplicationPath).Returns("/");
    request.SetupGet(x => x.Url).Returns(new Uri("http://localhost/Account/LogOnReturnTo", UriKind.Absolute));
    request.SetupGet(x => x.ServerVariables).Returns(new System.Collections.Specialized.NameValueCollection());

    var response = new Mock<HttpResponseBase>(MockBehavior.Strict);
    response.Setup(x => x.ApplyAppPathModifier("/Account/LogOnReturnTo")).Returns("http://localhost/Account/LogOnReturnTo");

    var context = new Mock<HttpContextBase>(MockBehavior.Strict);
    context.SetupGet(x => x.Request).Returns(request.Object);
    context.SetupGet(x => x.Response).Returns(response.Object);

    var Controller = new AccountController(MockIFormsAuthentication.Object, MockIOpenIdRelyingParty.Object, MockRealm);
    Controller.ControllerContext = new ControllerContext(context.Object, new RouteData(), Controller);
    Controller.Url = new UrlHelper(new RequestContext(context.Object, new RouteData()), routes);

    return Controller;
}

The error I am getting is:

HttpResponseBase.ApplyAppPathModifier("/Home/LogOnReturnTo") invocation failed with mock behavior Strict.

Any help or a push in the right direction is highly appreciated


MockBehavior.Strict will

Make mock behave like a "true Mock", raising exceptions for anything that doesn't have a corresponding expectation.

(From de Moq wiki)

So if you set your MockBehavior to Loose, it:

never throws and returns default values or empty arrays, enumerables, etc. if no expectation is set for a member

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜