【问题标题】:Compound expectations stopped working rspec复合期望停止工作rspec
【发布时间】:2021-08-05 10:03:53
【问题描述】:

最近我的测试开始失败,出现以下异常。

 NoMethodError:
       undefined method `and' for #<Capybara::RSpecMatchers::Matchers::HaveText:0x00007f97687dbe30>
       Did you mean?  end 

我的测试看起来像

it "starts german but allows to be changed to english" do
          within(".e-header") do
            # german locale
            expect(page).to have_content("Mein Anliegen").and not_have_content("My issue")
          end
end 

compound expectations 的 Rspec 3.10 文档仍然支持它。我不知道为什么我的测试突然开始失败了。

【问题讨论】:

  • 多重预期已经有点臭了——如果你要这样做,我至少会明确,不要使用复合预期。
  • 没错,我也不赞成。但如果你有遗留代码。它停止工作你需要让它正常工作。这就是我问这个问题的原因。

标签: ruby-on-rails rspec capybara


【解决方案1】:

如果rspec expectations is not be defined 看起来像capybara will not include compound module(换句话说,还不需要)

所以请明确尝试require "rspec/expectations"

the reason:

这是由于我们支持的 RSpec 的版本范围以及一些人在我们这样做时抱怨的事实,因为有些人不想要 RSpec::Expectations。大多数人不会遇到这个问题,因为他们使用 Capybara 和 rspec-rails。

【讨论】:

  • 我尝试在 rails_helper 中要求 rspec/expectations,但没有成功。不知何故,当水豚加载 rspec/expectations 时仍未加载。
  • @AsadAli 在规范类中尝试require "rspec/expectations"
  • 我已经尝试过要求内部规范文件,但没有奏效。我安装了gem "rspec-expectations" 然后它工作了。
  • 等等,我以为 rspec 依赖于 rspec-expectations ?我读到如果你安装 gem rspec 你也有rspec-expectations,你可以在这里查看:github.com/rspec/rspec-expectationsjust install the rspec gem and RubyGems will also install rspec-expectations for you (along with rspec-core and rspec-mocks)
  • 我正在使用 rspec-rails gem。这也是我的意图。但是 capybara 和 rspec/expectations 的加载顺序不正确。我什至尝试在 rails_helper 中更改 require 文件的顺序,它没有改变任何东西,但是在 gemfile 中添加 "rspec-expectations" 解决了问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多