public void invalid_user_should_throw( User user, InvalidUser validationResult, [Frozen] IUserValidator validator, // note we don't setup repository at all UserService userService) { validator.Validate(user).Returns(validationResult); userService.Invoking(_ => _.Register(user)) .ShouldThrow<Exception>(validationResult.Message); // assert on exceptions with messages }
public void happy_path( User user, [Frozen] IUserRepository repository, [Frozen] IUserValidator validator, UserService userService) { repository.Exists(user.Name).Returns(false); validator.Validate(user).Returns(new ValidUser()); userService.Register(user); repository.Received().Add(user); }
public async Task CreateUserAsyncBadRequestException([Frozen] Mock <IUnitOfWork> unitOfWorkMock, [Frozen] Mock <IUserRepository> userRepositoryMock, UserDTO model, User userRecord, UserService sut) { // Arrange userRepositoryMock.Setup(x => x.GetUserAsync(model.Email)).ReturnsAsync(userRecord); unitOfWorkMock.Setup(x => x.Users).Returns(userRepositoryMock.Object); // Act Func <Task> act = async() => await sut.CreateUserAsync(model); // Assert Exception result = await Assert.ThrowsAsync <BadRequestException>(act); Assert.Equal("User with this email alredy exists", result.Message); userRepositoryMock.Verify(x => x.GetUserAsync(model.Email), Times.Once);; unitOfWorkMock.Verify(x => x.Users, Times.Once); }
public async Task CreateUserAsyncSuccessful([Frozen] Mock <IUnitOfWork> unitOfWorkMock, [Frozen] Mock <IUserRepository> userRepositoryMock, UserDTO model, UserService sut) { // Arrange var encodedPassword = PasswordHasher.EncodePasswordToBase64(model.Password); userRepositoryMock.Setup(x => x.GetUserAsync(model.Email)).ReturnsAsync((User)null); unitOfWorkMock.Setup(x => x.Users).Returns(userRepositoryMock.Object); // Act await sut.CreateUserAsync(model); // Assert userRepositoryMock.Verify(x => x.GetUserAsync(model.Email), Times.Once); userRepositoryMock.Verify(x => x.CreateUserAsync(It.Is <User>(user => user.Email.Equals(model.Email))), Times.Once); unitOfWorkMock.Verify(x => x.Users, Times.Exactly(2)); unitOfWorkMock.Verify(x => x.CommitAsync(), Times.Once); }