测试中的整个单轨行动调用

时间:2010-03-09 20:29:18

标签: unit-testing testing castle-monorail

BaseControllerTest.PrepareController足以用于控制器属性设置,例如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"]);
 }
}

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

编辑: 我对视图渲染,控制器逻辑以及声明性过滤器不感兴趣。

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

1 个答案:

答案 0 :(得分:2)

你可以抓住过滤器并运行它们。

所以,假设actionAction<YourController>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);

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