main
  1using System;
  2using Machine.Specifications;
  3using solidware.financials.service.domain.accounting;
  4
  5namespace specs.unit.service.domain.accounting
  6{
  7    public class TransactionSpecs
  8    {
  9        public class concern : runner<Transaction>
 10        {
 11            protected override Transaction create_sut()
 12            {
 13                return Transaction.New(Currency.CAD);
 14            }
 15            Establish c = () => {
 16                //sut = Activator.CreateInstance(GetType());
 17                sut = new concern().create_sut();
 18            };
 19        }
 20
 21        [Concern(typeof (Transaction))]
 22        public class when_transferring_funds_from_one_account_to_another : concern
 23        {
 24            Establish c = () =>
 25            {
 26                source_account = DetailAccount.New(Currency.CAD);
 27                destination_account = DetailAccount.New(Currency.CAD);
 28                source_account.add(Entry.New<Deposit>(100, Currency.CAD));
 29            };
 30
 31            Because b = () =>
 32            {
 33                sut.deposit(destination_account, new Quantity(100, Currency.CAD));
 34                sut.withdraw(source_account, new Quantity(100, Currency.CAD));
 35                sut.post();
 36            };
 37
 38            It should_increase_the_balance_of_the_destination_account = () =>
 39            {
 40                destination_account.balance().should_be_equal_to(new Quantity(100, Currency.CAD));
 41            };
 42
 43            It should_decrease_the_balance_of_the_source_account = () =>
 44            {
 45                source_account.balance().should_be_equal_to(new Quantity(0, Currency.CAD));
 46            };
 47
 48            static DetailAccount source_account;
 49            static DetailAccount destination_account;
 50        }
 51
 52        [Concern(typeof (Transaction))]
 53        public class when_transferring_funds_from_one_account_to_multiple_accounts : concern
 54        {
 55            Establish c = () =>
 56            {
 57                chequing = DetailAccount.New(Currency.CAD);
 58                savings = DetailAccount.New(Currency.CAD);
 59                rrsp = DetailAccount.New(Currency.CAD);
 60                chequing.add(Entry.New<Deposit>(100, Currency.CAD));
 61            };
 62
 63            Because b = () =>
 64            {
 65                sut.withdraw(chequing, new Quantity(100, Currency.CAD));
 66                sut.deposit(savings, new Quantity(75, Currency.CAD));
 67                sut.deposit(rrsp, new Quantity(25, Currency.CAD));
 68                sut.post();
 69            };
 70
 71            It should_increase_the_balance_of_each_destination_account = () =>
 72            {
 73                savings.balance().should_be_equal_to(new Quantity(75, Currency.CAD));
 74                rrsp.balance().should_be_equal_to(new Quantity(25, Currency.CAD));
 75            };
 76
 77            It should_decrease_the_balance_of_the_source_account = () =>
 78            {
 79                chequing.balance().should_be_equal_to(new Quantity(0, Currency.CAD));
 80            };
 81
 82            static DetailAccount chequing;
 83            static DetailAccount savings;
 84            static DetailAccount rrsp;
 85        }
 86
 87        [Concern(typeof (Transaction))]
 88        public class when_a_transaction_does_not_reconcile_to_a_zero_balance : concern
 89        {
 90            Establish c = () =>
 91            {
 92                chequing = DetailAccount.New(Currency.CAD);
 93                savings = DetailAccount.New(Currency.CAD);
 94                chequing.add(Entry.New<Deposit>(100, Currency.CAD));
 95            };
 96
 97            Because b = () =>
 98            {
 99                sut.withdraw(chequing, new Quantity(1, Currency.CAD));
100                sut.deposit(savings, new Quantity(100, Currency.CAD));
101                call = () =>
102                {
103                    sut.post();
104                };
105            };
106
107            It should_not_transfer_any_money = () =>
108            {
109                call.should_have_thrown<TransactionDoesNotBalance>();
110            };
111
112            static DetailAccount chequing;
113            static DetailAccount savings;
114            static Action call;
115        }
116    }
117}