3 ms·
> How do you deal with tests that end up self-referencing, testing only specific implementation details and adding no additional safety? Generally, by describi
by eyelidlessness 7y ago
> How do you deal with tests that end up self-referencing, testing only specific implementation details and adding no additional safety?
Generally, by describing tests in plain english that express "user" intent (where "user" may be the dev at the call site, or may be an end user) rather than expressing things like return values or state changes.
> In order to add or change a feature, you now have to write the code twice: Once to implement the change, and once to re-implement the change to assert that it does what it's supposed to.
From this description, it doesn't sound like you're doing TDD.
1. Write a failing test (where "test" describes user intent).
2. Make that test pass with an implementation.
3. Make that implementation meet your expectations for code quality, appropriate separation of concerns, appropriate abstraction, DRY etc.
(This is commonly referred to as red-green-refactor.)
> And no safety has been added, because the logic itself can still be flawed. You've just written it twice instead of once.
While you can always get the logic wrong, it's significantly less likely if what you're testing is intent. I really don't understand what you mean by "written it twice".
> Generally I find tests that test the controllers, where multiple units of work are attached together, to be much more useful.
I'm... honestly surprised by that. Those tests tend to be the most brittle, the least likely to cover all scenarios you'd like to test, the most error prone, and the hardest to understand and maintain.
- jniedrauer 7y ago> Those tests tend to be the most brittle While true, it's far more likely that they will help you during a refactor. If you use TDD and have a test for every function, you will more often than not throw entire functions and their tests away. A million tests that test immutable functions are all useless. But tests that fall in between TDD style test-per-function and integration tests, tend to be re-used post refactor, and give certainty that the refactor worked as expected.
- eyelidlessness 7y agoI think the size of a test shouldn't be defined by the size of the functionality you're testing, but rather a unit of "user intent", as expressed at whatever layer you're testing.