tags:

views:

102

answers:

4

I've read various articles about Mock vs Stub in testing, even the Fowler one.

Also some threads here in the Stackoverflow.

The answers are too difficult and abstract for me to understand.

Could someone explain it in an Apple way? :)

+1  A: 

A Mock is just testing behaviour, making sure certain methods are called. A Stub is a testable version (per say) of a particular object.

What do you mean an Apple way?

kuroutadori
"What do you mean an Apple way?"Use Helvetica
kubi
In an Apple way as opposed to in a Microsoft way :)
never_had_a_name
What is iTesting? iTesting is beautiful, iTesting is thin, you already know how to use it. iTesting is the future, and it's only... just... begun.
Andy
Does this help the situation any?
kuroutadori
+3  A: 

Stub is simple fake object. It just makes sure test runs smoothly.
Mock is smarter stub. You verify Your test passes through it.

Arnis L.
A: 

following is my understanding...

  • if you create test objects locally and feed your local service with that, you are using mock object. this will give a test for the method you implemented in your local service. it is used to verify behaviors

  • when you get the test data from the real service provider, though from a test version of interface and get a test version of the object, you are working with stubs the stub can have logic to accept certain input and give corresponding output to help you perform state verification...

Andy Lin
+1  A: 

I believe the biggest distinction is that a stub you have already written with predetermined behavior. So you would have a class that implements the dependency (abstract class or interface most likely) you are faking for testing purposes and the methods would just be stubbed out with set responses. They wouldn't do anything fancy and you would have already written the stubbed code for it outside of your test.

A mock is something that as part of your test you have to setup with your expectations. A mock is not setup in a predetermined way so you have code that does it in your test. Mocks in a way are determined at runtime since the code that sets the expectations has to run before they do anything.

Tests written with mocks usually follow an initialize -> set expectations -> exercise -> verify pattern to testing. While the pre-written stub would follow an initialize -> exercise -> verify. The purpose of both is to eliminate testing all the dependencies of a class or function so your tests are more focused and simpler in what they are trying to prove.

I hope that helps.

Sean Copenhaver