如何使用rspec和capybara协调动画的计时

时间:2013-10-29 18:19:59

标签: rspec capybara

我无法在不调用睡眠的情况下让rspec测试工作:

it 'should allow deleting of foo brand attributes', :js => true do                                                                                            
  @foo.brand_attributes << BrandAttribute.new(key: :test, value: :test)                                                                                 
  @foo.save!                                                                                                                                            
  @foo.brand_attributes.length.should eq 1                                                                                                              
  visit('/admin')                                                                                                                                         
  expect(page).to have_content 'Test Foo'                                                                                                               
  click_link 'Edit'    
  # Edit opens a modal edit form with a list of attribute fields                                                                                                                                   
  page.should have_selector('#edit-modal-div', visible: true)                                                                                            
  page.should have_selector('.attribute_fields', visible: true)
  page.should have_selector('.attribute_fields input[type=hidden]') 
  # 1st sleep:
  sleep 0.3                                                                                      
  within(:css, "#edit-modal-div") do   
    # should update hidden input with destroy value                                                                                                                  
    click_link 'Delete'                                                                                                                                   
  end
  # 2nd sleep                                                                                                                                               
  sleep 0.3                                                                                                                                               
  page.should have_selector('.attribute_fields', visible: false)                                                                                          

  within(:css, "#edit-modal-div") do                                                                                                                     
    click_button 'Save'                                                                                                                                   
  end                                                                                                                                                     

  page.find('.alert').should have_content 'successfully updated'                                                                                          
  @foo = Foo.find(@food.id)                                                                                                                          
  @foo.brand_attributes.length.should eq 0 

我的期望是“page.should have_selector(”调用应该等到jquery动画完成后再单击编辑,但由于某种原因,删除按钮不会更新隐藏字段,除非我在调用之前插入睡眠单击“删除”。似乎页面上的某些内容尚未准备就绪。

不推荐使用wait_until,那么确定单击“删除”按钮的最佳方法是什么?

我尝试将我的链接更改为:

<a class="destroy_link" href="#" onclick="x = 4; return false;">Delete</a>

当我单击“删除”时,它不会将x设置为4.看起来,当capybara触发点击时,onclick处理程序未初始化。有什么我需要等待的吗?

0 个答案:

没有答案