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*

10
  • 3
    The problem is that as soon as you write myDataStore.containsPerson('Joe') you are assuming the existence of a functional test database. Once you do that, you are writing an integration test and not a unit test. Commented Jun 22, 2016 at 14:06
  • 2
    it does not have to be a real database - just an object that implements the same interface as the real data store (read: it passes the relevant unit tests for the data store interface). I'd still consider this a mock. Let the mock store everything that gets added by any method for doing so in an array and check whether the test data (elements of myPeople) are in the array. IMHO a mock should still have the same observable behavior that is expected from a real object, otherwise you are testing for compliance with the mock interface, not the real interface. Commented Jun 22, 2016 at 16:40
  • 2
    "Let the mock store everything that gets added by any method for doing so in an array and check whether the test data (elements of myPeople) are in the array" -- that's still a "real" database, just an ad-hoc, in-memory one you have built. "IMHO a mock should still have the same observable behavior that is expected from a real object" -- I suppose you can advocate for that, but that's not what "mock" means in the testing literature or in any of the popular libraries I've seen. A mock simply verifies that expected methods are called with expected parameters. Commented Jun 22, 2016 at 17:03
  • 2
    Before calling savePeople, you probably should test that myDataStore does not contain Person('Joe'). Otherwise, if myDataStore is a database that has already been used in other tests, there might be already a Joe. If savePeople somehow stops working, you wouldn't notice it. Commented Sep 27, 2017 at 18:22
  • 1
    @EricDuminil Raises a very important point: myDataStore (whether it is a real database or a fake) must be reset between every unit test. You can't use a real database that might also be used by other processes. Commented Jul 1, 2021 at 8:17