我有一个.NET测试班。在初始化方法中,我创建一个windsor容器并进行一些注册。在实际的测试方法中,我在控制器类上调用一个方法,但拦截器不工作,直接调用该方法。可能的原因是什么?
以下是所有相关代码:
Test.cs:
private SomeController _someController;
[TestInitialize]
public void Initialize()
{
Container.Register(Component.For<SomeInterceptor>());
Container.Register(
Component.For<SomeController>()
.ImplementedBy<SomeController>()
.Interceptors(InterceptorReference.ForType<SomeInterceptor>())
.SelectedWith(new DefaultInterceptorSelector())
.Anywhere);
_someController = Container.Resolve<SomeController>();
}
[TestMethod]
public void Should_Do_Something()
{
_someController.SomeMethod(new SomeParameter());
}
某些控制器.cs:
[HttpPost]
public JsonResult SomeMethod(SomeParameter parameter)
{
throw new Exception("Hello");
}
一些拦截器.cs:
public class SomeInterceptor : IInterceptor
{
public void Intercept(IInvocation invocation)
{
// This does not gets called in test but gets called in production
try
{
invocation.Proceed();
}
catch
{
invocation.ReturnValue = new SomeClass();
}
}
}
默认拦截器elector.cs:
public class DefaultInterceptorSelector : IInterceptorSelector
{
public IInterceptor[] SelectInterceptors(Type type, MethodInfo method, IInterceptor[] interceptors)
{
return
method.ReturnType == typeof(JsonResult)
? interceptors
: interceptors.Where(x => !(x is SomeInterceptor)).ToArray();
}
}