Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FakeItEasy - Is it possible to intercept a method and replace it with my own implementation?

I have the following interface :

public interface IOuputDestination
{
    void Write(String s);
}

In my unit test, I mock it like so :

var outputDestination = A.Fake<IOutputDestination>();

What I want to do, is intercept the Write method so that it uses my custom implementation that I define in my test. Something like this :

String output = "";
A.CallTo(() => outputDestination.Write(A<String>.Ignored)).Action(s => output += s);
              //Is something like this even possible ? ----^

Here the Action method does not exist, but what I would like to happen is that any parameter passed into outputDestination.Write be redirected to my custom action. Is this possible using FakeItEasy? If not, is there another mocking framework that allows this kind of behavior?

like image 488
marco-fiset Avatar asked Oct 19 '12 18:10

marco-fiset


1 Answers

You can get that behavior with Invokes:

String output = "";
A.CallTo(() => outputDestination.Write(A<String>.Ignored))
    .Invokes((string s) => output += s);
like image 150
k.m Avatar answered Nov 15 '22 08:11

k.m