public async Task ShouldLendGame() { GenerateMock(); var gameMock = new Mock <Game>(); gameMock.Setup(x => x.Lent(It.IsAny <Guid>())); _gameRepositoryMock .Setup(x => x.FindAsync(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(gameMock.Object); _friendQuery .Setup(x => x.IsValid(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(true); _gameRepositoryMock.Setup(x => x.Update(It.IsAny <Game>())); _gameRepositoryMock.Setup(x => x.UnitOfWork.SaveAsync(It.IsAny <CancellationToken>())); var handler = GetHandler(); var friendId = Guid.NewGuid(); var command = new LendGameCommand(Guid.NewGuid(), friendId); await handler.Handle(command, default); gameMock.Verify(x => x.Lent( It.Is <Guid>(f => f.Equals(friendId)) )); _gameRepositoryMock.Verify(x => x.Update(It.Is <Game>(f => f.Equals(gameMock.Object)))); _gameRepositoryMock.Verify(x => x.UnitOfWork.SaveAsync(It.IsAny <CancellationToken>())); }
public async Task <IActionResult> Lent(Guid id, [FromBody] LendGameCommand command) { if (id != command.Id) { return(BadRequest()); } await _mediator.Send(command); return(NoContent()); }
public void ShouldThrowMessageGameNotFound() { GenerateMock(); _gameRepositoryMock .Setup(x => x.FindAsync(It.IsAny <Guid>(), It.IsAny <CancellationToken>())); var handler = GetHandler(); var command = new LendGameCommand(Guid.NewGuid(), Guid.NewGuid()); FluentActions.Invoking(async() => await handler.Handle(command, default)) .Should() .Throw <NotFoundException>().WithMessage("Game not found!"); _gameRepositoryMock .Verify(x => x.FindAsync(It.Is <Guid>(f => f.Equals(command.Id)), It.IsAny <CancellationToken>())); }
public Task <bool> Handle(LendGameCommand command, CancellationToken cancellationToken) { if (!command.IsValid()) { NotifyValidationErrors(command); return(Task.FromResult(false)); } var game = gameReadRepository.GetById(command.Id); game.Lend(command.PersonId.Value); gameWriteRepository.Update(game); if (Commit()) { mediator.RaiseEventAsync(new LendGameEvent(game.Id, game.PersonId.Value)); } return(Task.FromResult(true)); }
public void ShouldThrowMessageGameInvalidFriend() { GenerateMock(); var gameMock = new Mock <Game>(); _gameRepositoryMock .Setup(x => x.FindAsync(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(gameMock.Object); _friendQuery .Setup(x => x.IsValid(It.IsAny <Guid>(), It.IsAny <CancellationToken>())).ReturnsAsync(false); var handler = GetHandler(); var command = new LendGameCommand(Guid.NewGuid(), Guid.NewGuid()); FluentActions.Invoking(async() => await handler.Handle(command, default)) .Should() .Throw <GuardValidationException>().WithMessage("Invalid friend to lend!"); _friendQuery .Setup(x => x.IsValid(It.Is <Guid>(f => f.Equals(command.FriendId)), It.IsAny <CancellationToken>())); }
public async Task <ValidationResult> Lend(Guid gameId, Guid friendId) { var lendCommand = new LendGameCommand(gameId, friendId); return(await _mediator.SendCommand(lendCommand)); }
public void Lend(Guid id, Guid personId) { var command = new LendGameCommand(id, personId); mediator.SendCommandAsync(command); }