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

I have the following interface:

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

In my unit test, I do it like this:

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

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

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

The Action method does not exist here, but I would like for any parameter passed to outputDestination.Write to be redirected to my custom action. Is this possible with FakeItEasy? If not, is there another mocking structure that allows this behavior?

+6
source share
2 answers

You can get this behavior with Invokes :

 String output = ""; A.CallTo(() => outputDestination.Write(A<String>.Ignored)) .Invokes((string s) => output += s); 
+7
source

In fact, you are not showing how you use outputDestination after creating it. Will you introduce it into any testing method? If so, what if, instead of creating a fake, you create a specially created class that implements IOutputDestination and which contains the required Write() implementation?

I assume that this will depend on what (if any) other Bahaviurs you need to check for your fake. It is difficult to recognize without additional context.

0
source

Source: https://habr.com/ru/post/928143/


All Articles