"Ambiguous match, found 2 elements matching visible link" 问题
"Ambiguous match, found 2 elements matching visible link" issue
我看了几个有同样问题的帖子,但还是觉得我的有点不一样。
viewing_categories_spec.rb
require 'rails_helper'
RSpec.feature 'Users can view categories' do
scenario 'with the category details' do
category = FactoryBot.create(:category, name: 'Real Estate')
visit '/categories'
click_link('Real Estate')
expect(page.current_url).to eq category_url(category)
end
end
category_factory.rb
FactoryBot.define do
factory :category do
name {"Computers"}
end
end
当我 运行 rspec
时,出现错误:
Failures:
1) Users can view categories with the category details
Failure/Error: click_link('Real Estate')
Capybara::Ambiguous:
Ambiguous match, found 2 elements matching visible link "Real Estate"
# ./spec/features/viewing_categories_spec.rb:8:in `block (2 levels) in <main>'
然后我通过添加 match: :first
:
修改了规范
require 'rails_helper'
RSpec.feature 'Users can view categories' do
scenario 'with the category details' do
category = FactoryBot.create(:category, name: 'Real Estate')
visit '/categories'
click_link('Real Estate', match: :first)
expect(page.current_url).to eq category_url(category)
end
end
这次报错:
Failures:
1) Users can view categories with the category details
Failure/Error: expect(page.current_url).to eq category_url(category)
expected: "http://www.example.com/categories/265"
got: "http://www.example.com/categories/17"
(compared using ==)
# ./spec/features/viewing_categories_spec.rb:9:in `block (2 levels) in <main>'
我注意到有时我没有看到错误,有时它会出现。
我唯一看到的总是"http://www.example.com/categories/17"
。
当我使用 运行 rspec
命令时,这部分始终保持不变。
完整的源代码在这里https://github.com/tenzan/kaganat
事实是“http://www.example.com/categories/17”url 是不变的,当您的测试似乎只创建一个时,水豚在页面上看到两个 "Real Estate" 链接,这让我相信你有一些旧数据留在你的测试数据库中。通过选择使用 match: :first
,您只是掩盖了一个事实,即您现有的记录比您预期的要多,并且该错误应该是您的第一个线索(以及仅查看测试 [=25= 的屏幕截图) ]).像
rails db:reset RAILS_ENV=test
将清除您的测试数据库并确保您没有旧数据。您还想回到没有 :match 设置的原始 click_link('Real Estate')
。此外,如果你想要稳定的测试,你几乎不应该使用标准 RSpec 匹配器('eq' 等)和 Capybara 返回的对象,因为页面 load/behavior 是一个异步的东西。相反,您应该使用 Capybara 提供的匹配器。在您当前的示例中,这意味着您应该写 expect(page).to have_current_path(category_url(category))
而不是写 expect(page.current_url).to eq category_url(category)
我看了几个有同样问题的帖子,但还是觉得我的有点不一样。
viewing_categories_spec.rb
require 'rails_helper'
RSpec.feature 'Users can view categories' do
scenario 'with the category details' do
category = FactoryBot.create(:category, name: 'Real Estate')
visit '/categories'
click_link('Real Estate')
expect(page.current_url).to eq category_url(category)
end
end
category_factory.rb
FactoryBot.define do
factory :category do
name {"Computers"}
end
end
当我 运行 rspec
时,出现错误:
Failures:
1) Users can view categories with the category details Failure/Error: click_link('Real Estate')
Capybara::Ambiguous: Ambiguous match, found 2 elements matching visible link "Real Estate" # ./spec/features/viewing_categories_spec.rb:8:in `block (2 levels) in <main>'
然后我通过添加 match: :first
:
require 'rails_helper'
RSpec.feature 'Users can view categories' do
scenario 'with the category details' do
category = FactoryBot.create(:category, name: 'Real Estate')
visit '/categories'
click_link('Real Estate', match: :first)
expect(page.current_url).to eq category_url(category)
end
end
这次报错:
Failures:
1) Users can view categories with the category details
Failure/Error: expect(page.current_url).to eq category_url(category)
expected: "http://www.example.com/categories/265"
got: "http://www.example.com/categories/17"
(compared using ==)
# ./spec/features/viewing_categories_spec.rb:9:in `block (2 levels) in <main>'
我注意到有时我没有看到错误,有时它会出现。
我唯一看到的总是"http://www.example.com/categories/17"
。
当我使用 运行 rspec
命令时,这部分始终保持不变。
完整的源代码在这里https://github.com/tenzan/kaganat
事实是“http://www.example.com/categories/17”url 是不变的,当您的测试似乎只创建一个时,水豚在页面上看到两个 "Real Estate" 链接,这让我相信你有一些旧数据留在你的测试数据库中。通过选择使用 match: :first
,您只是掩盖了一个事实,即您现有的记录比您预期的要多,并且该错误应该是您的第一个线索(以及仅查看测试 [=25= 的屏幕截图) ]).像
rails db:reset RAILS_ENV=test
将清除您的测试数据库并确保您没有旧数据。您还想回到没有 :match 设置的原始 click_link('Real Estate')
。此外,如果你想要稳定的测试,你几乎不应该使用标准 RSpec 匹配器('eq' 等)和 Capybara 返回的对象,因为页面 load/behavior 是一个异步的东西。相反,您应该使用 Capybara 提供的匹配器。在您当前的示例中,这意味着您应该写 expect(page).to have_current_path(category_url(category))
expect(page.current_url).to eq category_url(category)