如何对测试的ASP.NET MVC行动过滤器? [英] How-to test action filters in ASP.NET MVC?

查看:118
本文介绍了如何对测试的ASP.NET MVC行动过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要对此的指针。发现和<一个href=\"http://blog.geniar.com/index.php/2009/05/aspnet-mvc-testing-a-custom-authorize-filters/\">this,但我至今仍一片迷茫。

Need some pointers for this. Found this and this, but I'm still kind a confused.

我只想嘲笑ActionExecutedContext,通过它,让过滤工作了一下,检查结果。

I just want to mock ActionExecutedContext, pass it, let filter to work a bit and check result.

任何帮助吗?

过滤器的源代码,你可以找到<一个href=\"http://stackoverflow.com/questions/1052394/asp-net-mvc-how-to-implement-link-which-returns-to-$p$pvious-page\">here

(它改了一下,但是这不是目前的点)。

Source of filter you can find here
(it's changed a bit, but that's not a point at the moment).

所以 - 我想单元测试,即RememberUrl过滤器是足够聪明,保存当前URL的会话。

So - i want unit test, that RememberUrl filter is smart enough to save current URL in session.

推荐答案

1)嘲讽Request.Url在ActionExecutedContext:

1) Mocking Request.Url in ActionExecutedContext:

var request = new Mock<HttpRequestBase>();
request.SetupGet(r => r.HttpMethod).Returns("GET");
request.SetupGet(r => r.Url).Returns(new Uri("http://somesite/action"));

var httpContext = new Mock<HttpContextBase>();
httpContext.SetupGet(c => c.Request).Returns(request.Object);

var actionExecutedContext = new Mock<ActionExecutedContext>();
actionExecutedContext.SetupGet(c => c.HttpContext).Returns(httpContext.Object);

2)假设你在你的RememberUrlAttribute的公共构造注入会话包装。

2) Suppose you are injecting session wrapper in your RememberUrlAttribute's public constructor.

var rememberUrl = new RememberUrlAttribute(yourSessionWrapper);

rememberUrl.OnActionExecuted(actionExecutedContext.Object);

// Then check what is in your SessionWrapper

这篇关于如何对测试的ASP.NET MVC行动过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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