title: Struggling with Moq: The following setups were not matched
body: <p>I'm using Moq for the first time, and I'm struggling with getting the tests to run properly.</p>
  
  <p>I'm trying to moq the <code>Save()</code> method of my service layer.</p>
  
  <pre><code>public void Save(UserViewModel viewModel)
  {
      // todo: this still doesn't address updating a password. The UserViewModel doesn't contain any Password data.
      if (viewModel.Id != Guid.Empty)
      {
          // The UserId is not empty, we're either updating an existing user
          // or we're inserting a new user via sync
          var user = _userRepository.GetById(viewModel.Id);
          if (user != null)
          {
              // Looks like we're updating a user because they're already in the database.
              _userRepository.Update(_userViewModelToModelMapper.BuildFrom(viewModel));
              return;
          }
      }
  
      // The user is being created, either via a Sync (Guid Exists), or via an Insert (Guid doesn't Exist)
      _userRepository.Create(_userViewModelToModelMapper.BuildFrom(viewModel));
  }
  </code></pre>
  
  <p>I've got Three tests, of which, <strong><em>I'm not sure any of them are right</em></strong>. The first two are passing, but the third one fails with</p>
  
  <blockquote>
    <p>Moq.MockVerificationException : The following setups were not matched:<br>
    IUserRepository r => r.Update(It.Is(um => um.Equals()))</p>
  </blockquote>
  
  <p>Here are the tests.</p>
  
  <pre><code>// PASSES but could be suspect
  [Test]
  public void ShouldSaveANewUserFromExistingId()
  {
      // emulating a "sync"
  
      // Setup
      var userId = new Guid("81C7FE19-2DB5-4083-BD6A-5433687561F7");
      var userModel = new UserModel();
      var userViewModel = new UserViewModel {Id = userId};
      var userRepository = new Mock&lt;IUserRepository&gt;();
      var viewModelToModelMapper = new Mock&lt;IAutoMapper&lt;UserViewModel, UserModel&gt;&gt;();
      var modelToViewModelMapper = new Mock&lt;IAutoMapper&lt;UserModel, UserViewModel&gt;&gt;();
  
      // Setup the Mock UserRepository
      userRepository.Setup(r =&gt; r.Create(It.Is&lt;UserModel&gt;(um =&gt; um.Equals(userModel))));
  
      viewModelToModelMapper.Setup(vmm =&gt; vmm.BuildFrom(It.Is&lt;UserViewModel&gt;(u =&gt; u.Equals(userViewModel))))
             .Returns(userModel);
  
      var userService = new UserService(userRepository.Object, viewModelToModelMapper.Object, modelToViewModelMapper.Object);
  
      // Execute
      userService.Save(userViewModel);
  
      // Assert
      userRepository.VerifyAll();
      viewModelToModelMapper.VerifyAll();
  }
  </code></pre>
  
  
  
  <pre><code>// PASSES but could be suspect.
  [Test]
  public void ShouldSaveANewUser()
  {
      // emulating a standard create
  
      // Setup
      var userId = Guid.Empty;
      var userModel = new UserModel();
      var userViewModel = new UserViewModel { Id = userId };
      var userRepository = new Mock&lt;IUserRepository&gt;();
      var viewModelToModelMapper = new Mock&lt;IAutoMapper&lt;UserViewModel, UserModel&gt;&gt;();
      var modelToViewModelMapper = new Mock&lt;IAutoMapper&lt;UserModel, UserViewModel&gt;&gt;();
  
      // Setup the Mock UserRepository
      userRepository.Setup(r =&gt; r.Create(It.Is&lt;UserModel&gt;(um =&gt; um.Equals(userModel))));
  
      viewModelToModelMapper.Setup(vmm =&gt; vmm.BuildFrom(It.Is&lt;UserViewModel&gt;(u =&gt; u.Equals(userViewModel))))
             .Returns(userModel);
  
      var userService = new UserService(userRepository.Object, viewModelToModelMapper.Object, modelToViewModelMapper.Object);
  
      // Execute
      userService.Save(userViewModel);
  
      // Assert
      userRepository.VerifyAll();
      viewModelToModelMapper.VerifyAll();
  
  }
  </code></pre>
  
  
  
  <pre><code>// FAILS MISERABLY
  [Test]
  public void ShouldSaveAnExistingUser()
  {
      // emulating an "Update"
  
      // Setup
      var userId = new Guid("0A88AEC2-9F8D-44DE-BD01-3EB9A23C78E3");
      var userModel = new UserModel { Id = userId };
      var userViewModel = new UserViewModel { Id = userId };
      var userRepository = new Mock&lt;IUserRepository&gt;();
      var viewModelToModelMapper = new Mock&lt;IAutoMapper&lt;UserViewModel, UserModel&gt;&gt;();
      var modelToViewModelMapper = new Mock&lt;IAutoMapper&lt;UserModel, UserViewModel&gt;&gt;();
  
      // Setup the Mock UserRepository
      userRepository.Setup(r =&gt; r.Update(It.Is&lt;UserModel&gt;(um =&gt; um.Equals(userModel))));
  
      viewModelToModelMapper.Setup(vmm =&gt; vmm.BuildFrom(It.Is&lt;UserViewModel&gt;(u =&gt; u.Equals(userViewModel))))
             .Returns(userModel);
  
      var userService = new UserService(userRepository.Object, viewModelToModelMapper.Object, modelToViewModelMapper.Object);
  
      // Execute
      userService.Save(userViewModel);
  
      // Assert
      userRepository.VerifyAll();
      viewModelToModelMapper.VerifyAll();
  }
  </code></pre>
  
  <p>Where am I going wrong with my Moqing?</p>