0

I have multiple unit tests, where I do check if a method was called.

I use the NSubstitute mocking libraries to check rather a method was called with the help of the "Received()" method, just like that:

MessageHandling.Received().Submit(Messages.DATA_EXPORT_SUCCESS);

The tests are working fine when I run them individually, but when run them all, some of them fail for no apparent reason. When I debug the code, I see that the method which should get called was called, but the Received() method from NSubstitute says there was no call at all.

I do also call the ClearReceivedCalls() in my TearDown methode

MessageHandling.ClearReceivedCalls();

But this does not seem to help.

Is there something else I should take care of, when using the Received() method?

My test functions are a bit more complicated then just the to check for a call, but that is the only reason why my tests fail.

Whatever
  • 77
  • 1
  • 12

1 Answers1

1

I assume MessageHandling is initialized as a single instance property, that is used in every test? Try to make your test class stateless, by initializing a new mocked instance in every test.

lionthefox
  • 369
  • 1
  • 2
  • 16