最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Testing destroy methods with capybara - Stack Overflow

programmeradmin1浏览0评论

I've written some tests using Capybara, but I'm not using selenium and neither any other JS drivers. But my doubt is if I can test a destroy method this way? Since I need to confirm a JS confirmation and the data-method = "delete" can't be visited...

I would like to do something very Capybara's way like:

visit '/people/123', :data-method => 'delete'

Do you guys know if is there some way to do that?

Thanks in advance, Andre

I've written some tests using Capybara, but I'm not using selenium and neither any other JS drivers. But my doubt is if I can test a destroy method this way? Since I need to confirm a JS confirmation and the data-method = "delete" can't be visited...

I would like to do something very Capybara's way like:

visit '/people/123', :data-method => 'delete'

Do you guys know if is there some way to do that?

Thanks in advance, Andre

Share Improve this question asked May 3, 2011 at 14:55 AndreDuraoAndreDurao 5,7957 gold badges46 silver badges65 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Rails has JavaScript code which generates a form from the link's href and data-method attributes and submits it; this won't work without JS.

One way to test this: first, test for the presence of the link and proper attributes (href, data-method), then trigger the delete request manually with the Capybara::RackTest::Driver#delete method. If you do this often, write a helper method wrapping those two steps.

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./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
发布评论

评论列表(0)

  1. 暂无评论