Unit Test Url.Action

I am trying to use the unit test code that I got from the DotNetOpenAuth example, but I find it hard to get UrlHelper to work in my tests.

Somewhere in the LogOn ActionResult on my controller, it calls the following UrlHelper. The following example is a simplified version of 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 UrlHelper:

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

}

The GetAccountController method obtained from the following question . I tried to tweak the settings a bit for my needs, but I have to admit that I don't completely understand.

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:

Error HttpResponseBase.ApplyAppPathModifier ("/ Home / LogOnReturnTo") with false behavior Strictly.

Any help or push in the right direction is much appreciated.

+3
1

MockBehavior.Strict

mock " ", -, .

(From de Moq wiki)

, MockBehavior Loose, :

, ..,

+2

Source: https://habr.com/ru/post/1735599/


All Articles