title: Simulate a delay in execution in Unit Test using Moq
body: <p>I'm trying to test the following:</p>
  
  <pre><code>protected IHealthStatus VerifyMessage(ISubscriber destination)
  {
      var status = new HeartBeatStatus();
  
      var task = new Task&lt;CheckResult&gt;(() =&gt;
      {
          Console.WriteLine("VerifyMessage(Start): {0} - {1}", DateTime.Now, WarningTimeout);
          Thread.Sleep(WarningTimeout - 500);
          Console.WriteLine("VerifyMessage(Success): {0}", DateTime.Now);
          if (CheckMessages(destination))
          {
              return CheckResult.Success;
          }
  
          Console.WriteLine("VerifyMessage(Pre-Warning): {0} - {1}", DateTime.Now, ErrorTimeout);
          Thread.Sleep(ErrorTimeout - 500);
          Console.WriteLine("VerifyMessage(Warning): {0}", DateTime.Now);
          if (CheckMessages(destination))
          {
              return CheckResult.Warning;
          }
  
          return CheckResult.Error;
      });
  
      task.Start();
  
      task.Wait();
      status.Status = task.Result;
  
      return status;
  }
  </code></pre>
  
  <p>with the following unit test:</p>
  
  <pre><code>public void HeartBeat_Should_ReturnWarning_When_MockReturnsWarning()
  {
      // Arrange
      var heartbeat = new SocketToSocketHeartbeat(_sourceSubscriber.Object, _destinationSubscriber.Object);
      heartbeat.SetTaskConfiguration(this.ConfigurationHB1ToHB2_ValidConfiguration());
  
      // Simulate the message being delayed to destination subscriber.
      _destinationSubscriber.Setup(foo =&gt; foo.ReceivedMessages).Returns(DelayDelivery(3000, Message_HB1ToHB2()));
  
      // Act
      var healthStatus = heartbeat.Execute();
  
      // Assert
      Assert.AreEqual(CheckResult.Warning, healthStatus.Status);
  }
  </code></pre>
  
  <p>Message_HB1ToHB2() just returns a string of characters and the "Delay Delivery" method is</p>
  
  <pre><code>private List&lt;NcsMessage&gt; DelayDelivery(int delay, string message)
  {
      var sent = DateTime.Now;
      var msg = new NcsMessage()
      {
          SourceSubscriber = "HB1",
          DestinationSubscriber = "HB2",
          SentOrReceived = sent,
          Message = message
      };
  
      var messages = new List&lt;NcsMessage&gt;();
      messages.Add(msg);
  
      Console.WriteLine("DelayDelivery: {0}", DateTime.Now);
      Thread.Sleep(delay);
      Console.WriteLine("DelayDelivery: {0}", DateTime.Now);
  
      return messages;
  }
  </code></pre>
  
  <p>I'm using Moq as the mocking framework and MSTest as the testing framework. Whenever I run the unit test, I get the following output:</p>
  
  <pre><code>DelayDelivery: 04/04/2013 15:50:33
  DelayDelivery: 04/04/2013 15:50:36
  VerifyMessage(Start): 04/04/2013 15:50:36 - 3000
  VerifyMessage(Success): 04/04/2013 15:50:38
  </code></pre>
  
  <p>Beyond the obvious "code smell" using the Thread.Sleep in the methods above, the result of the unit test is not what I'm trying to accomplish. </p>
  
  <p>Can anyone suggest a better/accurate way to use the Moq framework to simulate a delay in "delivery" of the message. I've left out some of the "glue" code and only included the relevant parts. Let me know if something I've left out that prevents you from being able to understand the question.</p>