【发布时间】:2019-01-07 13:06:34
【问题描述】:
我浏览了几篇有相同问题的帖子,但仍然觉得我的有点不同。
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 时,出现错误:
失败:
1) 用户可以通过类别详细信息查看类别 失败/错误: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
【问题讨论】:
标签: capybara factory-bot rspec-rails ruby-on-rails-5.2