当我运行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 :(得分:9)
为了将来参考,您还可以使用Rubocop的Autofix功能修复这些(批量!):
http://rubocop.readthedocs.io/en/latest/cops_rails/#railshttppositionalarguments
bundle exec rubocop --rails --only HttpPositionalArguments --auto-correct
请记住在Rubocop配置中设置TargetRailsVersion: 5.0
或更高版本,以启用该警察。
答案 1 :(得分: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
答案 2 :(得分:1)
感谢@samjewell的提示。 看来这种语法现在已经过时了,我无法使用--rails参数。
这是今天实现这一目标的最新方法
在您的Gemfile中,使用gem 'rubocop-rails'
代替rubocop
。
在您的.rubocop.yml
中添加require和相关的Rails版本
require: rubocop-rails
Rails:
Enabled: true
AllCops:
TargetRubyVersion: 2.4
TargetRailsVersion: 5.0
然后运行
bundle exec rubocop --only Rails/HttpPositionalArguments -a
自动修复这些弃用警告
希望这对某人有帮助。