Ser*_*gel 2 mstest castle-windsor interceptor
我有一个.net测试类.在Initialize方法中,我创建了一个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());
}
Run Code Online (Sandbox Code Playgroud)
SomeController.cs:
[HttpPost]
public JsonResult SomeMethod(SomeParameter parameter)
{
throw new Exception("Hello");
}
Run Code Online (Sandbox Code Playgroud)
SomeInterceptor.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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
DefaultInterceptorSelector.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();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1537 次 |
| 最近记录: |