Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Revised... this passes:</p> <pre><code>public class Bar { public virtual void SomeMethod(string param) { //whatever } } public interface IBarRepository { List&lt;Bar&gt; GetBarsFromStore(); } public class FooService { private readonly IBarRepository _barRepository; public FooService(IBarRepository barRepository) { _barRepository = barRepository; } public List&lt;Bar&gt; GetBars() { var bars = _barRepository.GetBarsFromStore(); foreach (var bar in bars) { bar.SomeMethod("someValue"); } return bars; } } [TestMethod] public void Verify_All_Bars_Called() { var myBarStub = new Mock&lt;Bar&gt;(); var mySecondBarStub = new Mock&lt;Bar&gt;(); var myBarList = new List&lt;Bar&gt;() { myBarStub.Object, mySecondBarStub.Object }; var myStub = new Mock&lt;IBarRepository&gt;(); myStub.Setup(repos =&gt; repos.GetBarsFromStore()).Returns(myBarList); var myService = new FooService(myStub.Object); myService.GetBars(); myBarStub.Verify(bar =&gt; bar.SomeMethod(It.IsAny&lt;string&gt;()), Times.Once()); mySecondBarStub.Verify(bar =&gt; bar.SomeMethod(It.IsAny&lt;string&gt;()), Times.Once()); } </code></pre> <p>Note the slight change to class Bar (SomeMethod() is virtual). A change, but not one involving a flag... :)</p> <p>Now, in terms of broader design, there is some mutation going on with your bar (whatever "SomeMethod()" actually does). The best thing to do would probably be to verify that this mutation happened on each Bar returned from FooService.GetBars(). That is, setup your repository stub to return some bars, and then verify that whatever mutation is performed by SomeMethod() has taken place. After all, you control the Bars that will be returned, so you can setup their pre-SomeMethod() state, and then inspect their post-SomeMethod() state.</p>
 

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