public async Task <IActionResult> BuySnack([FromBody] BuySnackModel.SnackRequest snackRequest) { if (snackRequest == null) { return(this.BadRequest($"Request {nameof(BuySnackModel.SnackRequest)} is null")); } var maybeAccount = await this.accountRepository.GetAccountAsync(); if (!maybeAccount.TryGetValue(out var account)) { return(this.NotFound("No account available")); } var maybeSnack = await this.snackRepository.GetSnackAsync(snackRequest.SnackId); if (!maybeSnack.TryGetValue(out var snack)) { return(this.NotFound($"Snack with id: {snackRequest.SnackId} does not exists")); } var result = this.accountService.BuyWithExchange(account, snack); if (!result.Success) { return(this.BadRequest(result.Code)); } await this.accountRepository.UpdateAccountAsync(account); return(this.NoContent()); }
public async Task Create_NotFound_result_when_snack_does_not_exists( [Frozen] Mock <IAccountRepository> accountRepositoryMock, BuySnackModel.SnackRequest snackRequest, AccountController sut) { // Act var result = await sut.BuySnack(snackRequest); // Assert accountRepositoryMock.Verify(x => x.UpdateAccountAsync(It.IsAny <Account>()), Times.Never); result.Should().BeOfType <NotFoundObjectResult>(); ((ObjectResult)result).Value.ToString().Should().Be($"Snack with id: {snackRequest.SnackId} does not exists"); }
public async Task Create_NotFound_result_when_no_account_is_available( [Frozen] Mock <IAccountRepository> accountRepositoryMock, BuySnackModel.SnackRequest snackRequest, AccountController sut) { // Act var result = await sut.BuySnack(snackRequest); // Assert accountRepositoryMock.Verify(x => x.UpdateAccountAsync(It.IsAny <Account>()), Times.Never); result.Should().BeOfType <NotFoundObjectResult>(); ((ObjectResult)result).Value.ToString().Should().Be("No account available"); }
public async Task Create_Ok_result_when_transaction_is_completed( [Frozen] Mock <IAccountRepository> accountRepositoryMock, [Frozen] Mock <IAccountService> accountServiceMock, BuySnackModel.SnackRequest snackRequest, AccountController sut) { // Arrange accountServiceMock.Setup(x => x.BuyWithExchange(It.IsAny <Account>(), It.IsAny <Snack>())) .Returns(() => Result <AccountCode> .Ok(AccountCode.Ok)); // Act var result = await sut.BuySnack(snackRequest); // Assert accountRepositoryMock.Verify(x => x.UpdateAccountAsync(It.IsAny <Account>()), Times.Once); result.Should().BeOfType <NoContentResult>(); }
public async Task Create_BadRequest_result_when_transaction_cannot_be_completed( [Frozen] Mock <IAccountRepository> accountRepositoryMock, [Frozen] Mock <IAccountService> accountServiceMock, BuySnackModel.SnackRequest snackRequest, AccountController sut) { // Arrange accountServiceMock.Setup(x => x.BuyWithExchange(It.IsAny <Account>(), It.IsAny <Snack>())) .Returns(() => Result <AccountCode> .Fail(AccountCode.NotEnoughChange)); // Act var result = await sut.BuySnack(snackRequest); // Assert accountRepositoryMock.Verify(x => x.UpdateAccountAsync(It.IsAny <Account>()), Times.Never); result.Should().BeOfType <BadRequestObjectResult>(); }