需要一些指针.发现
this和
this,但我仍然很困惑.
我只是想模拟ActionExecutedContext,传递它,让过滤器工作一下,检查结果.
任何帮助?
过滤器来源可以找到here
(它改变了一点,但这不是现在的一点).
所以 – 我想要单元测试,RememberUrl过滤器足够聪明,可以保存会话中的当前URL.
解决方法
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的公共构造函数中注入会话包装器.
var rememberUrl = new RememberUrlAttribute(yourSessionWrapper); rememberUrl.OnActionExecuted(actionExecutedContext.Object); // Then check what is in your SessionWrapper