我已经使用 Capybara 编写了一些测试,但我没有使用 selenium,也没有使用任何其他 JS 驱动程序。但我怀疑我是否可以通过这种方式测试破坏方法?由于我需要确认一个 JS 确认并且 data-method = "delete" 无法访问...
我想做一些非常像水豚的方式:
访问 '/people/123', :data-method => '删除'
你们知道有没有办法做到这一点?
提前致谢, 安德烈
我已经使用 Capybara 编写了一些测试,但我没有使用 selenium,也没有使用任何其他 JS 驱动程序。但我怀疑我是否可以通过这种方式测试破坏方法?由于我需要确认一个 JS 确认并且 data-method = "delete" 无法访问...
我想做一些非常像水豚的方式:
访问 '/people/123', :data-method => '删除'
你们知道有没有办法做到这一点?
提前致谢, 安德烈
Rails 有 JavaScript 代码,它从链接href
和data-method
属性生成一个表单并提交它;没有 JS,这是行不通的。
一种测试方法:首先,测试是否存在链接和适当的属性(href
, ),然后使用方法data-method
手动触发删除请求。如果您经常这样做,请编写一个包含这两个步骤的辅助方法。Capybara::RackTest::Driver#delete
Just something to be aware of with @Mike Mazur's answer, if your controller does a redirect on the #destroy
action, invoking the delete method on Capybara's driver doesn't actually follow the redirect. Instead it just populates the response
with the redirection message, i.e. body == <html><body>You are being <a href=\"http://www.example.com/model_names\">redirected</a>.</body></html>
and status_code == 302
.
So, to get everything populated like you would expect on a normal Capybara click_button 'Delete Model'
, you'll need to manually follow the redirect. One (unrefactored and RSpec flavored) way to do this:
Capybara.current_session.driver.delete controller_path(model_instance)
Capybara.current_session.driver.response.should be_redirect
visit Capybara.current_session.driver.response.location