mocking in python
TRANSCRIPT
Test Double Taxonomy 101
Dummy - A placeholder object, unused and unusable
Fake - A working implementation that takes shortcuts
Stub - Provides default canned answers to calls
Mock - A simulated object that mimics behavior
http://martinfowler.com/articles/mocksArentStubs.html
What can mocking help to test?
Did the client code pass the parameters we expected?
Does the client code make the correct calls?
Were the calls made in the correct order?
Were the calls made the right number of times?
test driver to mock cook: expect a hot dog order and give him this dummy hot dog in response
test driver (posing as customer) to waiter: I would like a hot dog please
waiter to mock cook: 1 hot dog please
mock cook to waiter: order up! 1 hot dog ready (gives dummy hot dog to waiter)
waiter to test driver: here is your hot dog (gives dummy hot dog to test driver)
test driver: TEST SUCCEEDED!
http://stackoverflow.com/questions/3622455/what-is-the-purpose-of-mock-objects
test driver to mock cook: expect a hot dog order and give him this dummy hot dog in response
test driver (posing as customer) to waiter: I would like a hot dog please
waiter to mock cook: 1 hamburger please
mock cook stops the test: I was told to expect a hot dog order!
test driver notes the problem: TEST FAILED! - the waiter changed the order
http://stackoverflow.com/questions/3622455/what-is-the-purpose-of-mock-objects
When should I use a Mock?The real object is slowThe real object is difficult to setupThe real object has nondeterministic behaviorThe real object does not yet existBut most importantly…The real object is NOT what you are testing