Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing an IInterceptionBehavior

I'm trying to test an implementation of IInterceptionBehavior:

public class LoggingInterceptorBehavior : IInterceptionBehavior
{

    public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
    {
        var methodBase = input.MethodBase;

        /* ... */

        return getNext()(input, getNext);
    }

    public IEnumerable<Type> GetRequiredInterfaces() { /* ... */ }

    public bool WillExecute { get { return true; } }
}

How can I create a fake MethodBase?

like image 396
BanksySan Avatar asked Jul 16 '26 02:07

BanksySan


2 Answers

The trick seems to be to, rather convolutely, get the MethodHandle using reflection and then pass this into a static method off the MethodBase class:

var methodHandle = dummyClass.GetType().GetMethod("dummyMethod").MethodHandle;

var methodBase = MethodBase.GetMethodFromHandle(methodHandle);
like image 76
BanksySan Avatar answered Jul 17 '26 14:07

BanksySan


This seems to be wrong to me. What you seem to need here, is a rather integration test, where you test the whole thing by using it and then checking if it has logged something in the log (or whatever the purpose of the interceptor is). You could create a container, register a dummy test class, execute a method on it, have the method intercepted by the interceptor, etc. you get the gesture.

like image 32
Hadi Eskandari Avatar answered Jul 17 '26 15:07

Hadi Eskandari