title: Unit testing ThrowIfCancellationRequested() was called
body: <p>I am currently using Moq to help with my unit testing however I ran in to an issue that I do not know how to resolve.</p>
  
  <p>For example, say I would like to validate that <code>CancellationToken.ThrowIfCancellationRequested()</code> was called once per <code>Upload(</code> call</p>
  
  <pre><code>public UploadEngine(IUploader uploader)
  {
       _uploader = uploader;
  }
  
  public void PerformUpload(CancellationToken token)
  {
      token.ThrowIfCancellationRequested();
      _uploader.Upload(token, "Foo");
  
      token.ThrowIfCancellationRequested();
      _uploader.Upload(token, "Bar");
  }
  </code></pre>
  
  <p>If <code>token</code> was a reference type I normally would do something like</p>
  
  <pre><code>[TestMethod()]
  public void PerformUploadTest()
  {
      var uploader = new Mock&lt;IUploader&gt;();
      var token = new Mock&lt;CancellationToken&gt;();
  
      int callCount = 0;
  
      uploader.Setup(a =&gt; a.Upload(token.Object, It.IsAny&lt;string&gt;())).Callback(() =&gt; callCount++);
      token.Setup(a =&gt; a.ThrowIfCancellationRequested());
  
      var engine = new UploadEngine(uploader.Object);
      engine.PerformUpload(token.Object);
  
      token.Verify(a =&gt; a.ThrowIfCancellationRequested(), Times.Exactly(callCount));
  }
  </code></pre>
  
  <p>However from what I can tell Moq does not support value types. What would be the correct way to test this, or is there no way to do what I want through Moq without boxing the <code>CancellationToken</code> inside a container first to be passed in to <code>PerformUpload(</code>?</p>