单元测试网址操作 [英] Unit Test Url.Action

查看:63
本文介绍了单元测试网址操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

这是UrlHelper的扩展方法:

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

}

我从以下问题获取的GetAccountController方法.我尝试根据自己的需要对设置进行一些调整,但是我不得不承认我并不完全了解所有设置.

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

我得到的错误是:

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

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

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

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

推荐答案

MockBehavior.Strict

使模拟行为像真正的模拟"一样,对于没有相应期望的任何事物都引发异常.

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

(摘自 Moq Wiki )

因此,如果将MockBehavior设置为Loose,则它:

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

这篇关于单元测试网址操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆