【问题标题】:How to resolve RSpec's deprecation warning about the new expect syntax?如何解决 RSpec 关于新期望语法的弃用警告?
【发布时间】:2016-02-13 11:57:46
【问题描述】:

当我运行这个 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

这是输出:

弃用警告:

使用来自 rspec-expectations 的旧 :should 语法的 should 没有 显式启用语法已被弃用。使用新的:expect 语法或使用config.expect_with(:rspec) { |c| c.syntax = :should } 显式启用:should。调用自 /home/rails/rails_nimish/Devise_use/spec/features/users_spec.rb:113:in `块(4级)'

如何解决这个警告?

更新:解决方案 我刚换了

page.should have_content "确认密码不匹配"

与:

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

【问题讨论】:

  • 最好在问题正文中输入警告的实际文本。使用 Google 会更容易阅读和查找。
  • 我不想使用 should,因为它是旧语法,我正在寻找一种 expect(page) 语法。

标签: ruby-on-rails rspec capybara rspec-rails rspec3


【解决方案1】:

正如消息所说,您有两个选择:

  • 明确配置 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 2Capybara's have_content matcher waits for you.

【讨论】:

  • 是的,您的第二个选项回答了我的问题。谢谢。
  • 不幸的是,您建议的 gem (transpec) 有未解决的未解决问题,可追溯到 2017 年,例如,似乎未维护
【解决方案2】:

我刚换了:

page.should have_content "Confirm Password Does not Match"

与:

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

【讨论】:

    【解决方案3】:

    如果您决定清除旧语法并且您是 vim 类,您可能会发现以下内容很方便 ;)

    argdo %s/ \([^ ]\+\)\.should == \([^}]\+\)\( }\)\?$/ expect(\1).to eql(\2)\3/gc | update
    argdo %s/should \(be_\w\+\)/expect(subject).to \1/gc | update
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-07
      • 1970-01-01
      • 2022-09-26
      • 2014-08-27
      • 2012-01-31
      • 2010-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多