5

I'm currently a Moq user and I'm researching other mocking frameworks.

When unit testing I frequently call _mock.VerifyNoOtherCalls() so I can be certain there are no unexpected interactions beyond the ones that I have already verified.

I've searched the FakeItEasy docs and cannot find the equivalent option in their framework. Can anyone suggest how I might do this?

TheCrimsonSpace
  • 188
  • 1
  • 8

1 Answers1

6

Strict fakes

FakeItEasy supports strict fakes (similar to strict mocks in Moq):

var foo = A.Fake<IFoo>(x => x.Strict());

This will fail the moment an unexpected call is made.

Semi-strict fakes

It is also possible to configure all calls directly:

A.CallTo(fakeShop).Throws(new Exception());

and combine this with specifying different behaviors for successive calls, however in this case, there's no benefit to doing so over using a strict fake, as a strict fake will give better messages when unconfigured methods are called. So if you want to configure some methods to be called a limited number of times, you could

var fakeShop = A.Fake<IShop>(options => options.Strict());
A.CallTo(() => fakeShop.GetTopSellingCandy()).Returns(lollipop).Once();
A.CallTo(() => fakeShop.Address).Returns("123 Fake Street").Once();

fakeShop.GetTopSellingCandy() and fakeShop.Address can be called once, the second time it will fail.

Arbitrary checks

If you want to check if no calls are made at arbitrary points in the test:

A.CallTo(fakeShop).MustNotHaveHappened();

It might be better to filter out some of the methods that can be executed while debugging:

A.CallTo(a)
 .Where(call => call.Method.Name != "ToString")
 .MustNotHaveHappened();

You don't want a failing test because you hovered over the variable.

Blair Conrad
  • 233,004
  • 25
  • 132
  • 111
Jeroen
  • 1,212
  • 10
  • 24
  • Nice answer, Jeroen. There were a few minor problems with your untried code, so I amended (with my own untried code). – Blair Conrad Oct 26 '18 at 15:13
  • 2
    Thanks. I wasn't entirely sure my example was correct. This looks better! – Jeroen Oct 29 '18 at 08:57
  • Thanks for a very detailed answer. I had hoped that there would be a simple equivalent. I guess Moq stays on top for now as our framework of choice. – TheCrimsonSpace Nov 13 '18 at 22:32