期望(页面).to have_content 不工作

expect(page) .to have_content not working

根据 capybara docsexpect(page).to have_content 语法是合法的,但它似乎不起作用

我添加了规范文件和下面的错误

require 'spec_helper.rb'

feature "Looking up recipes", js: true do
  before do
    Recipe.create!(name: 'Baked Potato w/ Cheese')
    Recipe.create!(name: 'Garlic Mashed Potatoes')
    Recipe.create!(name: 'Potatoes Au Gratin')
    Recipe.create!(name: 'Baked Brussel Sprouts')
  end

  scenario "finding recipes" do
    visit '/'
    fill_in "keywords", with: "baked"
    click_on "Search"

    expect(page).to have_content("Baked Potato")
    expect(page).to have_content("Baked Brussel Sprouts")
  end
end  

运行 规格时出错:

sai@saip:~/rails_apps/receta$ rspec spec/features/search_spec.rb 
F
Failures:

 1) Looking up recipes finding recipes
 Failure/Error: expect(page).to have_content("Baked Potato")
   only the `receive` matcher is supported with `expect(...).to`, but you have provided: #<Capybara::RSpecMatchers::HaveText:0x000000057c0470>
 # ./spec/features/search_spec.rb:15:in `block (2 levels) in <top (required)>'

Finished in 9.95 seconds
1 example, 1 failure

Failed examples:

rspec ./spec/features/search_spec.rb:10 # Looking up recipes finding recipes

Randomized with seed 31312

这些是各种 rspec 宝石的版本

水豚的版本是水豚(2.4.4)

您为该语法使用了错误版本的 RSpec。 expect(...).to ... 的使用是在 RSpec 3+ 中编写测试的公认方式。您正在使用 2.99.

你的语法是

page.should have_content("Baked Potato")

你真的应该升级RSpec。