How to resolve RSpec's deprecation warning about the new expect syntax?

六眼飞鱼酱① 提交于 2020-01-03 10:18:29

问题


When I run this RSpec example, it passes but I'm getting a deprecation warning.

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

Here is the output:

Deprecation Warnings:

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 '

How to resolve this warning?

Update:solution I just replaced

page.should have_content "Confirm Password Does not Match"

with:

expect(page).to have_content "Confirm Password Does not Match"

回答1:


As the message says you have two options:

  • Explicitly configure RSpec to allow .should. Don't use that option; .should is deprecated and will likely not be as well supported in the future. If you really wanted to allow .should, though, you could do it by adding this to your spec_helper.rb (or editing the example configuration of rspec-mocks that is probably already there):

    RSpec.configure do |config|
      config.expect_with :rspec do |expectations|
        expectations.syntax = :should
      end
    end
    

    If you want to use both expect and .should, set

    expectations.syntax = [:expect, :should]
    

    But don't do that, pick one and use it everywhere in your tests suite.

  • Rewrite your expectation to

    expect(page).to have_content "Confirm Password Does not Match"
    

    If you have many specs whose syntax needs upgrading, you can use the transpec gem to do it automatically.

Side note: Don't sleep 2 before your expectation. Capybara's have_content matcher waits for you.




回答2:


I just replaced:

page.should have_content "Confirm Password Does not Match"

with:

expect(page).to have_content "Confirm Password Does not Match"


来源:https://stackoverflow.com/questions/35379535/how-to-resolve-rspecs-deprecation-warning-about-the-new-expect-syntax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!