2

Capybaraを使用していくつかのテストを作成しましたが、セレンも他のJSドライバーも使用していません。しかし、私の疑問は、この方法でdestroyメソッドをテストできるかどうかです。JS確認を確認する必要があり、data-method="delete"にアクセスできないため...

私はカピバラのやり方で次のようなことをしたいと思います。

'/ people / 123'、:data-method=>'delete'にアクセスしてください

あなたたちはそれをする方法があるかどうか知っていますか?

よろしくお願いします、アンドレ

4

2 に答える 2

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.

于 2011-05-03T15:17:39.870 に答える
4

@Mike Mazurの回答で注意すべき点は、コントローラーが#destroyアクションでリダイレクトを実行した場合、Capybaraのドライバーでdeleteメソッドを呼び出しても、実際にはリダイレクトに従わないことです。代わりにresponse、リダイレクトメッセージ、つまりbody == <html><body>You are being <a href=\"http://www.example.com/model_names\">redirected</a>.</body></html>とを入力するだけstatus_code == 302です。

したがって、通常のカピバラで期待するようにすべてを入力するにclick_button 'Delete Model'は、リダイレクトを手動でたどる必要があります。これを行う1つの(リファクタリングされていないRSpecフレーバーの)方法:

Capybara.current_session.driver.delete controller_path(model_instance)
Capybara.current_session.driver.response.should be_redirect
visit Capybara.current_session.driver.response.location
于 2013-02-14T17:22:17.563 に答える