'NSubsitute - Receive() fails when more than one method of the same service is invoked
I'm switching from Moq to NSubstitute and have a weird problem - I have a simple Interface which I subistitute:
public interface ISampleService
{
string GenerateCode(SampleData data);
BitmapImage GetCode(string code);
}
Above interface is called in this method:
public void GenerateCode()
{
SampleCode = _sampleService.GenerateCode(Label);
Code = _sampleService.GetCode(SampleCode);
}
And try to verify that both method were called but below test fails:
[TestMethod]
public void SomethingChanged_WhenIsChanged_SampleCodeShouldBeGenerated()
{
Sut.GenerateCode();
_barcodeService.Received().GenerateCode(Arg.Any<SampleData>());
_barcodeService.Received().GetCode(Arg.Any<string>());
}
Only last substituted method was verified as invoked _barcodeService.Received().GetCode(Arg.Any<string>()); but _barcodeService.Received().GenerateCode(Arg.Any<SampleData>()); throws ReceivedCallsException
When I change the order of invoking methods in Sut.GenerateCode() method - situation is opposite: _barcodeService.Received().GenerateCode(Arg.Any<SampleData>()); is verified as infoked but _barcodeService.Received().GetCode(Arg.Any<string>()); throws exception.
Interesting thing is that when run this test:
[TestMethod]
public void SomethingChanged_WhenIsChanged_SampleCodeShouldBeGenerated()
{
_sampleService.GenerateCode(Arg.Any<SampleData>()).Returns("111");
Sut.GenerateCode();
_barcodeService.Received().GenerateCode(Arg.Any<SampleData>());
}
It throws ReceivedCallsException but Sut.SampleCode = "111" -> It seems for me as only last call of substituted service is notified as invoked...
I cannot also invoke Received.InOrder method because it throws CallSequenceNotFoundException
Could you please let me now what I did wrong? Thanks in advance for your help!
PS. When I change testing method like below (double the line: SampleCode = _sampleService.GenerateCode(Label); all tests are green
public void GenerateCode()
{
SampleCode = _sampleService.GenerateCode(Label);
SampleCode = _sampleService.GenerateCode(Label);
Code = _sampleService.GetCode(SampleCode);
}
Did anyone has similar situation?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
