Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is part of the fun of any kind of test-first approach, it will force you to design your objects in a way that they can be tested - and they'll be better off for it!</p> <p>In this example I'd consider doing the following: (Please note that the following code has not been tested, nor is it the "perfect" solution - but it is more testable and closer to where you want to be).</p> <pre><code>//Assume the moneybox only contains £1 coins (hence int is ok) public void Withdraw(int withdrawlAmount, Action&lt;InsufficientFundsException&gt; handleException) { if(this.balance &gt;= withdrawlAmount) { this.balance -= withdrawlAmount; } else { handleException(new InsufficientFundsException()); } } </code></pre> <p>You can then in your test code have a field or property for a possible exception, and when you call the Withdraw method just specify a quick action to do the callback, eg:</p> <pre><code>moneyBox.Withdraw(10, e =&gt; this.WithdrawException = e); </code></pre> <p>Your <code>Then</code> statement can then assert that the <code>WithdrawException</code> was not null, and make asserts on the message within it.</p> <p>Your production code (i.e. console application) would possibly do this instead:</p> <pre><code>moneyBox.Withdraw(10, e =&gt; Console.WriteLine("Error: {0}", e.Message)); </code></pre> <p>The more perfect solution would be replacing the <code>Action&lt;InsufficientFundsException&gt;</code> with something like <code>Action&lt;MoneyBoxWithdrawResponse&gt;</code>, or something similar. Where <code>MoneyBoxWithdrawResponse</code> contain information on the success of the operation, perhaps the remaining balance and also any potential error/exception details.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload