BlueFish
BlueFish

Reputation: 5145

In RSpec, what is the difference between stub! and stub?

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?

Upvotes: 10

Views: 1598

Answers (1)

numbers1311407
numbers1311407

Reputation: 34072

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

Upvotes: 13

Related Questions