The whole monorail call challenge in tests

BaseControllerTest.PrepareController is enough to configure controller properties, such as PropertyBag and 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"]);
 }
}

But now to run the entire pipeline for integration testing, including filters declared in action attributes?

EDIT: I'm not interested in viewing the render, just the controller logic along with declarative filters.

I like the idea of ​​transferring a significant amount of view setting logic into action filters, and I'm not sure if I need an additional level of integration tests, or is it better to do this with Selenium?

+3
source share
1 answer

You can capture filters and run them.

, action - Action<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);

( ), ,

+2

Source: https://habr.com/ru/post/1736192/


All Articles