加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读