gpt4 book ai didi

unit-testing - 测试中的整个单轨操作调用

转载 作者:行者123 更新时间:2023-11-28 19:59:39 24 4
gpt4 key购买 nike

BaseControllerTest.PrepareController 足以设置 Controller 属性,例如 PropertyBag 和 Context

[TestClass]
public ProjectsControllerTest : BaseControllerTest
{
[TestMethod]
public void List()
{
// Setup
var controller = new ProjectsController();
PrepareController(controller);
controller.List();

// Asserts ...
Assert.IsInstanceOfType(typeof(IEnumerable<Project>),controller.PropertyBag["Projects"]);
}
}

但现在要运行整个管道进行集成测试,包括在操作属性中声明的过滤器?

编辑:我对 View 渲染不感兴趣,只对 Controller 逻辑和声明性过滤器感兴趣。

我喜欢将大量 View 设置逻辑移动到操作过滤器中的想法,我不确定我是否需要额外级别的集成测试,还是使用 Selenium 更好?

最佳答案

您可以获取过滤器并运行它们。

所以,假设 actionAction<YourController> , 和 controller是被测 Controller 的一个实例,

var filtersAttributes = GetFiltersFor(controller); // say by reflecting over its attributes
var filters = filtersAttributes
.OrderBy(attr => attr.ExecutionOrder)
.Select(attr => new { Attribute = attr, Instance =
(IFilter)Container.Resolve(attr.FilterType) }); // assuming you use IoC, otherwise simply new the filter type with Activator.CreateInstance or something

Action<ExecuteWhen> runFilters = when =>
{
// TODO: support IFilterAttributeAware filters
foreach (var filter in filters)
if ((filter.Attribute.When & when) != 0)
filter.Instance.Perform(when, Context, controller, controllerContext);
};

// Perform the controller action, including the before- and after-filters
runFilters(ExecuteWhen.BeforeAction);
action(controller);
runFilters(ExecuteWhen.AfterAction);

让 View 引擎发挥作用比较棘手(尽管可能),但我认为测试生成的 View 以及 Controller 逻辑涉及太多的移动并导致不合理的维护工作

关于unit-testing - 测试中的整个单轨操作调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2412306/

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