title: Using Moq to verify calls are made in the correct order
body: <p>I need to test the following method:</p>
  
  <pre><code>CreateOutput(IWriter writer)
  {
      writer.Write(type);
      writer.Write(id);
      writer.Write(sender);
  
      // many more Write()s...
  }
  </code></pre>
  
  <p>I've created a Moq'd <code>IWriter</code> and I want to ensure that the <code>Write()</code> methods are called in the right order.</p>
  
  <p>I have the following test code:</p>
  
  <pre><code>var mockWriter = new Mock&lt;IWriter&gt;(MockBehavior.Strict);
  var sequence = new MockSequence();
  mockWriter.InSequence(sequence).Setup(x =&gt; x.Write(expectedType));
  mockWriter.InSequence(sequence).Setup(x =&gt; x.Write(expectedId));
  mockWriter.InSequence(sequence).Setup(x =&gt; x.Write(expectedSender));
  </code></pre>
  
  <p>However, the second call to <code>Write()</code> in <code>CreateOutput()</code> (to write the <code>id</code> value) throws a <code>MockException</code> with the message "<em>IWriter.Write() invocation failed with mock behavior Strict. All invocations on the mock must have a corresponding setup.</em>".</p>
  
  <p>I'm also finding it hard to find any definitive, up-to-date documentation/examples of Moq sequences.</p>
  
  <p>Am I doing something wrong, or can I not set up a sequence using the same method?
  If not, is there an alternative I can use (preferably using Moq/NUnit)?</p>