单元测试 URL.Action
我正在尝试对从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
MockBehavior.Strict
将(来自 de Moq wiki)
因此,如果您设置您的
MockBehavior
为Loose
,它:MockBehavior.Strict
will(From de Moq wiki)
So if you set your
MockBehavior
toLoose
, it: