当内容在page.html中时,RSpec / Capybara“expect(page).to have_content”测试失败

时间:2016-03-27 07:40:40

标签: ruby-on-rails-4 capybara rspec-rails

我正在使用RSpec& amp;编写一个应用BDD的Rails应用程序。水豚。我的一个测试仍然失败。测试的目标是检查索引页面上显示的每个计算机记录,单击编辑链接,结果可视化详细信息编辑页面。当我运行我的应用程序时,此功能有效。所以我想,我的RSpec场景有问题。

这是失败的测试:

Failures:

  1) existing machines have a link to an edit form
     Failure/Error: expect(page).to have_content(@existing_machine.model)
       expected to find text "RX22" in "Toggle navigation uXbridge Catalogue Settings Brands Machine Types Machine Groups Repair States Titles User Signed in as john@example.com Sign out Machine details Brand TORO Model Machine type ZITMAAIER Description Engine Purchase Price Unit Price VAT Minimal Stock Current Stock Warehouse Location"
 # ./spec/features/machine_spec.rb:50:in `block (2 levels) in <top (required)>'

以下是测试代码:

RSpec.feature 'existing machines' do
  before do
    @john = User.create!(email: 'john@example.com', password: 'password')

    login_as @john
    brand = Brand.create!(name: 'TORO')
    machinegroup = Machinegroup.create!(name: 'GAZON')
    machinetype = Machinetype.create!(name: 'ZITMAAIER', machinegroup_id: machinegroup.id)
    @existing_machine = Machine.create!(brand_id: brand.id, model: 'RX22', machinetype_id: machinetype.id, description: 'fantastic machine', engine: '100PK' )
  end

  scenario 'have a link to an edit form'  do
    visit '/machines'
    find("a[href='/machines/#{@existing_machine.id}/edit']").click
    expect(page).to have_content('Machine details')
    expect(page).to have_content(@existing_machine.model)
  end
end

调试场景时,@ before_machine对象似乎通过before do块中的.create!()方法正确填充。

screenshot of debug window in IDE

在调试器中检查page.html时,我看到出现了“RX22”字符串。

screenshot of page.html inspection

那么为什么RSpec / Capybara在执行expect(page).to have_content(@ existing_machine.model)时看不到相同的内容?

1 个答案:

答案 0 :(得分:1)

RX22是输入元素的值而不是文本内容,因此您需要以不同方式检查它。像

这样的东西
expect(page).to have_field('Model', with: 'RX22')

应该有效