May
23rd
Sat
23rd
why do we call mocks objects as mocks in our tests?
Confusing title… I know. I am good in making such titles :).
This is for people who when they mock something call it as mockXXX. It is a convention with a lot of people even me till a few days back. I moved away from the keboard and saw that the code was having too much of mock naming noise. I started wondering why do we do that and started talking to people. Some of the replies I got and my opinion on them is the root of this post.
- It is a convention - My way is if the convention doesn’t make any sense or add value don’t follow it.
- When I read my test I will know which is mock and which is not - My opinions on this are
- I don’t care if it is a mock or the real object. I just want the behavior I want. As long as the behavioral requirement gets satisfied I don’t think I care. Is there any other reason?
- A good unit test should be short and crisp. So when reading a test it will be easy to spot out mocks if there are any.
- I am not really a dogmatic advocate of anything except being aware of the context and basing decisions on that. If using the real object makes more sense than mock go ahead use it.
Basing on these reasons I don’t feel that it is good to pollute the test code with mockFileReader, mockDataProvider statements. Use meaningful names which make the test readable.
I think I also don’t like to name interfaces as Ixxx. But that is for a different post :)