Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using positional arguments in functional tests has been deprecated

I'm getting this warning when I run rspec:-

DEPRECATION WARNING: Using positional arguments in functional tests has been deprecated,
in favor of keyword arguments, and will be removed in Rails 5.1.

Deprecated style:
get :show, { id: 1 }, nil, { notice: "This is a flash message" }

New keyword style:
get :show, params: { id: 1 }, flash: { notice: "This is a flash message" },
  session: nil # Can safely be omitted.
 (called from block (4 levels) in <top (required)> at /home/user/organization/fooobarr/spec/controllers/contacts_controller_spec.rb:13)

This is my controller spec:-

require 'rails_helper'

RSpec.describe ContactsController, :type => :controller do

  describe "#create" do
    it "sends an email when message is valid" do
      expect{
        post :create, message: attributes_for(:message)
      }.to change{ ActionMailer::Base.deliveries.count }.by(1)
    end

    it "does not send email when message is invalid" do
      expect{
        post :create, message: {subject: "", name: "",
                                email:"", content: ""}
      }.to change{ ActionMailer::Base.deliveries.count }.by(0)
    end
  end
end

Throws the error on line 13 and 19.

I'm not sure how to change the code I have so that the warning no longer appears.

like image 324
Ragav Y Avatar asked Nov 27 '22 02:11

Ragav Y


2 Answers

For future reference you can also fix these (in bulk!) with Rubocop's Autofix feature:

http://rubocop.readthedocs.io/en/latest/cops_rails/#railshttppositionalarguments

bundle exec rubocop --rails --only HttpPositionalArguments --auto-correct

Remember to set TargetRailsVersion: 5.0 or higher in the Rubocop config, to enable that cop.

like image 150
samjewell Avatar answered Nov 29 '22 16:11

samjewell


Thanks @samjewell for the hint. it seems this syntax is now outdated, I could not use the --rails parameters.

Here is the updated way to achieve this today

in your Gemfile, use gem 'rubocop-rails' instead of rubocop.

in your .rubocop.yml add the require and relevant Rails version

require: rubocop-rails
Rails:
  Enabled: true
AllCops:
   TargetRubyVersion: 2.4
   TargetRailsVersion: 5.0

Then run

bundle exec rubocop --only Rails/HttpPositionalArguments -a

to fix those deprecation warnings automatically

Hope this helps someone.

like image 20
Mathieu J. Avatar answered Nov 29 '22 17:11

Mathieu J.