【问题标题】:expect(page) .to have_content not working期望(页面) .to have_content 不起作用
【发布时间】:2015-03-11 12:05:36
【问题描述】:

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

我已经添加了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  

运行规范时出错:

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

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

水豚的版本是水豚(2.4.4)

【问题讨论】:

    标签: ruby-on-rails rspec


    【解决方案1】:

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

    你的语法是

    page.should have_content("Baked Potato")
    

    你真的应该升级 RSpec。

    【讨论】:

    • 嗯,做了一点研究,发现应该从 2.11 版(here)开始支持 expect。从 v3 开始会有弃用警告,计划在 v4 (here) 中默认禁用它。还是我理解错了?
    猜你喜欢
    • 1970-01-01
    • 2017-11-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多