Obromios
Obromios

Reputation: 16393

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?

Upvotes: 3

Views: 2735

Answers (1)

Obromios
Obromios

Reputation: 16393

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.

Upvotes: 3

Related Questions