4 ms·
OK, you caught me, give bar an argument. (I'll edit the gist) Why should mocking only be used in 'extreme circumstances'? I want to test what bar does, and I
by sorahn 8y ago
OK, you caught me, give bar an argument. (I'll edit the gist)
Why should mocking only be used in 'extreme circumstances'? I want to test what bar does, and I don't care what baz does, and if someone breaks baz, my unit tests for bar shouldn't fail, because it is doing its job.
I would mock it if it was calling some function in another module, so what's the difference if it's calling another function in the class?
- yorwba 8y agoMocking a component means that you now have two places where that component's behavior is specified and they can diverge. To prevent that, you'll need an integration test where the components interact directly. Just not using a mock is enough to get such an integration test. Then the value of the original, mocked unit test is questionable. It only provides additional information in the event that the mock differs from the actual component. If that's unintentional, then either the component is wrong (which should be caught by the tests for that component) or the mock is wrong. In either case, the mocked test provides little or negative value. Then the remaining case, where mocking is actually useful, is when the mock intentionally shows different behavior. Mocking a slow computation to return the result instantly. Deliberately failing, to test error-handling code. Simulating unlikely events in general. Those are good uses of mocking. TL;DR: Write more integration tests instead of unit tests with mocking.
- sorahn 8y ago> TL;DR: Write more integration tests instead of unit tests with mocking. Interesting. I will investigate what that looks like at work tomorrow. Thanks!