RSpec/Capybara "expect(page).to have_content" 内容在 page.html 时测试失败
RSpec/Capybara "expect(page).to have_content" test fails while content is in page.html
我正在使用 RSpec 和 Capybara 编写应用 BDD 的 Rails 应用程序。我的一项测试继续失败。测试的目的是检查索引页面上显示的每条机器记录,点击编辑link,是否会导致可视化详细信息编辑页面。当我 运行 我的应用程序时,此功能有效。所以我想,我的 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
调试场景时,@existing_machine 对象似乎已通过 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)时看不到相同的内容?
RX22 是输入元素的值而不是文本内容,因此您需要以不同的方式检查它。像
expect(page).to have_field('Model', with: 'RX22')
应该可以
我正在使用 RSpec 和 Capybara 编写应用 BDD 的 Rails 应用程序。我的一项测试继续失败。测试的目的是检查索引页面上显示的每条机器记录,点击编辑link,是否会导致可视化详细信息编辑页面。当我 运行 我的应用程序时,此功能有效。所以我想,我的 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
调试场景时,@existing_machine 对象似乎已通过 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)时看不到相同的内容?
RX22 是输入元素的值而不是文本内容,因此您需要以不同的方式检查它。像
expect(page).to have_field('Model', with: 'RX22')
应该可以