gpt4 book ai didi

unit-testing - 对ActionFilter进行单元测试-正确设置ActionExecutingContext

转载 作者:行者123 更新时间:2023-12-04 04:48:47 25 4
gpt4 key购买 nike

在自定义ActionFilter中,我要检查将要执行的 Controller 操作的属性。在一个小型测试应用程序中运行时,在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的操作方法将抛出 ArgumentException,相反,具有 SomeAttribute的操作方法则不会。到现在为止还挺好。

现在,我想为ActionFilter设置一些单元测试,但是如何设置在单元测试中应在其上运行 OnActionExecuting方法的action方法呢?使用以下代码在要执行的操作方法上找不到 SomeAttribute。测试设置正确吗?我在考试中安排的东西不正确吗?为了澄清,测试尚未完成,但是我不确定我错过了什么,因此测试中 someAttribute中的 OnActionExecutingnull
    [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属性是虚拟的,因此您可以仅提供override并提供自己的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
}

关于unit-testing - 对ActionFilter进行单元测试-正确设置ActionExecutingContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2367462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com