Created
March 18, 2014 17:50
-
-
Save orient-man/9625459 to your computer and use it in GitHub Desktop.
Unit test describing problems with interception of inherited methods...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Ninject; | |
using Ninject.Extensions.Interception; | |
using Ninject.Extensions.Interception.Attributes; | |
using Ninject.Extensions.Interception.Infrastructure.Language; | |
using Ninject.Extensions.Interception.Request; | |
using NUnit.Framework; | |
namespace InterceptionGotchas | |
{ | |
[TestFixture] | |
public class InterceptionTests | |
{ | |
public static bool InterceptionFlag = false; | |
private readonly IKernel kernel = new StandardKernel(); | |
[TestFixtureSetUp] | |
public void SetUpAllTests() | |
{ | |
kernel.Bind<IFoo>().To<Foo>(); | |
kernel.Bind<IBar>().To<Bar>().Intercept().With<MethodInterceptor>(); | |
kernel.Bind<IBaz>().To<Baz>(); | |
kernel | |
.Intercept(ctx => typeof(IBaz).IsAssignableFrom(ctx.Plan.Type)) | |
.With<MethodInterceptor>(); | |
} | |
[SetUp] | |
public void SetUpEachTest() | |
{ | |
InterceptionFlag = false; | |
} | |
[Test] | |
public void InterceptionViaAttributeWorks() | |
{ | |
kernel.Get<IFoo>().DoSomething(); | |
Assert.IsTrue(InterceptionFlag); | |
} | |
[Test] | |
public void InterceptionViaAttributeDoesNotInterceptInheritedMethods() | |
{ | |
kernel.Get<IFoo>().GetHashCode(); | |
Assert.IsFalse(InterceptionFlag); | |
} | |
[Test] | |
public void InterceptionViaBindingExtensionWorks() | |
{ | |
kernel.Get<IBar>().DoSomething(); | |
Assert.IsTrue(InterceptionFlag); | |
} | |
[Test] | |
public void InterceptionViaBindingExtension_Does_InterceptInheritedMethods() | |
{ | |
kernel.Get<IBar>().GetHashCode(); | |
// This is FALSE with Ninject.Extensions.Interception.DynamicProxy 3.0.0.8 | |
Assert.IsTrue(InterceptionFlag); | |
} | |
[Test] | |
public void InterceptionViaKernelExtensionWorks() | |
{ | |
kernel.Get<IBaz>().DoSomething(); | |
Assert.IsTrue(InterceptionFlag); | |
} | |
[Test] | |
public void InterceptionViaKernelExtension_Does_InterceptInheritedMethods() | |
{ | |
kernel.Get<IBaz>().GetHashCode(); | |
// This is FALSE with Ninject.Extensions.Interception.DynamicProxy 3.0.0.8 | |
Assert.IsTrue(InterceptionFlag); | |
} | |
public interface IFoo | |
{ | |
void DoSomething(); | |
} | |
[ChangeFlag] | |
public class Foo : IFoo | |
{ | |
public virtual void DoSomething() | |
{ | |
} | |
} | |
public interface IBar | |
{ | |
void DoSomething(); | |
} | |
public class Bar : IBar | |
{ | |
public virtual void DoSomething() | |
{ | |
} | |
} | |
public interface IBaz | |
{ | |
void DoSomething(); | |
} | |
public class Baz : IBaz | |
{ | |
public virtual void DoSomething() | |
{ | |
} | |
} | |
public class ChangeFlagAttribute : InterceptAttribute | |
{ | |
public override IInterceptor CreateInterceptor(IProxyRequest request) | |
{ | |
return new MethodInterceptor(); | |
} | |
} | |
public class MethodInterceptor : IInterceptor | |
{ | |
public void Intercept(IInvocation invocation) | |
{ | |
InterceptionFlag = true; | |
invocation.Proceed(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment