I am getting a behaviour I didnt expect from NSubstitute when setting up my mocks to call a function. A simplified version of the behavuiour is
[Test]
public void NSubstituteTest()
{
var mockedFoo = Substitute.For<IFoo>();
mockedFoo.GenerateString(Arg.Any<string>()).Returns(x => GetValue(x.Args()[0]));
mockedFoo.GenerateString("0").Returns("hi");
string result1 = mockedFoo.GenerateString("0");
string result2 = mockedFoo.GenerateString("1");
Assert.AreEqual("hi", result1);
Assert.AreEqual("1", result2);
}
private string GetValue(object val)
{
string returnValue = val != null ? val.ToString() : "I am null";
System.Diagnostics.Trace.WriteLine(returnValue);
return returnValue;
}
The test passes but I get the output: 0 1
This indicates that the call to mockedFoo.GenerateString("0"); actually results in a call to the GetValue() function.
If I do the same with Moq:
[Test]
public void MoqTest()
{
var mockedFoo = new Mock<IFoo>();
mockedFoo.Setup(x => x.GenerateString(It.IsAny<string>())).Returns((object s) => GetValue(s));
mockedFoo.Setup(x => x.GenerateString("0")).Returns("hi");
string result1 = mockedFoo.Object.GenerateString("0");
string result2 = mockedFoo.Object.GenerateString("1");
Assert.AreEqual("hi", result1);
Assert.AreEqual("1", result2);
}
Then my tests also passes but I get the result: 1
Indicating the function was not called.
Is this behaviour described somewhere or do I set something up in a wrong way perhaps?