示例#1
0
        public void Can_deposit_amount(decimal balance, decimal depositAmount, decimal finalBalance, DepositStatus expectedStatus, string expectedErrorMessage)
        {
            var card = new CashCard(TestPin, _pinValidationService);

            card.Deposit(balance);

            var response = card.Deposit(depositAmount);

            Assert.That(response.Status, Is.EqualTo(expectedStatus));
            Assert.That(response.ErrorMessage, Is.EqualTo(expectedErrorMessage));
            Assert.That(card.Balance, Is.EqualTo(finalBalance));
        }
示例#2
0
        public void Can_withdraw_multiple_amounts_simultaneously()
        {
            var pinValidationService = Substitute.For <IPinValidationService>();

            pinValidationService.IsPinCorrect(Arg.Any <EncryptedPin>(), Arg.Any <string>()).ReturnsForAnyArgs(true)
            .AndDoes(async _ => await Task.Delay(1000));

            var card = new CashCard(TestPin, pinValidationService);

            card.Deposit(100);

            var tf    = new TaskFactory();
            var tasks = new List <Task>();

            for (var i = 1; i <= 4; i++)
            {
                tasks.Add(tf.StartNew(() =>
                {
                    var resp = card.Withdraw(10, TestPin);
                    Assert.That(resp.Status, Is.EqualTo(WithdrawalStatus.Successful));
                }));
            }

            Task.WaitAll(tasks.ToArray());

            // One final one to ensure we end up with the right value
            var response = card.Withdraw(10, TestPin);

            Assert.That(response.Status, Is.EqualTo(WithdrawalStatus.Successful));
            Assert.That(response.ErrorMessage, Is.EqualTo(""));
            Assert.That(card.Balance, Is.EqualTo(50));
        }
示例#3
0
        public void Cannot_withdraw_more_than_balance_with_multiple_simultaneous_requests()
        {
            var pinValidationService = Substitute.For <IPinValidationService>();

            pinValidationService.IsPinCorrect(Arg.Any <EncryptedPin>(), Arg.Any <string>()).ReturnsForAnyArgs(true)
            .AndDoes(async _ => await Task.Delay(1500));

            var card = new CashCard(TestPin, pinValidationService);

            card.Deposit(25);

            var tf        = new TaskFactory();
            var tasks     = new List <Task>();
            var responses = new List <WithdrawalStatus>();

            for (var i = 1; i <= 4; i++)
            {
                tasks.Add(tf.StartNew(() =>
                {
                    var resp = card.Withdraw(10, TestPin);
                    responses.Add(resp.Status);
                }));
            }

            Task.WaitAll(tasks.ToArray());
            Assert.That(responses.Count(r => r == WithdrawalStatus.Successful), Is.EqualTo(2));
            Assert.That(responses.Count(r => r == WithdrawalStatus.InsufficientFunds), Is.EqualTo(2));

            Assert.That(card.Balance, Is.EqualTo(5));
        }
示例#4
0
        public void Cannot_withdraw_with_incorrect_pin()
        {
            var card = new CashCard(TestPin, _pinValidationService);

            card.Deposit(50);

            var response = card.Withdraw(25, "4321");

            Assert.That(response.Status, Is.EqualTo(WithdrawalStatus.Error));
            Assert.That(response.ErrorMessage, Is.EqualTo(ErrorCodes.InvalidPin));
            Assert.That(card.Balance, Is.EqualTo(50));
        }