Search code examples
ruby-on-railsrspecrspec-railsruby-on-rails-6

RSpec controller error wrong number of arguments


I'm starting to learn RSpec from a tutorial.

I'm stuck on tests for the controller.

For some reason, I can’t run the next test in my application:

require "rails_helper"

RSpec.describe PostsController, type: :controller do
  context "GET #index" do
    it "returns a success response" do
      get :index
      expect(response).to be_success
    end
  end
end

I get the error:

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

I don’t understand what could be the problem, because in the tutorial it works.


Solution

  • This is an issue with Rails 6 and rspec-rails 3.x gem, which has been fixed in rspec-rails 4.0 version, upgrade your rspec-rails gem version

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

    bundle install and run the specs again.

    More info - https://github.com/rails/rails/issues/35417#issuecomment-475723528