title: How do I write a unit test for this method that is called inside a loop?
body: <p>I'm trying to test a method using xUnit and Moq.
  The <code>public void DoSomeWork_Verify_GetJsonDataString_is_called()</code> test is failing. Please can someone let me know what I am missing in the test/mocking. The <code>GetJsonDataString</code> is an API call that returns a JSON string.</p>
  <pre><code>public class A 
  {
      private readonly IData _data;
      private readonly IApiCall _apiCall;
         
      public A (IData data, IApiCall apiCall)
      {
          _data = data;
          _apiCall = apiCall;
      }
         
      public void DoSomeWork
      {
          var apps = _data.GetListOfApps(); // test on this method call is passing
          foreach(var app in apps)
          {
              if(string.IsNullOrEmpty(app.AppId)) continue;
              string jsonString = _apiCall.GetJsonDataString(&quot;ApiEndpoint&quot; + app.AppId); 
              // how do I test this method call? Note: I'm not using async, this is just a console app.
          }       
      }
  }
  
  //MyTest
  public class TestA
  {
      private readonly Mock&lt;IData&gt; _data;
      private readonly Mock&lt;IApiCall&gt; _apicall;
  
      public TestA()
      {
          _data = new Mock&lt;IData&gt;;
          _apiCall = new Mock&lt;IApiCall&gt;;
      }
  
      public void DoSomeWork_Verify_GetListOfApps_is_called() 
      {
          _data.Setup(x =&gt; x.GetListOfApps());
          var sut = new A(_data.Object, _apiCall.Object);
          sut.DoSomeWork();
          _data.Verify(x =&gt; x.GetListOfApps(), Times.AtLeastOnce);  // this passes
      }
  
      public void DoSomeWork_Verify_GetJsonDataString_is_called()
      {
          _apicall.Setup(x =&gt; x.GetJsonDataString(It.IsAny&lt;string&gt;()));
          var sut = new A(_data.Object, _apiCall.Object);
          sut.DoSomeWork();
          _apicall.Verify(x =&gt; x.GetJsonDataString(It.IsAny&lt;string&gt;()), Times.AtLeastOnce);  // Failing
      }  
  } 
  </code></pre>