Poetry of Programming

Its about Ruby on Rails – Kiran Soumya


Difference between Mocks and Stubs

  • Many developers confuse the ideas behind stubbing and mocking. Stubbing simply replaces a real-world implementation with a simpler implementation. A stub can replace full login system with a simple substitute. A stub’s job is to simulate the real world. Mocks are not stubs. A mock object, instead, is like a gauge that measures the way your application uses an interface.
  • Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what’s programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it ‘sent’, or maybe only how many messages it ‘sent’.
  • Mocks are what we are talking about here: objects pre-programmed with expectations which form a specification of the calls they are expected to receive.

Rspec Definitions for Mock and Stubs:Mocks are objects that allow you to set and verify expectations. They are very useful for specifying how the subject of the spec interacts with its collaborators. This approach is widely known as “interaction testing”.Mocks are also very powerful (though less widely understood) as a design tool. As you are driving the implementation of a given class, Mocks provide an amorphous collaborator that can change in behaviour as quickly as you can write an expectation in your spec. These changes are tantamount to designing the interface to a collaborator that often does not yet exist. As the shape of the class being specified becomes more clear, so do the requirements for its collaborators – often leading to the discovery of new types that are needed in your system.
Stubs are objects that allow you to set “stub” responses to messages. Stubs provide canned responses to messages they might receive in a test, while mocks allow you to specify and, subsquently, verify that certain messages should be received during the execution of a test.

Leave a Reply

Your email address will not be published. Required fields are marked *