2017-06-15 11 views
0

を廃止されました機能テストで位置引数を使用した: -私は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) 

これは私のコントローラの仕様である: -

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 

はライン上でエラーをスローします13,19。

警告が表示されなくなるようにコードを変更する方法がわかりません。

+0

廃止予定メッセージは非常にはっきりしていますが、正確には分かりませんが、新しいスタイルでコードを書き直そうとしましたか? –

+0

@コード例を見ると、それらはすでにキーワード引数を使用しているので、非推奨メッセージの理由はないようです。 – janfoeh

+0

@janfoeh 'message'のようなものはありません。' params、headers、env、xhr'しかないので、彼は試していないと思います。 –

答えて

1

[OK]を、私はそれは次のように動作しました: -

describe "#create" do 
    it "sends an email when message is valid" do 
     expect{ 
     post :create, params: {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, params: {message: {subject: "", name: "", 
           email:"", content: ""}} 
     }.to change{ ActionMailer::Base.deliveries.count }.by(0) 
    end 
    end 
end 
1

を今後の参考のためにあなたはまた、これらの修正ができRubocopの自動修正機能で:

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

bundle exec rubocop --rails --only HttpPositionalArguments --auto-correct 
(バルク中!)

ルブコプ設定でTargetRailsVersion: 5.0以上を設定して、その警官を有効にすることを忘れないでください。

+0

これは以前は明らかではなかったため、以前は私のために働いていなかった。私は、ローカルの 'spec/.rubocop.yml'ファイル(空であっても)が何らかの形でこの警官を無効にしているか、失敗させていることを発見しました。私はRubocopの問題としてhttps://github.com/bbatsov/rubocop/issues/5576に提出しました。その問題を回避するために、ファイルを削除し、上記の自動修正を実行して、削除したローカル設定をチェックアウトしました。うまくいけば、これは他の誰かを助けるでしょう – Aaron

関連する問題