Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rspec stub giving wrong number of arguments error

I am upgrading from rails 3.2.19 to rails 4.1.5. I am using rspec-rails 2.14.0.rc1.

With rails 4.1.5 all my tests are passing, except for a handful that use stub. The ones that are failing are of the form:

ENV.stub(:[]).with("ADWORDS_RUN").and_return("Yes")
Rails.stub(env: ActiveSupport::StringInquirer.new("production"))
Kernel.stub(:rand).and_return(2)

Each is returning ArgumentError: wrong number of arguments (1 for 2+). All were passing in rails 3.2.19. I have tried going back to rspec-rails 2.8.1, but same error. Also rails 4.0, but the error persists. The last error (stubbing :rand) does not occur when I run the whole test suite but does occur when I run the individual test file for that test. Here is an example test

it "should have google tracking code in production" do
  Rails.stub(env: ActiveSupport::StringInquirer.new("production"))
  get :home
  response.body.should =~ /Google Analytics Tracking code/
end

and here is the output from the test:

 Failure/Error: Rails.stub(env: ActiveSupport::StringInquirer.new("production"))
 ArgumentError:
   wrong number of arguments (1 for 2+)
 # ./spec/controllers/pages_controller_spec.rb:107:in `block (4 levels) in <top (required)>'

Line 107 is the Rails.stub line.

Please let me know how to rectify this problem?

like image 791
Obromios Avatar asked Jan 20 '26 11:01

Obromios


1 Answers

I did not manage to find the cause of this problem, but I did find a workaround. It was to change all the stub statements to the newer rspec allow syntax. So for example

 Rails.stub(env: ActiveSupport::StringInquirer.new("production"))

becomes

allow(Rails).to receive(:env).and_return(ActiveSupport::StringInquirer.new("production"))

I did this using rspec-rails 2.14.0.rc1. This fixed the problem.

Thank you for the comments, they either clarified my question or pointed me in the right direction.

like image 130
Obromios Avatar answered Jan 22 '26 11:01

Obromios



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!