10

I have a brand new rails 6 application, and I installed rspec.

I created a controller, and when I run rspec I get this error:

PagesController GET #index returns http success
     Failure/Error: get :index

     ActionView::Template::Error:
       wrong number of arguments (given 2, expected 1)
     # ./spec/controllers/pages_controller_spec.rb:7:in `block (3 levels) in <top (required)>'
     # ------------------
     # --- Caused by: ---
     # ArgumentError:
     #   wrong number of arguments (given 2, expected 1)
     #   ./spec/controllers/pages_controller_spec.rb:7:in `block (3 levels) in <top (required)>'

The generated rspec test looks like:

require 'rails_helper'

RSpec.describe CartController, type: :controller do

  describe "GET #index" do
    it "returns http success" do
      get :index
      expect(response).to have_http_status(:success)
    end
  end

end

Is there a bug in the generated code or is some other environmental issue the cause of this?

I don't see where there are 2 arguments anywhere?

1
  • 1
    Don't add controller specs to a new app. "Request specs provide a high-level alternative to controller specs. In fact, as of RSpec 3.5, both the Rails and RSpec teams discourage directly testing controllers in favor of functional tests like request specs." Commented Oct 29, 2019 at 13:34

1 Answer 1

12

Per the following GitHub issue for Rails 6, upgrade to rspec-rails 4.0:

gem 'rspec-rails', '~> 4.0.0.beta3'

This is also referenced in this GitHub issue for rspec-rails.

Update from July 2021

This question generates pretty regular traffic and rspec-rails is now well past the 4.0.0beta3 version. (v5.0.1 as of today) Always check for the latest version at https://rubygems.org/gems/rspec-rails and use that instead:

gem 'rspec-rails', '~> 5.0.1'
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for keeping this up to date. The upgrade to Ruby 3 will hit this a lot.
I am using ruby 3 but still adding 'rspec-rails', '~> 5.0.1' doesn't solve the issue. @anothermh can you help with that?
Ruby 2.7 Rails 5.2 rspec-rails 5.1.1 and I just started seeing this error today - tests have not changed recently

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.