I've seen it used many times, and never really stopped to question it. Now it's got me wondering if there is a difference between stub and stub!
Is there? Or is it historical? Does stub! mean it stubs it once? and returns back to the normal method call?
In RSpec, a stub is often called a Method Stub, it's a special type of method that “stands in” for an existing method, or for a method that doesn't even exist yet.
Stub: A class or object that implements the methods of the class/object to be faked and returns always what you want. Mock: The same of stub, but it adds some logic that "verifies" when a method is called so you can be sure some implementation is calling that method.
A Test Stub is a fake object that's used in place of a real object for the purpose of getting the program to behave the way we need it to in order to test it.
In both version 2, and v1.3.2, they are simply aliases of each other. In v1.3.2 stub
is an alias of stub!
https://github.com/dchelimsky/rspec/blob/v1.3.2/lib/spec/mocks/methods.rb#L12
While in v2.6 of rspec 2, stub!
is an alias of stub
https://github.com/rspec/rspec-mocks/blob/master/lib/rspec/mocks/methods.rb#L12
However in v1.1.4, stub
is an alias for mock
, and stub!
is a separate method. From the docs, it would seem that stub!
in this early version was used to add methods to existing objects, creating a "partial mock".
The explanation:
https://github.com/dchelimsky/rspec/blob/1.1.4/lib/spec/mocks.rb#L43
The definitions:
https://github.com/dchelimsky/rspec/blob/1.1.4/lib/spec/mocks/spec_methods.rb#L27
https://github.com/dchelimsky/rspec/blob/1.1.4/lib/spec/mocks/methods.rb#L12
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