public async Task GetBalanceAsync_CurrentBalanceIs4_ReturnsOk4Async() { //// Arrange decimal currentBalanceAmount = 4; // Setup Mocks Mock <IWalletService> walletServiceMock = new Mock <IWalletService>(); Balance currentBalance = new Balance() { Amount = currentBalanceAmount }; walletServiceMock .Setup(walletService => walletService.GetBalanceAsync()) .Returns(Task.FromResult(currentBalance)); 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/Balance"; // Set Expectations BalanceResponse expectedBalanceResponse = new BalanceResponse() { Amount = currentBalanceAmount }; //// Act HttpResponseMessage response = await client.GetAsync(endpoint); BalanceResponse actualBalanceResponse = await response.Content.ReadAsAsync <BalanceResponse>(new[] { new JsonMediaTypeFormatter() }); //// Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); actualBalanceResponse.ShouldCompare(expectedBalanceResponse); walletServiceMock.Verify(walletService => walletService.GetBalanceAsync(), Times.Once); 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(); }