Capybara have_text 在它应该失败的时候通过了

Capybara have_text Passes When It Should Fail

我有一个用 RSpec 和 Capybara 编写的测试应该不会通过,但它是:

it 'should have the given text' do
  visit root_path
  expect(page).to have_text("This is not here")
end

然后我将其包含在测试中以查找问题:

puts page.text

它 returns 比页面上的文字多很多,其中包括:

Extracted source (around line #12):
10 11 12 13
})
visit root_path
expect(page).to have_text("This is not here")

看起来我在测试中放入的任何内容都会在调用 has_text? 时出现。为什么它包含所有不在页面上的文本?

这是因为您的 app/code 和 gem 将错误和周围代码呈现到在测试模式下启用的返回页面中有错误。您希望在测试模式下禁用所有这些类型的 gems(better_errors 等)(因此它更接近生产模式)。您可以通过将它们移动到 Gemfile 中的仅开发组来做到这一点。您还可以查看 test.log 以查看抛出的错误。