Code: https://github.com/DanBunea/katas/tree/master/outside-in%20tdd%20bank%20kata
In "London school" TDD or Outside-In we start from an acceptance test, then write unit tests. This is better explained as the double loop of TDD:
Some time ago, I found this great step by step explanation of Outside-In TDD, by Sandro Mancuso from Codurance:
However it was all Java and obviously OOP, so I wanted do the same thing but in Clojure, and fully functional: functions and not classes. So here we go:
Restrictions (adapted for functional):
1. Start with a module account that has the following functions:
(defn deposit [account amount] ...)
(defn withdraw [account amount] ...)
(defn print-statement [account] ...)
Threat the methods, as if they cannot return any values!
2. You're not allowed to add any new functions to the module
3. Strings and integeres are to be used for dates and amounts for simplicity
4. Don't worry about spacing in the statement printed at the console
Threat the methods, as if they cannot return any values!
2. You're not allowed to add any new functions to the module
3. Strings and integeres are to be used for dates and amounts for simplicity
4. Don't worry about spacing in the statement printed at the console
Step 1: The acceptance test (the big loop)
we need to take this test, unit it fails for the right reason: the results of the console-print are not what was expected, so we write two modules:
and account.cljs:
and now the test:
Step 2: The first unit test - account module - the business layer (the small loop)
The very first module we should test is account, so we make account_tests. For account, we could test the deposit function. Considering we might have a module which will actually do the depositing/withdrawals in a database or something, that module will be separate and we'll call it transaction-repository. So we could test the interactions between modules like:
We expect that when we invoke deposit from the account module, the add-deposit from the transaction-repository will be invoked.
we make it pass,
and continue with withdraw and later with print-statement for which we'll do a separate module: statement-printer. The final test will be:
and:
and:
Step 3: the unit tests for the "data layer" (the small loop)
Like for the account, we start with a simple test, where we make a deposit and check if all-transactions will return it. We also need to make sure it's on a certain date. We make the test pass, and we move on to withdraw, ending up with:and:
and:
Step 4: Unit testing the statement-printer (the small loop)
Now, we'll go straight to the results, even though I used TDD to get to it:
The tests:
The code obtained:
The tests result:
Let's not forget that our console-print function is still mocked, and the implementation is:
and we can now implement it:
Tests still pass.
Step 5: Going back to the acceptance test (the big loop)
We will modify the test, mocking the dates delivered, as well as console-print:
And when we run all the tests:
Conclusion
Starting from the outside, we could test-drive the design and the code.
acceptance-test ->
account-tests->account->
transaction-respository-tests->transaction-repository->
statement-printer-tests->statement-printer->console
No comments:
Post a Comment