Raw*_*ode 0 rspec ruby-on-rails
我运行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)
Run Code Online (Sandbox Code Playgroud)
这是我的控制器规格: -
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
Run Code Online (Sandbox Code Playgroud)
在第13行和第19行引发错误.
我不知道如何更改我的代码,以便不再出现警告.
Mat*_* J. 12
感谢@samjewell 的提示。看来这种语法现在已经过时了,我无法使用 --rails 参数。
这是今天实现这一目标的更新方法
在您的 Gemfile 中,使用gem 'rubocop-rails'代替rubocop.
在您.rubocop.yml添加需要和相关的 Rails 版本
require: rubocop-rails
Rails:
Enabled: true
AllCops:
TargetRubyVersion: 2.4
TargetRailsVersion: 5.0
Run Code Online (Sandbox Code Playgroud)
然后运行
bundle exec rubocop --only Rails/HttpPositionalArguments -a
自动修复这些弃用警告
希望这可以帮助某人。
sam*_*ell 11
为了将来参考,您还可以使用Rubocop的Autofix功能修复这些(批量!):
http://rubocop.readthedocs.io/en/latest/cops_rails/#railshttppositionalarguments
bundle exec rubocop --rails --only HttpPositionalArguments --auto-correct
Run Code Online (Sandbox Code Playgroud)
请记住TargetRailsVersion: 5.0在Rubocop配置中设置或更高,以启用该警察.
好的,我让它与以下内容一起工作:-
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1221 次 |
| 最近记录: |