Capybara复选框与jQuery没有找到字段

时间:2014-04-19 17:33:19

标签: jquery ruby-on-rails rspec capybara

我有一个复选框,一旦设置为true(或勾选),它就会打开一个文本字段。它有效,但自实施以来,我的测试不再通过。我试过通过xpath识别路径

    it "can decide price" do 
  visit '/posts/new'
  fill_in 'post_description', with: 'Owl in a hat'
  expect(page).to have_css("#want_sell")
  find(:css, "#want_sell").set(true)
  expect(page).to have_xpath("//*[@id='price']")

我收到错误

User posting an image chooses to sell prints can decide price
 Failure/Error: expect(page).to have_xpath("//*[@id='price']")
 Capybara::ExpectationNotMet:
   expected to find xpath "//*[@id='price']" but there were no matches. Also found "", which matched the selector but not all filters.

我已经改变了

      find(:css, "#want_sell").set(true)

blah.click

blah.set('true')

但我仍然有同样的错误。

的JavaScript

$('#want_sell').click(function() {
8      $("#price").toggle(this.checked);
9  });

红宝石/轨

<%= f.label :want_sell?, 'Want to sell your item?' %></br>  
<%= f.check_box :want_sell, :id => 'want_sell' %>

1 个答案:

答案 0 :(得分:0)

好的伙计们,我做到了。

以下是:

首先,我需要将Poltergeist添加到Capybara并在我的测试中指定它,就像这样

    it "can decide price", js: true do 

其次,我有预先存在的javascript错误,需要在Poltergeist运行之前修复。 第三,这段代码没有连接,

<%= f.label :want_sell?, 'Want to sell your item?' %></br>  
<%= f.check_box :want_sell, :id => 'want_sell' %> 

应该是这样的

<%= f.label :want_sell?, 'Want to sell your item?' %></br>  
<%= f.check_box :want_sell?, :id => 'want_sell' %> 

所以我的测试规范可以点击标签作为对复选框的引用。

现在我可以简单地写下我的测试......

 check 'Want to sell your item?'

总任务布鲁夫。