|
6 | 6 | public class BankAccountTests |
7 | 7 | { |
8 | 8 | [Fact] |
9 | | - public void Returns_empty_balance_after_opening() |
| 9 | + public void Newly_opened_account_has_zero_balance() |
10 | 10 | { |
11 | 11 | var account = new BankAccount(); |
12 | 12 | account.Open(); |
13 | | - |
14 | 13 | Assert.Equal(0m, account.Balance); |
15 | 14 | } |
16 | 15 |
|
17 | 16 | [Fact(Skip = "Remove this Skip property to run this test")] |
18 | | - public void Check_basic_balance() |
| 17 | + public void Single_deposit() |
19 | 18 | { |
20 | 19 | var account = new BankAccount(); |
21 | 20 | account.Open(); |
| 21 | + account.Deposit(100m); |
| 22 | + Assert.Equal(100m, account.Balance); |
| 23 | + } |
22 | 24 |
|
23 | | - var openingBalance = account.Balance; |
| 25 | + [Fact(Skip = "Remove this Skip property to run this test")] |
| 26 | + public void Multiple_deposits() |
| 27 | + { |
| 28 | + var account = new BankAccount(); |
| 29 | + account.Open(); |
| 30 | + account.Deposit(100m); |
| 31 | + account.Deposit(50m); |
| 32 | + Assert.Equal(150m, account.Balance); |
| 33 | + } |
24 | 34 |
|
25 | | - account.UpdateBalance(10m); |
26 | | - var updatedBalance = account.Balance; |
| 35 | + [Fact(Skip = "Remove this Skip property to run this test")] |
| 36 | + public void Withdraw_once() |
| 37 | + { |
| 38 | + var account = new BankAccount(); |
| 39 | + account.Open(); |
| 40 | + account.Deposit(100m); |
| 41 | + account.Withdraw(75m); |
| 42 | + Assert.Equal(25m, account.Balance); |
| 43 | + } |
27 | 44 |
|
28 | | - Assert.Equal(0m, openingBalance); |
29 | | - Assert.Equal(10m, updatedBalance); |
| 45 | + [Fact(Skip = "Remove this Skip property to run this test")] |
| 46 | + public void Withdraw_twice() |
| 47 | + { |
| 48 | + var account = new BankAccount(); |
| 49 | + account.Open(); |
| 50 | + account.Deposit(100m); |
| 51 | + account.Withdraw(80m); |
| 52 | + account.Withdraw(20m); |
| 53 | + Assert.Equal(0m, account.Balance); |
30 | 54 | } |
31 | 55 |
|
32 | 56 | [Fact(Skip = "Remove this Skip property to run this test")] |
33 | | - public void Balance_can_increment_and_decrement() |
| 57 | + public void Can_do_multiple_operations_sequentially() |
34 | 58 | { |
35 | 59 | var account = new BankAccount(); |
36 | 60 | account.Open(); |
37 | | - var openingBalance = account.Balance; |
| 61 | + account.Deposit(100m); |
| 62 | + account.Deposit(110m); |
| 63 | + account.Withdraw(200m); |
| 64 | + account.Deposit(60m); |
| 65 | + account.Withdraw(50m); |
| 66 | + Assert.Equal(20m, account.Balance); |
| 67 | + } |
38 | 68 |
|
39 | | - account.UpdateBalance(10m); |
40 | | - var addedBalance = account.Balance; |
| 69 | + [Fact(Skip = "Remove this Skip property to run this test")] |
| 70 | + public void Cannot_check_balance_of_closed_account() |
| 71 | + { |
| 72 | + var account = new BankAccount(); |
| 73 | + account.Open(); |
| 74 | + account.Close(); |
| 75 | + Assert.Throws<InvalidOperationException>(() => account.Balance); |
| 76 | + } |
41 | 77 |
|
42 | | - account.UpdateBalance(-15m); |
43 | | - var subtractedBalance = account.Balance; |
| 78 | + [Fact(Skip = "Remove this Skip property to run this test")] |
| 79 | + public void Cannot_deposit_into_closed_account() |
| 80 | + { |
| 81 | + var account = new BankAccount(); |
| 82 | + account.Open(); |
| 83 | + account.Close(); |
| 84 | + Assert.Throws<InvalidOperationException>(() => account.Deposit(50m)); |
| 85 | + } |
44 | 86 |
|
45 | | - Assert.Equal(0m, openingBalance); |
46 | | - Assert.Equal(10m, addedBalance); |
47 | | - Assert.Equal(-5m, subtractedBalance); |
| 87 | + [Fact(Skip = "Remove this Skip property to run this test")] |
| 88 | + public void Cannot_deposit_into_unopened_account() |
| 89 | + { |
| 90 | + var account = new BankAccount(); |
| 91 | + Assert.Throws<InvalidOperationException>(() => account.Deposit(50m)); |
48 | 92 | } |
49 | 93 |
|
50 | 94 | [Fact(Skip = "Remove this Skip property to run this test")] |
51 | | - public void Closed_account_throws_exception_when_checking_balance() |
| 95 | + public void Cannot_withdraw_from_closed_account() |
52 | 96 | { |
53 | 97 | var account = new BankAccount(); |
54 | 98 | account.Open(); |
55 | 99 | account.Close(); |
| 100 | + Assert.Throws<InvalidOperationException>(() => account.Withdraw(50m)); |
| 101 | + } |
56 | 102 |
|
57 | | - Assert.Throws<InvalidOperationException>(() => account.Balance); |
| 103 | + [Fact(Skip = "Remove this Skip property to run this test")] |
| 104 | + public void Cannot_close_an_account_that_was_not_opened() |
| 105 | + { |
| 106 | + var account = new BankAccount(); |
| 107 | + Assert.Throws<InvalidOperationException>(() => account.Close()); |
58 | 108 | } |
59 | 109 |
|
60 | 110 | [Fact(Skip = "Remove this Skip property to run this test")] |
61 | | - public void Change_account_balance_from_multiple_threads() |
| 111 | + public void Cannot_open_an_already_opened_account() |
62 | 112 | { |
63 | 113 | var account = new BankAccount(); |
64 | | - var tasks = new List<Task>(); |
| 114 | + account.Open(); |
| 115 | + Assert.Throws<InvalidOperationException>(() => account.Open()); |
| 116 | + } |
65 | 117 |
|
66 | | - var threads = 500; |
67 | | - var iterations = 100; |
| 118 | + [Fact(Skip = "Remove this Skip property to run this test")] |
| 119 | + public void Reopened_account_does_not_retain_balance() |
| 120 | + { |
| 121 | + var account = new BankAccount(); |
| 122 | + account.Open(); |
| 123 | + account.Deposit(50m); |
| 124 | + account.Close(); |
| 125 | + account.Open(); |
| 126 | + Assert.Equal(0m, account.Balance); |
| 127 | + } |
68 | 128 |
|
| 129 | + [Fact(Skip = "Remove this Skip property to run this test")] |
| 130 | + public void Cannot_withdraw_more_than_deposited() |
| 131 | + { |
| 132 | + var account = new BankAccount(); |
69 | 133 | account.Open(); |
70 | | - for (int i = 0; i < threads; i++) |
| 134 | + account.Deposit(25m); |
| 135 | + Assert.Throws<InvalidOperationException>(() => account.Withdraw(50m)); |
| 136 | + } |
| 137 | + |
| 138 | + [Fact(Skip = "Remove this Skip property to run this test")] |
| 139 | + public void Cannot_withdraw_negative() |
| 140 | + { |
| 141 | + var account = new BankAccount(); |
| 142 | + account.Open(); |
| 143 | + account.Deposit(100m); |
| 144 | + Assert.Throws<InvalidOperationException>(() => account.Withdraw(-50m)); |
| 145 | + } |
| 146 | + |
| 147 | + [Fact(Skip = "Remove this Skip property to run this test")] |
| 148 | + public void Cannot_deposit_negative() |
| 149 | + { |
| 150 | + var account = new BankAccount(); |
| 151 | + account.Open(); |
| 152 | + Assert.Throws<InvalidOperationException>(() => account.Deposit(-50m)); |
| 153 | + } |
| 154 | + |
| 155 | + [Fact(Skip = "Remove this Skip property to run this test")] |
| 156 | + public void Can_handle_concurrent_transactions() |
| 157 | + { |
| 158 | + var account = new BankAccount(); |
| 159 | + account.Open(); |
| 160 | + for (int i = 0; i < 500; i++) |
71 | 161 | { |
| 162 | + var tasks = new List<Task>(); |
72 | 163 | tasks.Add(Task.Factory.StartNew(() => |
73 | 164 | { |
74 | | - for (int j = 0; j < iterations; j++) |
| 165 | + for (int j = 0; j < 100; j++) |
75 | 166 | { |
76 | | - account.UpdateBalance(1m); |
77 | | - account.UpdateBalance(-1m); |
| 167 | + account.Deposit(1m); |
| 168 | + account.Withdraw(1m); |
78 | 169 | } |
79 | 170 | })); |
| 171 | + Task.WaitAll(tasks.ToArray()); |
80 | 172 | } |
81 | | - Task.WaitAll(tasks.ToArray()); |
82 | | - |
83 | 173 | Assert.Equal(0m, account.Balance); |
84 | 174 | } |
85 | 175 | } |
0 commit comments