如何解决 RSpec 关于新 expect 语法的弃用警告?
How to resolve RSpec's deprecation warning about the new expect syntax?
当我 运行 这个 RSpec 示例时,它通过了,但我收到了弃用警告。
context "should have valid detail for signup" do
it "should give error for invalid confirm password" do
find("#usernamesignup").set("Any_name")
find("#mobile_number").set("1234567890")
find("#emailsignup").set("mymail@yopmail.com")
find("#passwordsignup").set("12345678")
find("#passwordsignup_confirm").set("")
find(".signin").click
sleep 2
page.should have_content "Confirm Password Does not Match"
end
end
这是输出:
弃用警告:
Using should
from rspec-expectations' old :should
syntax without
explicitly enabling the syntax is deprecated. Use the new :expect
syntax or explicitly enable :should
with config.expect_with(:rspec)
{ |c| c.syntax = :should }
instead. Called from
/home/rails/rails_nimish/Devise_use/spec/features/users_spec.rb:113:in
`block (4 levels) in '
如何解决这个警告?
更新:解决方案
我刚刚更换了
page.should have_content "Confirm Password Does not Match"
与:
expect(page).to have_content "Confirm Password Does not Match"
如消息所述,您有两个选择:
明确配置 RSpec 以允许 .should
。不要使用那个选项; .should
已弃用,将来可能不会得到很好的支持。但是,如果你真的想允许 .should
,你可以通过将它添加到你的 spec_helper.rb 来实现(或者编辑可能已经存在的 rspec-mocks 的示例配置):
RSpec.configure do |config|
config.expect_with :rspec do |expectations|
expectations.syntax = :should
end
end
如果您想同时使用 expect
和 .should
,请设置
expectations.syntax = [:expect, :should]
但不要那样做,选择一个并在你的测试套件中的任何地方使用它。
将您的期望改写为
expect(page).to have_content "Confirm Password Does not Match"
如果你有很多语法需要升级的规范,你可以使用the transpec gem自动完成。
旁注:不要sleep 2
超出您的预期。 Capybara's have_content
matcher waits for you.
我刚刚更换:
page.should have_content "Confirm Password Does not Match"
与:
expect(page).to have_content "Confirm Password Does not Match"
如果您决定清除旧语法并且您属于 vim 类型,您可能会发现以下内容很方便 ;)
argdo %s/ \([^ ]\+\)\.should == \([^}]\+\)\( }\)\?$/ expect().to eql()/gc | update
argdo %s/should \(be_\w\+\)/expect(subject).to /gc | update
当我 运行 这个 RSpec 示例时,它通过了,但我收到了弃用警告。
context "should have valid detail for signup" do
it "should give error for invalid confirm password" do
find("#usernamesignup").set("Any_name")
find("#mobile_number").set("1234567890")
find("#emailsignup").set("mymail@yopmail.com")
find("#passwordsignup").set("12345678")
find("#passwordsignup_confirm").set("")
find(".signin").click
sleep 2
page.should have_content "Confirm Password Does not Match"
end
end
这是输出:
弃用警告:
Using
should
from rspec-expectations' old:should
syntax without explicitly enabling the syntax is deprecated. Use the new:expect
syntax or explicitly enable:should
withconfig.expect_with(:rspec) { |c| c.syntax = :should }
instead. Called from /home/rails/rails_nimish/Devise_use/spec/features/users_spec.rb:113:in `block (4 levels) in '
如何解决这个警告?
更新:解决方案 我刚刚更换了
page.should have_content "Confirm Password Does not Match"
与:
expect(page).to have_content "Confirm Password Does not Match"
如消息所述,您有两个选择:
明确配置 RSpec 以允许
.should
。不要使用那个选项;.should
已弃用,将来可能不会得到很好的支持。但是,如果你真的想允许.should
,你可以通过将它添加到你的 spec_helper.rb 来实现(或者编辑可能已经存在的 rspec-mocks 的示例配置):RSpec.configure do |config| config.expect_with :rspec do |expectations| expectations.syntax = :should end end
如果您想同时使用
expect
和.should
,请设置expectations.syntax = [:expect, :should]
但不要那样做,选择一个并在你的测试套件中的任何地方使用它。
将您的期望改写为
expect(page).to have_content "Confirm Password Does not Match"
如果你有很多语法需要升级的规范,你可以使用the transpec gem自动完成。
旁注:不要sleep 2
超出您的预期。 Capybara's have_content
matcher waits for you.
我刚刚更换:
page.should have_content "Confirm Password Does not Match"
与:
expect(page).to have_content "Confirm Password Does not Match"
如果您决定清除旧语法并且您属于 vim 类型,您可能会发现以下内容很方便 ;)
argdo %s/ \([^ ]\+\)\.should == \([^}]\+\)\( }\)\?$/ expect().to eql()/gc | update
argdo %s/should \(be_\w\+\)/expect(subject).to /gc | update