I'm building a testing framework for our Xunit tests. I wanted to create a fake SMTP service that will mimic ours but won't actually work for the tests (For obvious reasons).
Creating a fake instance was easy and worked like a charm. I've added before the modules my the registration of the fakeItEasyObject:
builder.RegisterInstance(A.Fake<IEmailSender>());
but now I want it to ALWAYS return true on EmailSender.Send()
, and I know I can achieve it from the actual unitests with the resolved instance: A.CallTo(()=>_emailSender.Send(A<Data>.Ignored)).Returns(true);
I don't want to do it on every test and was wondering if there is a way form to do it on registration. Do you know a way to do it?
Unless you're repeating your registration all over the place, you could just
var fake = A.Fake<IEmailSender>();
A.CallTo(() => fake.Send(A<Data>.Ignored))).Returns(true);
builder.RegisterInstance(fake);
But I suspect that's not what you're asking.
If you want the default for all your fakes to return true
on Send
, you could use FakeItEasy's Implicit Creation Options. Specifically an IFakeOptionsBuilder
. Something like
public class EmailSenderFakeOptionsBuilder : FakeOptionsBuilder<IEmailSender>
{
protected override void BuildOptions(IFakeOptions<IEmailSender> options)
{
options.ConfigureFake(fake =>
{
A.CallTo(() => fake.Send(A<Data>.Ignored)).Returns(true);
});
}
}
Then if there are certain tests where you want to return false
, you could override at that point.