Whatever
Whatever

Reputation: 77

NSubstitue Received() does not work when I run multible test in row, but if I run the test individually they work

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.

Upvotes: 0

Views: 548

Answers (1)

lionthefox
lionthefox

Reputation: 439

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.

Upvotes: 1

Related Questions