Remix.run Logo
9rx 2 days ago

> "mock" - an object that has expectations about how it is to be used. It encodes some test logic itself.*

Something doesn't add up. Your link claims that mock originated from XP/TDD, but mock as you describe here violates the core principles of TDD. It also doesn't fit the general definition of mock, whereas what you described originally does.

Beck seemed to describe a mock as something that:

1. Imitates the real object.

2. Records how it is used.

3. Allows you to assert expectations on it.

#2 and #3 sound much like what is sometimes referred to as a "spy". This does not speak to the test logic being in the object itself. But spies do not satisfy #1. So it is seems clear that what Beck was thinking of is more like, say, an in-memory database implementation where it:

1. Behaves like a storage-backed database.

2. Records changes in state. (e.g. update record)

3. Allows you to make assertions on that change in state. (e.g. fetch record and assert it has changed)

I'm quite sure Fowler's got it wrong here. He admits to being wrong about it before, so the odds are that he still is. The compounding evidence is not in his favour.

Certainly if anyone used what you call a mock in their code you'd mock (as in make fun of) them for doing so. It is not a good idea. But I'm not sure that equates to the pattern itself also being called a mock.