public void CanInterceptCreationOfFilters() { // Arrange this.containerMock.Setup(x => x.BuildUp(this.customActionFilter)); var controllerMock = new FilterProviderControllerMock(); var routeData = new RouteData(); // as the FilterProvider derives from FilterAttributeFilterProvider and this class has a GetFilters method on it, we have to setup // the ControllerContext and the ActionDescriptor - there is no other way of testing filter providers even using Moq. var controllerContext = new ControllerContext(this.httpContextMock.Object, routeData, controllerMock); ControllerDescriptor controllerDescriptor = new ReflectedControllerDescriptor(typeof(FilterProviderControllerMock)); var mockActionMethodInfo = controllerMock.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance).Where(x => x.Name.Equals("MockAction")).FirstOrDefault(); ActionDescriptor actionDescriptor = new ReflectedActionDescriptor(mockActionMethodInfo, "MockAction", controllerDescriptor); // Act var filters = this.filterProvider.GetFilters(controllerContext, actionDescriptor); // Assert // ensure the custom filter provider "buildup" the filter. This injects any dependencies into the filter using structuremap without having to // bind directly to structuremap within the filter using the [Inject] decorator. That would work, but this is nicer. this.containerMock.Verify(x => x.BuildUp(this.customActionFilter), Times.Once()); }
public void ShouldReturnNoResultsOnFilterNotFound() { var controllerMock = new FilterProviderControllerMock(); var controllerContext = new ControllerContext(this.httpContextMock.Object, new RouteData(), controllerMock); ControllerDescriptor controllerDescriptor = new ReflectedControllerDescriptor(typeof(FilterProviderControllerMock)); var mockActionMethodInfo = controllerMock.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance).Where(x => x.Name.Equals("Dispose")).FirstOrDefault(); ActionDescriptor actionDescriptor = new ReflectedActionDescriptor(mockActionMethodInfo, "Dispose", controllerDescriptor); var filters = this.filterProvider.GetFilters(controllerContext, actionDescriptor); Assert.IsTrue(filters.Count() == 0); }