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