期待(页面).to_content不工作

时间:2015-03-11 12:05:36

标签: ruby-on-rails rspec

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

我已添加spec文件和

下面的错误
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  

运行spec时出错:

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 gems的版本

  • rspec(2.99.0)
  • rspec-collection_matchers(1.1.2)
  • rspec-core(2.99.2)
  • rspec-expectations(2.99.2)
  • rspec-mocks(2.99.3)
  • rspec-rails(2.99.0)

水豚的种类是水豚(2.4.4)

1 个答案:

答案 0 :(得分:1)

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

您的语法是

page.should have_content("Baked Potato")

你应该真的升级RSpec。