简体   繁体   English

测试与水豚和rspec的破坏行动

[英]Test destroy action with capybara and rspec

I'm under capybara and rspec, which is the best way to test the destroy link for an object? 我在capybara和rspec下,这是测试对象的destroy链接的最佳方法吗? I have to click this link 我必须点击此链接

<a href="/categories/1" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Destroy</a>

but I'm not able to select it with: 但是我无法选择它:

it "should destroy a category" do
  visit categories_path
  find(:xpath, '//link[@href="/categories/1"]').click
  # how to handle javascript pop up for confirmation?
end

any hint? 任何提示? Thank you! 谢谢!

expect { click_link 'Destroy' }.to change(Category, :count).by(-1)
it "should destroy a category" do
 expect { click_link('Destroy') }.to change(Category, :count).by(-1)
end

Since it looks as if this is a request spec, you can more closely simulate the user with: 由于它看起来好像这是一个请求规范,因此您可以使用以下命令更密切地模拟用户:

it "should destroy a category" do
  visit categories_path
  find(:xpath, '//link[@href="/categories/1"]').click
  sleep 1.seconds
  alert = page.driver.browser.switch_to.alert
  expect { alert.accept }.to change(Category, :count).by(-1)
end

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM