public async Task Withdrawal_WithdrawalInvalidAmounts_ReturnsBadRequest(int withdrawalAmount) { //// Arrange // Setup Mocks Mock <IWalletService> walletServiceMock = new Mock <IWalletService>(); IWalletService walletService = walletServiceMock.Object; // Initialize HTTP client and request data WebApplicationFactory <Startup> factory = new CustomWebApplicationFactory <Startup>(services => services.SwapTransient(provider => walletService) ); HttpClient client = factory.CreateClient(); string endpoint = "Wallet/Withdraw"; WithdrawalRequest withdrawalRequest = new WithdrawalRequest { Amount = withdrawalAmount }; StringContent content = withdrawalRequest.AsStringContent(); //// Act HttpResponseMessage response = await client.PostAsync(endpoint, content); //// Assert Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); walletServiceMock.VerifyNoOtherCalls(); }
public async Task Withdrawal_WithdrawalInsufficientBalanceException_ReturnsBadRequest() { //// Arrange int withdrawalAmount = 1000; // Setup Mocks Mock <IWalletService> walletServiceMock = new Mock <IWalletService>(); Withdrawal withdrawal = new Withdrawal { Amount = withdrawalAmount }; walletServiceMock .Setup(walletService => walletService.WithdrawFundsAsync(It.Is <Withdrawal>( actualWithdrawal => _comparer.Compare(withdrawal, actualWithdrawal).AreEqual))) .Throws <InsufficientBalanceException>(); IWalletService walletService = walletServiceMock.Object; // Initialize HTTP client and request data WebApplicationFactory <Startup> factory = new CustomWebApplicationFactory <Startup>(services => services.SwapTransient(provider => walletService) ); HttpClient client = factory.CreateClient(); string endpoint = "Wallet/Withdraw"; WithdrawalRequest withdrawalRequest = new WithdrawalRequest { Amount = withdrawalAmount }; StringContent content = withdrawalRequest.AsStringContent(); //// Act HttpResponseMessage response = await client.PostAsync(endpoint, content); //// Assert Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); walletServiceMock.Verify(walletService => walletService.WithdrawFundsAsync(It.Is <Withdrawal>( actualWithdrawal => _comparer.Compare(withdrawal, actualWithdrawal).AreEqual) ), Times.Once); walletServiceMock.VerifyNoOtherCalls(); }
public async Task Withdraw_Withdraw5_ReturnsOkPostWithdrawalBalanceAsync() { //// Arrange decimal withdrawalAmount = 5; decimal postWithdrawalBalanceAmount = 10; // Setup Mocks Mock <IWalletService> walletServiceMock = new Mock <IWalletService>(); Withdrawal withdrawal = new Withdrawal { Amount = withdrawalAmount }; Balance postWithdrawalBalance = new Balance() { Amount = postWithdrawalBalanceAmount }; walletServiceMock .Setup(walletService => walletService.WithdrawFundsAsync(It.Is <Withdrawal>(actualWithdrawal => _comparer.Compare(withdrawal, actualWithdrawal).AreEqual))) .Returns(Task.FromResult(postWithdrawalBalance)); IWalletService walletService = walletServiceMock.Object; // Initialize HTTP client and request data WebApplicationFactory <Startup> factory = new CustomWebApplicationFactory <Startup>(services => services.SwapTransient(provider => walletService) ); HttpClient client = factory.CreateClient(); string endpoint = "Wallet/Withdraw"; WithdrawalRequest withdrawalRequest = new WithdrawalRequest { Amount = withdrawalAmount }; StringContent content = withdrawalRequest.AsStringContent(); // Set Expectations BalanceResponse expectedBalanceResponse = new BalanceResponse() { Amount = postWithdrawalBalanceAmount }; //// Act HttpResponseMessage response = await client.PostAsync(endpoint, content); BalanceResponse actualBalanceResponse = await response.Content.ReadAsAsync <BalanceResponse>(new[] { new JsonMediaTypeFormatter() }); //// Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); actualBalanceResponse.ShouldCompare(expectedBalanceResponse); walletServiceMock.Verify(walletService => walletService.WithdrawFundsAsync(It.Is <Withdrawal>( actualWithdrawal => _comparer.Compare(withdrawal, actualWithdrawal).AreEqual) ), Times.Once); walletServiceMock.VerifyNoOtherCalls(); }