title: How can I verify a mocked object is removed from a mocked list?
body: <p>I am trying to write a unit test for this method:</p>
  
  <pre><code>public void AddItem( Cart cart, Item item )
  {
      var duplicates = cart.Items.OfType&lt;Item&gt;()
                           .Where( i =&gt; i.Key == item.Key )
                           .ToList();
  
      foreach ( var duplicate in duplicates )
      {
          cart.Items.Remove( duplicate );
      }
  
      cart.Items.Add( item );
  }
  </code></pre>
  
  <p>I want to verify that duplicate items (items with the same key) are removed from the list before the new one is added. This test fails:</p>
  
  <pre><code>[TestMethod]
  public void AddItemRemovesDuplicateItemsFirst()
  {
      const int itemKey = 123;
  
      var cart = new Mock&lt;Cart&gt;();
  
      var duplicate = new Mock&lt;Item&gt;();
      duplicate.SetupGet( m =&gt; m.Key ).Returns( itemKey );
  
      cart.SetupGet( m =&gt; m.Items ).Returns( new List&lt;Item&gt;
          {
              duplicate.Object
          } );
  
      var addItem = new Mock&lt;Item&gt;();
      addItem.SetupGet( m =&gt; m.Key ).Returns( itemKey );
  
      var task = GetTask();
      task.AddItem( cart.Object, addItem.Object );
  
      Assert.AreEqual( 1, cart.Object.Items.Count );
  }
  </code></pre>
  
  <p>It fails because <code>Count</code> is 2. On the other hand, this test passes:</p>
  
  <pre><code>[TestMethod]
  public void AddItemRemovesDuplicateItemsFirst()
  {
      const int itemKey = 123;
  
      var cart = new Mock&lt;Cart&gt;();
  
      cart.SetupGet( m =&gt; m.Items ).Returns( new List&lt;Item&gt;
          {
              new Item
                  {
                      Key = itemKey
                  }
          } );
  
      var addItem = new Mock&lt;Item&gt;();
      addItem.SetupGet( m =&gt; m.Key ).Returns( itemKey );
  
      var task = GetTask();
      task.AddItem( cart.Object, addItem.Object );
  
      Assert.AreEqual( 1, cart.Object.Items.Count );
  }
  </code></pre>
  
  <p>The only difference is the second one doesn't use a <code>Mock</code> object for the list item.</p>
  
  <p>I'm trying to avoid actually creating the objects as in the second approach. How can I get the first to pass?</p>