Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rspec 3 - stub a class method

I am upgrading from rspec 2.99 to rspec 3.0.3 and have converted instance methods to use allow_any_instance_of, but haven't figured out how to stub a class method. I have code like this:

module MyMod   class Utils     def self.find_x(myarg)       # Stuff     end   end end 

and my rspec 2 test does this:

MyMod::Utils.stub(:find_x).and_return({something: 'testing'}) 

What is the Rspec 3 way of doing this?

like image 267
Peter Sankauskas Avatar asked Jul 31 '14 18:07

Peter Sankauskas


1 Answers

You should do

allow(MyMod::Utils).to receive(:find_x).and_return({something: 'testing'}) 

Check out the doco Method stubs.

like image 163
Arup Rakshit Avatar answered Sep 30 '22 19:09

Arup Rakshit