0

I have an interface, IFoo, with some number of methods. I also have an implementation of IFoo which decorates any IFoo, so something like:

class FooDecroator : IFoo {
  FooDecorator(IFoo decoratedFoo) {
    ...
  }

  public void SomeFooMethod() {
    decoraterdFoo.SomeMethod();
  }
}

I want to use FakeItEasy to mock the constructor arg so I can assert everything is delegated properly. I could just manually write out every call but I have some logic where certain methods are not delegated.

I know I can iterate over FooDecorator using reflection, get the public methods, get args to each method, and invoke the method using reflection.

What I'm not sure about is how to then assert the mock methods were called with the same parameters using reflection (or some other means). I'm trying to avoid writing code for each method call.

Any thoughts are appreciated.

Mike
  • 150
  • 1
  • 8

1 Answers1

0

Probably the best bet would be to use the CallTo variant for specifying any call to an object: Specifying a call to any method or property (the page title mentions specifying a call to configure, but A.CallTo is also used for verification). There's a Where overload that takes a predicate that tests an IFakeObjectCall, which gives access to the called method and passed parameters:

A.CallTo(wrappedFake)
    .Where(call => CallMatchesMethodAndArguments(call, method, outerArguments)
    .MustHaveHappened();

…

bool CallMatchesMethodAndArguments(
    IFakeObjectCall call,
    MethodInfo method,
    object[] outerArguments)
{
   // check call.Method matches method
   // check each of call.Arguments matches outerArguments
}
Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
  • 1
    I missed this in the doc, thank you. I will be checking it out tomorrow and will let you know if it works. – Mike Nov 07 '21 at 19:42