2017-06-15 42 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

好吧,我得到了它具有以下工作: -

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 
(散裝!)

請記住在Rubocop配置中設置TargetRailsVersion: 5.0或更高,以啓用該警報。

+0

這以前不適用於我,沒有明顯的原因。我發現有一個本地的'spec/.rubocop.yml'文件(即使是空的)以某種方式禁用了該警察或導致其失敗。我已將它作爲問題提交給Rubocop,網址爲https://github.com/bbatsov/rubocop/issues/5576。爲了解決它,我刪除了文件,運行了上面的自動更正,並檢出了我刪除的本地配置。希望這可以幫助別人。 – Aaron