3 ms·
"having test play some part in how the code takes shape" That is exactly the point why I don't like TDD. I mean, there are enough constraints that shape the c
by k__ 9y ago
"having test play some part in how the code takes shape"
That is exactly the point why I don't like TDD.
I mean, there are enough constraints that shape the code, why should something artificial like tests shape it too?
With mocking and everything you end up writing code for tests and not code for your problems.
- testvox 9y agoIf you use mocks extensively in your tests then you probably aren't getting much of the benefit of TDD. Mocks indicate high coupling so using them is just powering though testing your bad design rather then driving good design via testing.
- efdee 9y agoHow do you handle dependencies of the class under test then?
- scruple 9y agoWould love an answer to this question, as well. I've only come up with DI as a long-term mostly win for this problem and it's something that I grapple with in every code base at work, since we have some microservices and other HTTP API layers that we need to interface with and I want to write testable code.
- rasmus4200 9y agoThis is the biggest challenge. What I find works is separating your orchestrations from your data processing. For example, instead of creating one class that does the network call, and processes the JSON response, break these out. One class does the network call (keep it extremely thin). Dependency inject stuff in if you want. But view this purely as an orchestration. if (connectionValid) json = makeNetworkCall anotherObject.processJson(json) <- test separately end Then, in the other plain old data process class, do you heavy off-by-one edge case testing there. If the first case, the orchestration, I can write tests like "One make the network call if the connection is valid". But that's all that test would do. I am at the point now where I actually don't even test that. I assume that is going to work, because it is so simple. That and I generally hate mocking. Way too much coupling. Way too hard to refactor. Where I do test heavy is on the plain old object side, that has no outside ports or connectors. These are just plain old objects. I agree with some other comments I have ready here. Mocking is a bitch and I generally don't like it. But it's there if I needed it, so I do use it occasionally. But it has so many downsides I try not to. Instead I separate the orchestrations from the processing. Unit test the processing (in an integration test sort of way), and try to stick to testing the public APIs of my class, so I am free to change the internals without things breaking. My 2c. Hope that helps.
- ISL 9y agoBecause tests tend to make your code do what you originally intended, and keep your attention on the quality of the result.
- humanrebar 9y agoWell, you write code for design flexibility. Testing just forces the issue and helps you explore possible design needs. For example, what if the database query you're using isn't suitable any more? Instead of designing around a User table, you start passing around User records. Then your design is more future proof in case you start getting Users from a service or in-memory cache instead of a database. > ...not code for your problems... Right. It's not your problem now. It may be your (or your successors') problem in the future. Is it always worth the effort? Nope. But experience, communication, and good teamwork will help with balancing short-term and long-term goals.
- discreteevent 9y agoThis is exactly the problem. In general you should not be writing code for design flexibility. Your code should contain the minimum number of abstractions to satisfy its requirements. If flexibility is a requirement right now then that's ok. Otherwise refactor in the flexibility later. But don't make the code flexible for the sake of tests. It vastly over-complicates it for no benefit. Instead write tests as close to the requirements level as possible (ideally most of your tests should be just below the ui)
- humanrebar 9y ago> Otherwise refactor in the flexibility later. I'm saying, with experience, teamwork, communication (including through tests), you'll know when this proposition makes sense or not. It's not universally true that "we'll worry about it later" make sense.
- deleted 9y ago[deleted]
- deleted 9y ago[deleted]
- deleted 9y ago[deleted]
- vinaybn 9y agoTests are not artificial constraints. They are the guarantees which (should) matter most to your users and stakeholders.
- efdee 9y agoI am quite convinced stakeholders couldn't really care less about the guarantees being made by unit tests. Higher level integration tests, maybe.
- sidlls 9y agoIf you're writing lots of mocks, odds are you're writing a lot of unit tests. In my experience these are rarely useful. Most often they are either really testing a compiler's output (indirectly), supplying an ad-hoc and incomplete type checking system to a dynamic language, or more appropriately covered by asserts that will scream when integration tests fail.
- MartinCron 9y agoI don't consider taking steps to make sure the code actually as you expect it to as "artificial" feels pretty essential, to me.
- tieTYT 9y agoI'm not hearing anything negative here. Why is any of this innately bad? Tests shaping your code is bad, because? Because you already have "enough constraints"? Sorry, I don't get it.