4 ms·
I agree that this usage of mocks is detrimental. I think the only verification that should be done with mocks (the verify method) is that important collaborato
by e28eta 13y ago
I agree that this usage of mocks is detrimental.
I think the only verification that should be done with mocks (the verify method) is that important collaborators have been called. Things that run off and mutate some global state, or perform an action.
Stubs are useful for controlling the execution state of the method under test. You could stub DateTime such that it was midnight in one test, and noon in another, if the behavior of the function relied on that. It does introduce a dependency that has to be changed along with the code, but is often helpful.
I do agree with the author that there's an art to testing appropriately. You could mechanically write testing code for every line of code under test (if statement with single condition? Two tests to verify each path is taken!), which leads to your app being coded twice: once in code and once (or more) in the tests.
- revscat 13y ago> It does introduce a dependency that has to be changed along with the code, but is often helpful. And that's the rub. Mocks are useful. They help you write tests that are more robust, without having to resort to massive and complex @Befores. At the same time, though, they have an effect that is more negative than positive on the ability to change the code under test.
- narrator 13y agoThe only thing mocks are good for IMHO is when the real code takes too long to run. Actually testing that a function is called twice is not what you're testing and this level of coupling to the supposedly encapsulated behavior of the method is ridiculous. The method is not called "callMethodInClassFooTwice()" it's called calculateInterestRate() or whatever and as long as it does that right that's all that's important.
- lostcolony 13y agoCalls to external services and hardware, or really, any external system. It's probably quite important that your code interacts properly with the system, but you also probably don't want to interact with it every time you run the tests. Not unless you have a copy of the external system running for every developer.