Note that there are some explanatory texts on larger screens.

plurals
  1. POFaking an enumerator in FakeItEasy
    primarykey
    data
    text
    <p>How can I create a fake with <a href="http://code.google.com/p/fakeiteasy/" rel="nofollow">FakeItEasy</a> that allows different return values on successive calls. This is one example of what I would like to be able to do:</p> <pre><code>var enumerator = A.Fake&lt;IDictionaryEnumerator&gt;(); A.CallTo(() =&gt; enumerator.MoveNext()).Returns(true); //Expected value for first call A.CallTo(() =&gt; enumerator.Key).Returns("key1"); A.CallTo(() =&gt; enumerator.Value).Returns("value1"); A.CallTo(() =&gt; enumerator.MoveNext()).Returns(false); //Expected value for second call Assert.IsTrue(enumerator.MoveNext()); //Fails Assert.IsFalse(enumerator.MoveNext()); </code></pre> <p>The assertion will fail since it the last set up of MoveNext will overwrite the first one.</p> <p>Is it possible to do what I want in <a href="http://code.google.com/p/fakeiteasy/" rel="nofollow">FakeItEasy</a>?</p> <p>.</p> <p><strong>[Edit]</strong><br> Clarified the original question's example and provided a working example below.</p> <p>Based on Patrik's answer this code shows how you can set up the fake. The trick is to reverse all setups and use <code>Once()</code>.</p> <pre><code>var enumerator = A.Fake&lt;IDictionaryEnumerator&gt;(); A.CallTo(() =&gt; enumerator.MoveNext()).Returns(false).Once(); A.CallTo(() =&gt; enumerator.MoveNext()).Returns(true).NumberOfTimes(2); A.CallTo(() =&gt; enumerator.Key).Returns("key2").Once(); A.CallTo(() =&gt; enumerator.Value).Returns("value2").Once(); A.CallTo(() =&gt; enumerator.Key).Returns("key1").Once(); A.CallTo(() =&gt; enumerator.Value).Returns("value1").Once(); while(enumerator.MoveNext()) { Debug.WriteLine(enumerator.Key + ": "+ enumerator.Value); } </code></pre> <p>This will print:</p> <pre><code>key1: value1 key2: value2 </code></pre>
    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