I want to write a class A
that has a method calculate(<params>)
. That method should calculate a value using database data. So I wrote a class Test_A
for unit testing (TDD). The database access is done using another class which I have mocked with a class, let's call it Accessor_Mockup
.
Now, the TDD cycle requires me to add a test that fails and make the simplest changes to A
so that the test passes. So I add data to Accessor_Mockup
and call A.calculate
with appropriate parameters.
But why should A
use the accessor class at all? It would be simpler (!) if the class just "knows" the values it could retrieve from the database. For every test I write I could introduce such a new value (or an if-branch or whatever).
But wait ... TDD is more. There is the refactoring part. I could refactor class A using the DB accessor. But I could refactor it in any other way as well (e.g. introducing new classes that encapsulate the data). So, again: How do I write tests that force A to use the database (and nothing else)?