FakeItEasy "not working" on a really easy setup
We have used FakeItEasy for a while and it basically works pretty well and reads fluently but recently someone asked me to help them work out why setting their fakes to strict was breaking their tests.
The code was along the lines of this:
but...
When set to non-strict, it was all fine. Set to strict, we got the error: FakeItEasy.ExpectationException: Call to non configured method "UpdateStatus(123,Failed)" of strict fake...
Hmm...
FetchBody worked and if I made the fake non-strict, not only did it work but the call to MustHaveHappened() also worked so the setup was definitely correct!
It was a subtle error (have you seen it yet?) but if a method does not return anything, you need to pass DoesNothing() to the setup to enact the setup in the fake, otherwise it's a bit like not finishing your sentence.
Since you cannot have a compiler error to signal that you have called a method but not used its return value, we saw nothing but the assertion error.
Thankfully, you can catch these using the very helpful FakeItEasy.Analyzer.CSharp nuget package, which uses the Visual Studio analyzer functionality to highlight issues including the one we created wrongly above!
The code was along the lines of this:
public void TestMethod()
{
A.CallTo(() => testMock.FetchBody(bodyId)).Returns(new MessageBody());
A.CallTo(() => testMock.UpdateStatus(bodyId, Status.Failed));
TestTarget.CallMethod();
A.CallTo(() => testMock.UpdateStatus(bodyId, Status.Failed)).MustHaveHappened();
}
but...
When set to non-strict, it was all fine. Set to strict, we got the error: FakeItEasy.ExpectationException: Call to non configured method "UpdateStatus(123,Failed)" of strict fake...
Hmm...
FetchBody worked and if I made the fake non-strict, not only did it work but the call to MustHaveHappened() also worked so the setup was definitely correct!
It was a subtle error (have you seen it yet?) but if a method does not return anything, you need to pass DoesNothing() to the setup to enact the setup in the fake, otherwise it's a bit like not finishing your sentence.
Since you cannot have a compiler error to signal that you have called a method but not used its return value, we saw nothing but the assertion error.
Thankfully, you can catch these using the very helpful FakeItEasy.Analyzer.CSharp nuget package, which uses the Visual Studio analyzer functionality to highlight issues including the one we created wrongly above!