Capybara测试n个按钮取决于模型中的记录数

时间:2014-08-08 15:36:09

标签: ruby-on-rails rspec capybara

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

我有一个模型套件和一个控制器 suites_controller suites_controller 索引操作检索套件模型中的所有记录

def index
    @suites = Suite.all
end

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

<% @suites.each do |suite| %>
    <%= submit_tag suite.name, :class => "btn btn-primary btn-lg" %>
<% end %>

我使用Capybara + Rspec进行集成和使用案例测试,但在我的生活中我不知道如何测试视图中是否出现了正确数量的按钮。我试图做

...

# more spec above which visits '/suites'

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

    expect(page).to have_selector(:link_or_button, 'Suite 1')
    expect(page).to have_selector(:link_or_button, 'Suite 2')
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 个答案:

答案 0 :(得分:1)

肯定缺少的一件事是您的测试用例未导航到给定页面。例如,如果您的套件显示的是SuitesController,那么您应该使用Capybara visit helper导航到该页面:

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