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

StackOverflow https://stackoverflow.com/questions/12980631

سؤال

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?

هل كانت مفيدة؟

المحلول

You can get that behavior with Invokes:

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

نصائح أخرى

You don't really show how you're using outputDestination once you've created it. Would you be injecting it into some method under test? If so, then What if, instead of creating a fake, you create a specially crafted class that implements IOutputDestination and which contains your required implementation of Write()?

I guess this would depend what (if any) other bahaviours you need to verify on your fake. Its hard to know without more context.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top