I'm using Moq for the first time, and I'm struggling with getting the tests to run properly.
I'm trying to moq the Save()
method of my service layer.
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));
}
I've got Three tests, of which, I'm not sure any of them are right. The first two are passing, but the third one fails with
Moq.MockVerificationException : The following setups were not matched:
IUserRepository r => r.Update(It.Is(um => um.Equals()))
Here are the tests.
// 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<IUserRepository>();
var viewModelToModelMapper = new Mock<IAutoMapper<UserViewModel, UserModel>>();
var modelToViewModelMapper = new Mock<IAutoMapper<UserModel, UserViewModel>>();
// Setup the Mock UserRepository
userRepository.Setup(r => r.Create(It.Is<UserModel>(um => um.Equals(userModel))));
viewModelToModelMapper.Setup(vmm => vmm.BuildFrom(It.Is<UserViewModel>(u => u.Equals(userViewModel))))
.Returns(userModel);
var userService = new UserService(userRepository.Object, viewModelToModelMapper.Object, modelToViewModelMapper.Object);
// Execute
userService.Save(userViewModel);
// Assert
userRepository.VerifyAll();
viewModelToModelMapper.VerifyAll();
}
// 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<IUserRepository>();
var viewModelToModelMapper = new Mock<IAutoMapper<UserViewModel, UserModel>>();
var modelToViewModelMapper = new Mock<IAutoMapper<UserModel, UserViewModel>>();
// Setup the Mock UserRepository
userRepository.Setup(r => r.Create(It.Is<UserModel>(um => um.Equals(userModel))));
viewModelToModelMapper.Setup(vmm => vmm.BuildFrom(It.Is<UserViewModel>(u => u.Equals(userViewModel))))
.Returns(userModel);
var userService = new UserService(userRepository.Object, viewModelToModelMapper.Object, modelToViewModelMapper.Object);
// Execute
userService.Save(userViewModel);
// Assert
userRepository.VerifyAll();
viewModelToModelMapper.VerifyAll();
}
// 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<IUserRepository>();
var viewModelToModelMapper = new Mock<IAutoMapper<UserViewModel, UserModel>>();
var modelToViewModelMapper = new Mock<IAutoMapper<UserModel, UserViewModel>>();
// Setup the Mock UserRepository
userRepository.Setup(r => r.Update(It.Is<UserModel>(um => um.Equals(userModel))));
viewModelToModelMapper.Setup(vmm => vmm.BuildFrom(It.Is<UserViewModel>(u => u.Equals(userViewModel))))
.Returns(userModel);
var userService = new UserService(userRepository.Object, viewModelToModelMapper.Object, modelToViewModelMapper.Object);
// Execute
userService.Save(userViewModel);
// Assert
userRepository.VerifyAll();
viewModelToModelMapper.VerifyAll();
}
Where am I going wrong with my Moqing?
Seems like in the last case you would need:
userRepository.Setup(r => r.GetById(userId)).Returns(userModel);
Before the userService.Save call. Without it userRepository.GetById will always be null and the update branch will never be taken.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With