public void GetUserById_CallsRepository_ReturnsUser() { //Arrange var id = Guid.NewGuid(); var user = new User( id, fixture.Create <string>(), fixture.Create <string>(), fixture.Create <string>(), fixture.Create <string>(), fixture.Create <DateTime>() ); userRepository.GetById(Arg.Is(id)).Returns(user); GetUserByIdRequest request = new GetUserByIdRequest(id.ToString()); CancellationToken token = new CancellationToken(); //Act Task <User> task = handler.Handle(request, token); task.Wait(); User result = task.Result; //Assert userRepository.Received(1).GetById(Arg.Is(id)); result.Should().NotBeNull(); result.Should().BeEquivalentTo(user); }
public async Task Should_ThrowException_When_UserStatusDisabled() { // Arrange IRepository <User> repository = new Repository <User>(_fixture.Context); var user = new User { UserId = 5, CompanyId = 1, UserName = "******", FullName = "Test User", Status = Domain.Enums.UserStatus.Disabled, CreatedAt = DateTime.Now, CreatedBy = -1 }; _fixture.Context.Users.Add(user); _fixture.Context.SaveChanges(); var handler = new GetUserHandler(repository, _mapper, _logger); var query = new GetUserQuery(companyId: 1, userId: 5); // Act var tcs = new CancellationToken(); var exception = await Assert.ThrowsAsync <VacationTrackingException>(async() => { await handler.Handle(query, tcs); }); // Assert Assert.NotNull(exception); Assert.Equal(404, exception.Code); Assert.Equal(ExceptionMessages.ItemNotFound, exception.Message); }
public void Test1() { var fixture = new Fixture().Customize(new AutoMoqCustomization()); var builder = new DbContextOptionsBuilder <DatabaseContext>() .UseInMemoryDatabase("TempDatabase"); var context = new DatabaseContext(builder.Options); var mapper = new Mock <IMapper>(); var userDto = fixture.Create <UserDto>(); var user = new User() { Id = userDto.UserId }; mapper.Setup(x => x.Map <User, UserDto>(It.IsAny <User>())) .Returns(userDto); context.Users.Add(user); context.SaveChanges(); GetUserHandler handler = new GetUserHandler(context, mapper.Object); var result = handler.Handle(new GetUser(1), CancellationToken.None).Result; Assert.NotNull(result); Assert.Equal(result.UserId, user.Id); }
public async Task Return_failed_result_when_validation_fails() { // Arrange var validator = Substitute.For <IValidateRequest <GetUser> >(); validator .IsValidAsync(Arg.Any <GetUser>()) .Returns(Task.FromResult(false)); var sut = new GetUserHandler(validator, _dbContext); // Act var result = await sut.Handle(new GetUser("a-sub"), CancellationToken.None); // Assert result.IsFailure.ShouldBeTrue(); }
public async Task Should_ReturnUser_When_PassValidUserId() { // Arrange IRepository <User> repository = new Repository <User>(_fixture.Context); var handler = new GetUserHandler(repository, _mapper, _logger); var query = new GetUserQuery(companyId: 1, userId: 1); // Act var tcs = new CancellationToken(); var result = await handler.Handle(query, tcs); // Assert Assert.Equal(1, result.UserId); Assert.Equal(1, result.CompanyId); }
public async Task Should_ThrowException_When_UserIdDoesNotBelongsToCompany() { // Arrange IRepository <User> repository = new Repository <User>(_fixture.Context); var handler = new GetUserHandler(repository, _mapper, _logger); var query = new GetUserQuery(companyId: 2, userId: 1); // Act var tcs = new CancellationToken(); var exception = await Assert.ThrowsAsync <VacationTrackingException>(async() => { await handler.Handle(query, tcs); }); // Assert Assert.NotNull(exception); Assert.Equal(404, exception.Code); Assert.Equal(ExceptionMessages.ItemNotFound, exception.Message); }
public AppUserDTO GetUser() { return(_getUserHandler.Handle()); }
public void TheUserShouldBeReturned() { AppUserDTO appUserDTO = _getUserHandler.Handle(); Assert.IsNotNull(appUserDTO); }