public void Invoke_ValidData_AddsUserToDatabaseWithCorrectValues() { var user = new DataAccess.Models.User { Name = "Ryszard" }; DataAccess.Models.UserToken tokensaved = null; var mockedUserTokenRepository = new Mock <IUserTokenRepository>(); var mockedUserRepository = new Mock <IUserRepository>(); var mockedHasher = new Mock <IHasher>(); mockedUserRepository.Setup(r => r.GetById(It.IsAny <Guid>())).Returns(user); mockedHasher.Setup(r => r.GenerateRandomGuid()).Returns("123"); mockedUserTokenRepository.Setup(r => r.Add(It.IsAny <DataAccess.Models.UserToken>())) .Callback <DataAccess.Models.UserToken>(u => tokensaved = u); var mockedUnitOfWork = new Mock <IUnitOfWork>(); var action = new AddUserTokenToUser(mockedUserTokenRepository.Object, mockedUserRepository.Object, mockedUnitOfWork.Object, mockedHasher.Object); action.Invoke(Guid.NewGuid()); Assert.Equal("Ryszard", tokensaved.User.Name); Assert.Equal("123", tokensaved.SecretToken); mockedUserTokenRepository.Verify(r => r.Delete(It.IsAny <DataAccess.Models.UserToken>()), Times.Never); mockedUserTokenRepository.Verify(r => r.Add(It.IsAny <DataAccess.Models.UserToken>()), Times.Once()); mockedUnitOfWork.Verify(r => r.Save(), Times.Once()); }
public void Invoke_InvalidData_AddNorSaveAreCalled() { // prepare var mockedUserRepository = new Mock <IUserRepository>(); var mockedUserTokenRepository = new Mock <IUserTokenRepository>(); var mockedUnitOfWork = new Mock <IUnitOfWork>(); var action = new AddUserTokenToUser(mockedUserTokenRepository.Object, mockedUserRepository.Object, mockedUnitOfWork.Object); // action var actionResult = action.Invoke(Guid.NewGuid()); // assert Assert.Equal(string.Empty, actionResult); mockedUserRepository.Verify(r => r.GetById(It.IsAny <Guid>()), Times.Once); mockedUserTokenRepository.Verify(r => r.Delete(It.IsAny <DataAccess.Models.UserToken>()), Times.Never); mockedUserTokenRepository.Verify(r => r.Add(It.IsAny <DataAccess.Models.UserToken>()), Times.Never); mockedUnitOfWork.Verify(r => r.Save(), Times.Never); }