单元测试 URL.Action

发布于 2024-08-23 22:45:40 字数 2563 浏览 5 评论 0原文

我正在尝试对从 DotNetOpenAuth 示例获得的代码进行单元测试,但我很难让 UrlHelper 在我的测试中工作。

在我的控制器上的 LogOn ActionResult 的某处,它调用以下 UrlHelper。以下示例是该 ActionResult 的简化版本。

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

我的测试看起来像这样:

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

这是 UrlHelper 的扩展方法:

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

}

我从以下内容获得的 GetAccountController 方法 问题。我尝试根据自己的需要稍微调整设置,但我不得不承认我并不完全理解这一切。

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

我收到的错误是:

HttpResponseBase.ApplyAppPathModifier("/Home/LogOnReturnTo") 调用失败,模拟行为严格。

高度赞赏任何帮助或朝着正确方向推动

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 ActionResult 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由如风 2024-08-30 22:45:40

MockBehavior.Strict

使模拟行为像“真正的模拟”,对任何没有相应期望的内容引发异常。

(来自 de Moq wiki

因此,如果您设置您的 MockBehaviorLoose,它:

如果没有为成员设置期望,则永远不会抛出并返回默认值或空数组、可枚举值等

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文