Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ruby

rspec

rspec2

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?

like image 650
BlueFish Avatar asked Sep 18 '11 23:09

BlueFish


People also ask

What is stub in RSpec?

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.

What is the difference between stubs and mocks in Ruby testing?

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.

What is stubbing in testing rails?

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.


1 Answers

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

like image 72
numbers1311407 Avatar answered Nov 04 '22 08:11

numbers1311407