1

Is there an alternative way to intercept method calls in FakeItEasy if the method and arguments are already represented as an Expression<Action<T>>?

Normally I would simply use

IFoo foo = A.Fake<IFoo>();
A.CallTo(() => foo.SomeMethod("SomeString", A<Exception>.Ignored)).Invokes( ... );

But in my current situation I have a fake IFoo and an Expression<Action<IFoo>> and am trying to marry the two together.

This is actually existing Moq code that I'm re-writing for FakeIEasy but I'm not sure whether it's possible. The Moq version of this is

private void ExampleMoqMethod(Expression<Action<IFoo>> setupAction)
{
     Mock<IFoo> Mock = new Mock<IFoo>();
     Mock.Setup(setupAction).Callback( ... );
}

I tried the obvious (below) but got a "The specified object is not recognized as a fake object" error (I suspect because the fake object is not being referred to at all!)

private void ExampleFIEMethod(Expression<Action<IFoo>> callSpecification)
{
     IFoo foo = A.Fake<IFoo>();
     A.CallTo(callSpecification).Invokes( ... );
}

I would hazard a guess that this is possible by implementing IFakeObjectCallRule and using Fake.GetFakeManager(foo).AddRuleFirst(customRule) but I was wondering if there was a more straightforward way doing this?

thudbutt
  • 1,481
  • 1
  • 19
  • 32
  • I don't suppose providing an `Expression` is an option? That would work in the obvious solution that you posted. Other than that, I have nothing for you yet. – Blair Conrad Sep 30 '13 at 22:28
  • Unfortunately not. I don't have access to the Fake until the point when an `Expression>` has been created. I could pass an `Expression` if there was a way to substitute the object in the `Expression` with the Fake object... – thudbutt Oct 01 '13 at 09:05
  • `A.CallTo(callSpecification).Invokes( ... );` <- this would never work since `callSpecification` is not a fake, as the exception message says. `A.CallTo()` takes either a fake or an expression involving a fake. You may find you have to alter your approach slightly in converting to FIE. It may help us to suggest something if you post a wider example showing what this approach is buying you. – Adam Ralph Oct 12 '13 at 14:04
  • I altered my approach in the end. The Moq version had a `SynchronizedMockLogger` object which implemented standard `Debug()`, `Error()` etc. methods in a threadsafe way but also had StartCounting and StopCounting methods which took the method call to count. (i.e. the `Expression>`) In the end I used a standard `A.Fake` and added an InterceptionListener on creation as detailed here [link](http://code.google.com/p/fakeiteasy/issues/detail?id=31) and the standard `.MustHaveHappened()` method but I'm not convinced this has worked.... I will edit my post with a proper example. – thudbutt Oct 15 '13 at 16:31

0 Answers0