title: How to test Automatonymous state machines?
body: <p>I'm trying to write tests for an Automatonymous state machine, but I'm having a fair bit of trouble getting it right, and I've found very little documentation.</p>
  
  <p>Here's what I have at the moment for one test:</p>
  
  <pre><code>[TestFixture]
  public class MyProcessStateMachineTests
  {
      InMemoryTestHarness _Harness;
      MyProcessStateMachine _Machine;
      StateMachineSagaTestHarness&lt;MyProcess, MyProcessStateMachine&gt; _Saga;
  
      [OneTimeSetUp]
      public void ConfigureMessages()
      {
          MessageCorrelation.UseCorrelationId&lt;RequestMyDetails&gt;(x =&gt; x.CorrelationId);
          MessageCorrelation.UseCorrelationId&lt;FileAttached&gt;(x =&gt; x.CorrelationId);
          MessageCorrelation.UseCorrelationId&lt;PDFGenerated&gt;(x =&gt; x.CorrelationId);
          MessageCorrelation.UseCorrelationId&lt;CustomerAttachFile&gt;(x =&gt; x.CorrelationId);
          MessageCorrelation.UseCorrelationId&lt;AddCustomerNote&gt;(x =&gt; x.CorrelationId);
          MessageCorrelation.UseCorrelationId&lt;EmailPublished&gt;(x =&gt; x.CorrelationId);
      }
  
  
      [SetUp]
      public void InitializeTestHarness()
      {
          _Harness = new InMemoryTestHarness();
          _Machine = new MyProcessStateMachine( /* snip */ );
          _Saga = _Harness.StateMachineSaga&lt;MyProcess, MyProcessStateMachine&gt;(_Machine);
  
          _Harness.Start().Wait();
      }
  
      [TearDown]
      public void StopTestHarness()
      {
          _Harness.Stop();
      }
  
  
      [Test]
      public async Task ShouldAttachToCustomer()
      {
          var sagaId = Guid.NewGuid();
          var custId = Guid.NewGuid();
          var fileAttached = BuildFileAttachedMessage(sagaId);
  
          await _Harness.InputQueueSendEndpoint.Send(BuildStartMessage(sagaId));
          await _Harness.InputQueueSendEndpoint.Send(BuildDetailsReceivedMessage(sagaId));
          await _Harness.InputQueueSendEndpoint.Send(BuildPdfGeneratedMessage(sagaId));
          await _Harness.InputQueueSendEndpoint.Send(fileAttached);
  
          // Next line is based on [the answer here][1]
          // Once the above messages are all consumed and processed,
          // the state machine should be in AwaitingEmail state
          await _Saga.Match(x =&gt;
              x.CorrelationId == sagaId
                  &amp;&amp; x.CurrentState == _Machine.AwaitingEmail.Name,
              new TimeSpan(0, 0, 30));
  
          // Grab the instance and Assert stuff...
      }
  
      // Snip...
  }
  </code></pre>
  
  <p>Given that the _Saga.Match call finds a match, I would expect that all messages have been processed and I should be able to grab my state machine instance and published events and check their values - but that isn't the case.  When I run the tests in the fixture, sometimes the instance I get has consumed and published the expected messages; sometimes it's not quite there yet.</p>
  
  <p>I've tried grabbing my instance using:</p>
  
  <pre><code>var inst = _Saga.Sagas.FirstOrDefault(x =&gt; x.Saga.CorrelationId == sagaId);
  </code></pre>
  
  <p>or grabbing published events with:</p>
  
  <pre><code>var test = _Harness.Published
      .FirstOrDefault(x =&gt; x.MessageType == typeof(IAttachFile) &amp;&amp; x.Context.CorrelationId == sagaId);
  </code></pre>
  
  <p>but it doesn't matter that the call to Match succeeded, the state machine instance (and published events) aren't always present.  </p>
  
  <p>I'm assuming that the async proccesses from Automatonymous, MassTransit, or test harness is causing the inconsistency.  Any help?</p>
  
  <p>Testing with MassTransit, MassTransit.Automatonymous and MassTransit.TestFramework 5.1.2.1528, Automatonymous 4.1.1.102, </p>
  
  <p>EDIT: </p>
  
  <p>Further review, I've found that when I have a problem, the call to <code>Match( ... )</code> 
  didn't succeed - it timed out.  (I had been incorrectly assuming that a timeout would throw an exception.)</p>