Test Driven Development in Ruby tuts+ course - Stubs and Mocks notes
Stubbing - A technique used in TDD/BDD allowing you to simulate using third party dependencies for your own code. #
Example, retrieving data from a database, you pass a “splat” = dummy data to a stub.
We have no control over what our data model is; as an example for the usage of stubbing.
pass a stub to a list for fake data
before { competition.stub(:questions => [stub] ) }
Mocking - A technique used to allow you to assert messages; specifically method calls without actually implementing any code for that method. #
Example, a competition class should be able to receive and react to a competition.close method once it has started. However we may assume that the close method logic is not going to be written or implemented by us truly. In real life situations, simulate API calls and pretend that we have data received from such a third party method call.
context:“when started” do
it “is closed” do
competition.should_receive(:close)
competition.start
end
end