Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generated RSpec controller test is failing with wrong number of arguments (given 2, expected 1)

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?

like image 819
Blankman Avatar asked Oct 27 '19 16:10

Blankman


1 Answers

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'
like image 114
anothermh Avatar answered Nov 15 '22 11:11

anothermh