public async Task Deposit_DepositInvalidAmounts_ReturnsBadRequest(int depositAmount) { //// 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/Deposit"; DepositRequest depositRequest = new DepositRequest { Amount = depositAmount }; StringContent content = depositRequest.AsStringContent(); //// Act HttpResponseMessage response = await client.PostAsync(endpoint, content); //// Assert Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); walletServiceMock.VerifyNoOtherCalls(); }
public async Task Deposit_Deposit10_ReturnsOkPostDepositBalanceAsync() { //// Arrange decimal postDepositBalanceAmount = 20; decimal depositAmount = 10; // Setup Mocks Mock <IWalletService> walletServiceMock = new Mock <IWalletService>(); Balance postDepositBalance = new Balance() { Amount = postDepositBalanceAmount }; Deposit deposit = new Deposit { Amount = depositAmount }; walletServiceMock .Setup(walletService => walletService.DepositFundsAsync(It.Is <Deposit>(actualDeposit => _comparer.Compare(deposit, actualDeposit).AreEqual)) ) .Returns(Task.FromResult(postDepositBalance)); 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/Deposit"; DepositRequest depositRequest = new DepositRequest { Amount = depositAmount }; StringContent payload = depositRequest.AsStringContent(); // Set Expectations BalanceResponse expectedBalanceResponse = new BalanceResponse() { Amount = postDepositBalanceAmount }; //// Act HttpResponseMessage response = await client.PostAsync(endpoint, payload); BalanceResponse actualBalanceResponse = await response.Content.ReadAsAsync <BalanceResponse>(new[] { new JsonMediaTypeFormatter() }); //// Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); actualBalanceResponse.ShouldCompare(expectedBalanceResponse); walletServiceMock.Verify(walletService => walletService.DepositFundsAsync(It.Is <Deposit>( actualDeposit => _comparer.Compare(deposit, actualDeposit).AreEqual) ), Times.Once); walletServiceMock.VerifyNoOtherCalls(); }