Note that there are some explanatory texts on larger screens.

plurals
  1. POFunction is called when I do not expect it to with NSubstitute
    primarykey
    data
    text
    <p>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</p> <pre class="lang-c# prettyprint-override"><code>[Test] public void NSubstituteTest() { var mockedFoo = Substitute.For&lt;IFoo&gt;(); mockedFoo.GenerateString(Arg.Any&lt;string&gt;()).Returns(x =&gt; 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; } </code></pre> <p>The test passes but I get the output: 0 1</p> <p>This indicates that the call to mockedFoo.GenerateString("0"); actually results in a call to the GetValue() function.</p> <p>If I do the same with Moq:</p> <pre class="lang-c# prettyprint-override"><code>[Test] public void MoqTest() { var mockedFoo = new Mock&lt;IFoo&gt;(); mockedFoo.Setup(x =&gt; x.GenerateString(It.IsAny&lt;string&gt;())).Returns((object s) =&gt; GetValue(s)); mockedFoo.Setup(x =&gt; 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); } </code></pre> <p>Then my tests also passes but I get the result: 1</p> <p>Indicating the function was not called.</p> <p>Is this behaviour described somewhere or do I set something up in a wrong way perhaps? </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload