title: Proper way to verify parameters being passed to a Mock are set as expected
body: <p>Is it acceptable to do asserts in your callbacks if you later verify that the methods were called? Is this the preferred way of making sure my mock is getting the expected parameters passed to it, or should I set a local variable in my callback and do the asserts on that instance?</p>
  
  <p>I have a situation where I have some logic in a Presenter class that derives values based on inputs and passes them to a Creator class. To test the logic in the Presenter class I want to verify that the proper derived values are observed when the Creator is called. I came up with the example below that works, but I'm not sure if I like this approach:</p>
  
  <pre><code>[TestFixture]
  public class WidgetCreatorPresenterTester
  {
      [Test]
      public void Properly_Generates_DerivedName()
      {
          var widgetCreator = new Mock&lt;IWidgetCreator&gt;();
          widgetCreator.Setup(a =&gt; a.Create(It.IsAny&lt;Widget&gt;()))
                       .Callback((Widget widget) =&gt; 
                       Assert.AreEqual("Derived.Name", widget.DerivedName));
  
          var presenter = new WidgetCreatorPresenter(widgetCreator.Object);
          presenter.Save("Name");
  
          widgetCreator.Verify(a =&gt; a.Create(It.IsAny&lt;Widget&gt;()), Times.Once());
      }
  }
  </code></pre>
  
  <p>I'm concerned because without the <code>Verify</code> call at the end, there is no guarantee that the assert in the callback would be invoked. Another approach would be to set a local variable in the callback:</p>
  
  <pre><code>[Test]
  public void Properly_Generates_DerivedName()
  {
      var widgetCreator = new Mock&lt;IWidgetCreator&gt;();
      Widget localWidget = null;
      widgetCreator.Setup(a =&gt; a.Create(It.IsAny&lt;Widget&gt;()))
          .Callback((Widget widget) =&gt; localWidget = widget);
  
      var presenter = new WidgetCreatorPresenter(widgetCreator.Object);
      presenter.Save("Name");
  
      widgetCreator.Verify(a =&gt; a.Create(It.IsAny&lt;Widget&gt;()), Times.Once());
      Assert.IsNotNull(localWidget);
      Assert.AreEqual("Derived.Name", localWidget.DerivedName);
  }
  </code></pre>
  
  <p>I feel that this approach is less error prone since it is more explicit, and it's easier to see that the <code>Assert</code> statements will be called. Is one approach preferable to the other? Is there a simpler way to test the input parameter passed to a mock that I'm missing?</p>
  
  <p>In case it is helpful, here is the rest of the code for this example:</p>
  
  <pre><code>public class Widget
  {
      public string Name { get; set; }
      public string DerivedName { get; set; }
  }
  
  public class WidgetCreatorPresenter
  {
      private readonly IWidgetCreator _creator;
  
      public WidgetCreatorPresenter(IWidgetCreator creator)
      {
          _creator = creator;
      }
  
      public void Save(string name)
      {
          _creator.Create(
              new Widget { Name = name, DerivedName = GetDerivedName(name) });
      }
  
      //This is the method I want to test
      private static string GetDerivedName(string name)
      {
          return string.Format("Derived.{0}", name);
      }
  }
  
  public interface IWidgetCreator
  {
      void Create(Widget widget);
  }
  </code></pre>
  
  <p><strong>EDIT</strong><br />
  I updated the code to make the second approach I outlined in the question easier to use. I pulled creation of the expression used in Setup/Verify into a separate variable so I only have to define it once. I feel like this method is what I'm most comfortable with, it's easy to setup and fails with good error messages.</p>
  
  <pre><code>[Test]
  public void Properly_Generates_DerivedName()
  {
      var widgetCreator = new Mock&lt;IWidgetCreator&gt;();
      Widget localWidget = null;
  
      Expression&lt;Action&lt;IWidgetCreator&gt;&gt; expressionCreate = 
          (w =&gt; w.Create(It.IsAny&lt;Widget&gt;()));
      widgetCreator.Setup(expressionCreate)
          .Callback((Widget widget) =&gt; localWidget = widget);
  
      var presenter = new WidgetCreatorPresenter(widgetCreator.Object);
      presenter.Save("Name");
  
      widgetCreator.Verify(expressionCreate, Times.Once());
      Assert.IsNotNull(localWidget);
      Assert.AreEqual("Derived.Name", localWidget.DerivedName);
  }
  </code></pre>