|
|
|
|
|
by mikekchar
3669 days ago
|
|
Generally speaking, with a mock you are not providing an implementation. You are simply making an assertion that something is called in a certain way. This is great for testing adaptors, for instance. Basically you are saying, "I expect to interact with another outside object in this way". Stubs return canned data. This is great for testing that the calculations in your unit are correct without having to use the real collaborators. With stubs you are saying, "I expect my unit to produce this output from this input". Fakes are objects that provide a working interface for your unit and often provide either stubs or mocks. It is not quite correct (IMHO) to say that a fake can be a stub or it can be a mock. It may also be used when you have highly coupled code and you just need the object that does intelligent things so that you can get to the meat of your test. This is a very common technique when you are trying to inject tests into legacy code. With a fake you are saying "I need to use a working collaborator, but I don't want to use the real collaborator for some reason" (usually because it is slow/dangerous/hard to create). Like I said, fakes often contain/return stubs or mocks. A good example of a fake would be a fake that implemented a REST interface and returned canned data depending on what parameters you sent it. It would replace the exact same object with a REST interface that would be used to talk to the network. |
|