title: Mocking while loop
body: <p>I need to mock a while loop to run just once however my setup makes it run infinite times as I think it returns true always.</p>
  
  <p>My Set up:</p>
  
  <pre><code>var loginName = "12345";
  
  cacheRepository.Setup(m =&gt; m.AddString(string.Format("{0}_{1}", Resources.ResetCodeCacheKey, randomCode), loginName)).Returns(true);
  </code></pre>
  
  <p>While loop in method:</p>
  
  <pre><code>while (_cacheRepository.AddString(string.Format("{0}_{1}", Resources.ResetCodeCacheKey, code), userLoginName))
  {
      //.........
  }
  </code></pre>
  
  <p>Add String implementation:</p>
  
  <pre><code>public virtual bool AddString(string key, string value)
  {
      if (!ExistsKey(key))
      {
          Cache.AddString(key, value);
          return true;
      }
      return false;
  }
  </code></pre>
  
  <p>How can I set up my method to return true just once? A code snippet will be helpful.Thanks for looking into this.</p>