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

3

12

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("[email protected]")
    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 '

enter image description here

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"
Demetri answered 13/2, 2016 at 11:57 Comment(2)
It would be better to enter the actual text of the warning in the body of your question. It would be easier to read and to find with Google.Highlander
I don't want to use should, because it is old syntax and I was looking for an expect(page) syntax.Demetri
H
16

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 test 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.

Highlander answered 13/2, 2016 at 13:21 Comment(2)
yes, your second option answered my question. Thank you.Demetri
unfortunately, the gem you suggested (transpec) has unanswered unresolved Issues going back to 2017, eg, appears to be unmaintainedFriesland
D
6

I just replaced:

page.should have_content "Confirm Password Does not Match"

with:

expect(page).to have_content "Confirm Password Does not Match"
Demetri answered 5/3, 2016 at 10:44 Comment(0)
B
0

If you decide to clean out the old syntax and you're a vim kind, you may find the following handy ;)

argdo %s/ \([^ ]\+\)\.should == \([^}]\+\)\( }\)\?$/ expect(\1).to eql(\2)\3/gc | update
argdo %s/should \(be_\w\+\)/expect(subject).to \1/gc | update
Brophy answered 16/1, 2020 at 0:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.