Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

11
  • 8
    I believe the point of mocking dependencies is to ensure that if the test fails, only the subject of the test could be the cause for the failure. If you're testing class A and using a real instance of B instead of mocking IB, if B has a bug in it, your A tests will fail. That's why I mock everything, until you reach integration tests, where you want to test all your functionality together. Commented Oct 17, 2016 at 13:28
  • 4
    There's only one problem with that argument, @Callum. When you're writing mock-heavy unit tests and they fail, what's the first thing you suspect? You blame the mock first, then the UUT. Having multiple causes of failure is unavoidable. Mocks aren't chosen because they are magically error-free. They are chosen because they break the dependency chain, are easier to introspect, and sometimes execute faster. Commented Oct 17, 2016 at 17:53
  • @David thanks for your answer. In response to your answer, I have updated my question to be more clear. When I said mocking, I meant all kinds of mocking/stubbing, as compared to using real implementation. Commented Oct 18, 2016 at 1:38
  • 4
    @David I have read the 2 links mentioned in your answer: the first article, actually is actually pointing to another article which is the current approach that I'm using (docs.google.com/file/d/0B_zwMmx15DL8NDdVT2gxLWk1OHc/edit?pli=1). In the second article, the author admits he used integration test to avoid using mock. That leads me back to your conclusion on definition of unit test. To me, a unit test should test smallest possible unit of code, which in a OO project, is normally a method or a class. A unit test that tests nearly the whole system is beyond my imagination Commented Oct 18, 2016 at 1:41
  • 1
    @DavidArno The point of unit tests is to test an individual piece of code, if there's a bug in B your unit tests for B should find them. If there's a bug in B and your tests in A find them, that's going to cause confusion and false negatives. The key to unit tests is that they test very specific things, and are free from outside influences, if you allow A to use B in tests you're muddying the waters, it's better to have individual tests for each class, so that you can easily identify the reason for failure, and come up with a solution. Commented Oct 19, 2016 at 11:15