我正在尝试使用水豚测试下拉列表。如果我不清理数据库,它将很好地工作。我必须清理数据库以使其他测试正常运行。
config.before(:suite) do
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.strategy = :transaction
end
config.before(:each, js: true) do
DatabaseCleaner.strategy = :truncation, {:except => %w[questions]}
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
如果我注释掉数据库清理程序,则测试必须至少运行两次才能通过。我可以看到它重试,并且总是以相同的条件进行第二次尝试。一旦将数据库清理器放置到位,测试参数就会在每次迭代中更改,因此就没有机会对其进行第二次测试了。
context 'when there are questions and answers in the system' do
before do
allow_any_instance_of(Form).to receive(:create_questions)
question = create(:question)
people.map do |person|
create(:answer, question: question, person: person) if [true, false].sample
end
end
it 'should allow filtering by whether a person has answered a specific question', js: true do
@question = Question.first
select_from_dropdown @question.text, from: 'question'
click_on 'Search'
people.each do |person|
if person.questions.include? @question
expect(page).to have_content person.full_name
else
expect(page).to_not have_content person.full_name
end
end
end
end
这是探索水豚下拉菜单的帮助方法
def select_from_dropdown(item_text, options)
# find dropdown selector
dropdown = find_field(options[:from], visible: false).first(:xpath, './/..')
# click on dropdown
dropdown.trigger('click')
# click on menu item
dropdown.find('.menu .item', text: item_text, visible: false).trigger('click')
end
我在sleep
的整个过程中都尝试过select_from_dropdown
,以为加载速度不够快,但这不是问题。理想情况下,此测试应该在第一次尝试时就可以运行,但至少需要通过数据库清理程序才能通过。
答案 0 :(得分:1)
测试中没有显示visit
,这意味着您在创建问题之前已经访问了该页面,因此期望的信息未显示在页面上。如果您不清除说明第二次传递原因的数据库,则该数据将在第一次运行中存在于第二次运行中。创建测试数据后,您应该始终访问该页面。
此外,正如我在评论中提到的那样,您可能想要修复(如果使用Rails 5.1+,则删除)您的DatabaseCleaner配置,并且在测试中使用trigger
是一个非常糟糕的主意,因为它可以做用户从未做过的事情可以。