Пример #1
0
        public async Task DepositFundsAsync_ValidDepositAmount_ReturnsExpectedBalance()
        {
            //// Arrange

            decimal lastTransactionBalanceBefore = 10;
            decimal lastTransactionAmount        = 5;
            decimal depositAmount        = 5;
            decimal depositBalanceBefore = 15;

            // Setup Mocks

            Mock <IWalletRepository> walletRepositoryMock = new Mock <IWalletRepository>();

            WalletEntry lastTransaction = new WalletEntry {
                Amount = lastTransactionAmount, BalanceBefore = lastTransactionBalanceBefore
            };

            walletRepositoryMock
            .Setup(walletRepository => walletRepository.GetLastWalletEntryAsync())
            .Returns(Task.FromResult(lastTransaction));

            walletRepositoryMock
            .Setup(walletRepositoryMock => walletRepositoryMock.InsertWalletEntryAsync(It.Is <WalletEntry>(
                                                                                           walletEntry => _compareWalletEntry(walletEntry, depositAmount, depositBalanceBefore)
                                                                                           ))
                   )
            .Returns(Task.CompletedTask);

            IWalletRepository walletRepository = walletRepositoryMock.Object;

            // Initialize SUT

            IWalletService walletService = new WalletService(walletRepository);
            Deposit        deposit       = new Deposit {
                Amount = depositAmount
            };

            // Set expectations

            decimal expectedBalanceAmount = 20;
            Balance expectedBalance       = new Balance {
                Amount = expectedBalanceAmount
            };

            //// Act

            Balance actualBalance = await walletService.DepositFundsAsync(deposit);

            //// Assert

            actualBalance.ShouldCompare(expectedBalance);

            walletRepositoryMock.Verify(walletRepository => walletRepository.GetLastWalletEntryAsync(), Times.Once);
            walletRepositoryMock.Verify(walletRepository => walletRepository.InsertWalletEntryAsync(It.Is <WalletEntry>(
                                                                                                        walletEntry => _compareWalletEntry(walletEntry, depositAmount, depositBalanceBefore))
                                                                                                    ), Times.Once);
            walletRepositoryMock.VerifyNoOtherCalls();
        }