Fake repositories #13
-
Hi Valentina, To test the use cases you said that it is better to create fake repositories. That's clear, and i understand why; but i am not sure of a thing when talking about in-memory database. I have a repository that call a dao for the in-memory db (and other types of db). I implemented a sort for the results from this db. So to test my use case, i made a fake repository. The problem is then that i have to re-implement the sort algorithm of the dao in my fake repo. Which create duplication. What do you think about that? Maybe that's the case where we have to add tests outside the use cases? But it feels to me that the use case is not fully tested. Thank you :) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
Hi Jérémy :) As you pointed out, it is useless to replicate the sorting algorithm because the database do it well for you. Here, you want to be able to configure your fake repository with "stub results" given a pre defined order. You'll need to assert on the received params too. Basically you'll want to expose the contract of the sorting algorithm in your repository interface. That the sort parameters you're talking about. Then, you'll eventually need a focused integration test to test the real adapter implementation (thus relying this time on the real db sorting algorithm) |
Beta Was this translation helpful? Give feedback.
-
@yereby, good question. My presentation showed only the usage of Fakes, but the wider list is: Dummies, Stubs, Spies, Mocks, Fakes. https://martinfowler.com/bliki/TestDouble.html
From Uncle Bob's article https://blog.cleancoder.com/uncle-bob/2014/05/14/TheLittleMocker.html
For this reason, when using Fakes, we have to write contract tests for the Fake (but don't write contract tests for spies) @PCreations, as far as I know, the example you mentioned above is an example for Stub approach, because the method is returning a precanned response, and not doing any logic (no sort logic). Overall, yes, this is a common approach. Difference Perspectives These are the preferences I've seen:
I also had a LinkedIn post regarding various thoughts on Test Doubles https://www.linkedin.com/posts/valentinacupac_tdd-testdrivendevelopment-unittesting-activity-6898896081627738112-TVsT My personal journey:
|
Beta Was this translation helpful? Give feedback.
@yereby, good question. My presentation showed only the usage of Fakes, but the wider list is: Dummies, Stubs, Spies, Mocks, Fakes. https://martinfowler.com/bliki/TestDouble.html
Stubs provide canned responses. So, for example, a StubRepository may always return John, Mary, Mike (these could be hard-coded in the StubRepository or it could be configurable, i.e. passing these in the constructor). There is NO logic within the StubRepository, so there's a method getCustomersOrderedByFirstName(), the Stub would NOT do any sorting, the Stub returns just a canned response.
Fakes have a working implementation, simulating the real adapter except they have to run in-memory. So for example, impl…