我在capybara和rspec之下,这是测试对象的destroy链接的最佳方法吗? 我必须点击此链接
<a href="/categories/1" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Destroy</a>
但我无法选择:
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
任何提示? 谢谢!
答案 0 :(得分:14)
expect { click_link 'Destroy' }.to change(Category, :count).by(-1)
答案 1 :(得分:4)
it "should destroy a category" do
expect { click_link('Destroy') }.to change(Category, :count).by(-1)
end
答案 2 :(得分:1)
由于看起来这是一个请求规范,您可以使用以下命令更精确地模拟用户:
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
干杯!