unit-testing – 对ActionFilter进行单元测试 – 正确设置Action
发布时间:2020-12-16 06:42:49 所属栏目:asp.Net 来源:网络整理
导读:在自定义ActionFilter中,我想检查将要执行的控制器操作的属性.运行一个小型测试应用程序,以下工作在asp.net开发服务器中启动应用程序时 – public class CustomActionFilterAttribute : ActionFilterAttribute{ public override void OnActionExecuting(Acti
|
在自定义ActionFilter中,我想检查将要执行的控制器操作的属性.运行一个小型测试应用程序,以下工作在asp.net开发服务器中启动应用程序时 –
public class CustomActionFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var someAttribute = filterContext.ActionDescriptor
.GetCustomAttributes(typeof(SomeAttribute),false)
.Cast<SomeAttribute>()
.SingleOrDefault();
if (someAttribute == null)
{
throw new ArgumentException();
}
// do something here
}
public override void OnActionExecuted(ActionExecutingContext filterContext)
{
// ...
}
}
没有SomeAttribute的action方法会抛出ArgumentException,相反,SomeAttribute的action方法则不会.到现在为止还挺好. 现在我想为ActionFilter设置一些单元测试,但是如何设置OnActionExecuting方法应该在单元测试中运行的action方法?使用以下代码在将要执行的操作方法上找不到SomeAttribute.测试设置正确吗?我没有在测试中正确安排一些东西吗?为了澄清,测试并不完整,但我不确定我错过了什么,因为测试中的OnActionExecuting中的someAttribute是null [TestMethod]
public void Controller_With_SomeAttribute()
{
FakeController fakeController =
new FakeController();
ControllerContext controllerContext =
new ControllerContext(new Mock<HttpContextBase>().Object,new RouteData(),fakeController);
var actionDescriptor = new Mock<ActionDescriptor>();
actionDescriptor.SetupGet(x => x.ActionName).Returns("Action_With_SomeAttribute");
ActionExecutingContext actionExecutingContext =
new ActionExecutingContext(controllerContext,actionDescriptor.Object,new RouteValueDictionary());
CustomActionFilterAttribute customActionFilterAttribute = new CustomActionFilterAttribute ();
customActionFilterAttribute.OnActionExecuting(actionExecutingContext);
}
private class FakeController : Controller
{
[SomeAttribute]
ActionResult Action_With_SomeAttribute()
{
return View();
}
}
解决方法
由于ActionExecutingContext的ActionDescriptor属性是虚拟的,因此您可以覆盖它并提供自己的ActionDescriptor实现.
以下是两个通过当前OnActionExecuting实现验证两个分支的测试: [ExpectedException(typeof(ArgumentException))]
[TestMethod]
public void OnActionExecutingWillThrowWhenSomeAttributeIsNotPresent()
{
// Fixture setup
var ctxStub = new Mock<ActionExecutingContext>();
ctxStub.Setup(ctx => ctx.ActionDescriptor.GetCustomAttributes(typeof(SomeAttribute),false))
.Returns(new object[0]);
var sut = new CustomActionFilterAttribute();
// Exercise system
sut.OnActionExecuting(ctxStub.Object);
// Verify outcome (expected exception)
// Teardown
}
[TestMethod]
public void OnActionExecutingWillNotThrowWhenSomeAttributeIsPresent()
{
// Fixture setup
var ctxStub = new Mock<ActionExecutingContext>();
ctxStub.Setup(ctx => ctx.ActionDescriptor.GetCustomAttributes(typeof(SomeAttribute),false))
.Returns(new object[] { new SomeAttribute() });
var sut = new CustomActionFilterAttribute();
// Exercise system
sut.OnActionExecuting(ctxStub.Object);
// Verify outcome (no exception indicates success)
// Teardown
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net – Azure网站和Azure云服务之间的区别
- asp.net – 将WebGrease升级到版本1.3.0仅在生产服务器上导
- C# 文件复制帮助类
- asp.net – 如何从数据库中获取null值到gridview中的复选框
- asp.net-mvc – UIHint属性在MVC中
- asp.net-mvc-4 – 如何为Web API控制器方法指定ContentType
- 如何在ASP.NET中使用列表<>集合作为Repeater数据源与C#
- asp.net-mvc – 在AppHarbor上获取文件内容
- msbuild – 如果不指定目标框架,则不支持“发布”目标
- asp.net – 如何从SQL数据库流.flv文件
推荐文章
站长推荐
- 在ASP.Net 2.0应用程序中检查用户会话状态的状态
- asp.net – LINQ – ‘无法翻译表达式’与以前使
- asp.net-mvc-5 – MVC 5 AttributeRouting Catch
- asp.net – 命名Dom元素的Id属性的最佳实践
- asp.net – 将c#字符串数组序列化为JSON数组
- asp.net-mvc – 嵌套对象的远程ViewModel验证无效
- SimpleMembership与ASP.NET MVC 4中的自定义数据
- asp.net – Web.config文件中的appSettings和con
- asp.net-mvc – 如何在MVC3中的局部视图中渲染节
- asp.net – 使用SSL无法找到wcf服务的资源
热点阅读
