Possible Duplicate:
What is Object Mocking and when do I need it?
You might indeed start writing tests for B and implementing B as soon as you know you need it and then come back to A. But when you come back to A you might want to use a mock for B so that your test for A is really testing A and its interactions with B.
Part of the reason for this is there might be another class C that B uses to do some of its work. If you want to test A using a real B, you'll also need C, and of course the difficulty can extend further.
Using mocks you can make your test check only that A calls B methods when it should and deals properly with the responses that you tell your mock to give, rather than depending on a real implementation of B.
To learn better how to use Mocks and in general TDD I recommend you nat pryce blog and his book http://www.natpryce.com/
Generally speaking it's not a good idea to use too much mocks. Use them when you have to isolate layers of your applications (db and view i.e.) or external (slow) system.
Writing mocks could make your tests hard to read, so it's better not abuse them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With