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.

Required fields*

4
  • I have updated my question to be clearer. Your first point is actually what I want to ask: some people might say testing that the class is calling the other object specific number of times might be going too much into internal implementation details of original class. So how do you differentiate what is implementation details and what is dependency that needs testing Commented Oct 18, 2016 at 1:50
  • 2
    I'd add the case where you want to ensure the tested code can handle cases that are hard or complex to induce without mocking. For example; How does my class handle an out of memory exception thrown by its dependency without actually forcing the dependency to run out of memory? Commented Oct 18, 2016 at 4:58
  • 1
    @axl Exactly, it's about having 100% control over the dependencies being passed in, and what they're outputting, without having to mess up your tests with setup / configuration / manipulation code required for using concrete instances. Commented Oct 18, 2016 at 9:21
  • @PhuongNguyen : A unit test is there to ensure the unit of code is behaving the way it is supposed to. The number of calls to another object may be important. For example, you may have built a function that is calling an IO component that you've also built, and you'd like to know that the function does so optimally. Then again, it may not be important to know how often the method calls some transformational helper function. It's a judgement call you have to make on a case by case basis. Commented Oct 18, 2016 at 16:11