Ruby on rails n个按钮的水豚试验取决于模型中的记录数

Ruby on rails n个按钮的水豚试验取决于模型中的记录数,ruby-on-rails,rspec,capybara,Ruby On Rails,Rspec,Capybara,是否可以编写一个集成测试,检查N个按钮是否出现在N是模型中记录数的页面上 我有一套模型和一套控制器。suites\u控制器索引操作检索套件模型中的所有记录 在我的视图/suites/index.html.erb中,我为每个套件生成一个按钮 <% @suites.each do |suite| %> <%= submit_tag suite.name, :class => "btn btn-primary btn-lg" %> <% end %>

是否可以编写一个集成测试,检查N个按钮是否出现在N是模型中记录数的页面上

我有一套模型和一套控制器。suites\u控制器索引操作检索套件模型中的所有记录

在我的视图/suites/index.html.erb中,我为每个套件生成一个按钮

<% @suites.each do |suite| %>
    <%= submit_tag suite.name, :class => "btn btn-primary btn-lg" %>
<% end %>
但是我得到了错误

1) creating a new test end to end user logs in user clicks the new test button displays all available suites
 Failure/Error: expect(page).to have_selector(:link_or_button, 'Suite 1')
   expected #has_selector?(:link_or_button, "Suite 1") to return true, got false
 # ./spec/integration/new_test_spec.rb:39:in `block (4 levels) in <top (required)>'
1)创建新的测试端到端用户登录用户单击“新建测试”按钮显示所有可用套件
失败/错误:预期(第页)。要有\u选择器(:链接\u或\u按钮,“套件1”)
期望#has_selector?(:link_或_按钮,“suite1”)返回true,得到false
#./spec/integration/new_test_spec.rb:39:in'block(4级)in'
我不确定我是否遗漏了一些非常明显的东西,或者是以错误的方式处理它


你知道我如何测试这个吗?

有一点肯定是你的测试用例没有导航到给定的页面。例如,如果您的套件是从
SuiteController
显示的,则应使用导航到该页面:

it "displays all available suites" do
    FactoryGirl.create(:suite, name:'Suite 1')
    FactoryGirl.create(:suite, name:'Suite 2')

    visit '/suites'

    expect(page).to have_selector(:link_or_button, 'Suite 1')
    expect(page).to have_selector(:link_or_button, 'Suite 2')
end

抱歉,我只包括测试视图的测试部分。我的整个规范确实导航到了“/suites”页面,这意味着此时测试在“/suites”上,我将更新问题我不相信这些更新。如果您访问
/suites
页面,然后创建新页面,它将不会神奇地更新该页面。如果访问时没有创建套件,则测试将失败。或者,如果您的测试用例正在做一些更复杂的事情,那么示例应该显示更多的细节。感谢@Alex P,我在访问“/suites”之后创建了条目,就像一个numpty…我应该在访问“/suites”之前创建测试数据。该死的星期五!
1) creating a new test end to end user logs in user clicks the new test button displays all available suites
 Failure/Error: expect(page).to have_selector(:link_or_button, 'Suite 1')
   expected #has_selector?(:link_or_button, "Suite 1") to return true, got false
 # ./spec/integration/new_test_spec.rb:39:in `block (4 levels) in <top (required)>'
it "displays all available suites" do
    FactoryGirl.create(:suite, name:'Suite 1')
    FactoryGirl.create(:suite, name:'Suite 2')

    visit '/suites'

    expect(page).to have_selector(:link_or_button, 'Suite 1')
    expect(page).to have_selector(:link_or_button, 'Suite 2')
end