2013-08-10 15 views
5

私はこの問題を回避するのに非常に困難な時間を抱いています。Cucumber + Capybara + PhantomJS in RailsのjQuery Ajaxのデバッグ

私がしたいのは、私のプロジェクトで簡単なAjaxベースの登録フォームをテストすることです。フォームの提出が成功すると、ウェルカムページにリダイレクトされます。そうでない場合は、問題のある各フィールドに関連する適切な検証エラーが発生します。

奇妙な理由のため、カピバラはリダイレクトに従っていません。 Ajaxコールが行われていて、データベースに登録された新しいアカウントが表示されますが、onSuccessコールバックがまったく呼び出されていないか、リダイレクトが無視されています。ここで

は、私は(簡潔にするために、私はコードを凝縮しています)で動作するようにしようとしているものです:

特集:

Feature: Registration 
    In order to obtain a new account 
    As a prospective customer 
    I must submit a valid registration form. 

    @javascript 
    Scenario: A valid registration attempt 
     Given an account registration form 
     When I complete the form with valid values 
     Then I should be redirected to the welcome screen 

テスト:

Given(/^an account registration form$/) do 
    visit("/signup") 
    assert current_path == "/signup" 
end 

When(/^I complete the form with valid values$/) do 
    within("#signupForm") do 
    fill_in("email", :with => Faker::Internet.email) 
    fill_in("name",  :with => Faker::Name.name) 
    fill_in("password", :with => "11111111") 

    click_link("signupFormSubmit") 
    end 
end 

Then(/^I should be redirected to the welcome screen$/) do 
    assert current_path == "/welcome" 
end 

JavaScript:

console.log('I am not yet inside you.') 

$.post(url, form.serialize(), function(response) { 
    // everything went well 
    // let's redirect them to the given page 
    window.location.replace(response.redirectUrl) 
    console.log('I am inside you and it is good.') 
}, function(response) { 
    // collect error responses from API 
    // apply error hints to associated fields 
    console.log('I am inside you and something went wrong.') 
}) 

大丈夫ですので、この特定のテストは、ユーザーをウェルカム画面にリダイレクトするはずです。 onSuccessonFailureコールバック内で何が起こっているのか分かりませんが、役に立たないものはすべて試しました。コードは実行されていないようです。

私は、テストの実行から次のような出力が得られます。私が例外を発生させる場合、それは拾われない問題ではありません

Then I should be redirected to the welcome screen # features/step_definitions/registration.rb:51 
    Failed assertion, no message given. (MiniTest::Assertion) 
    ./features/step_definitions/registration.rb:52:in `/^I should be redirected to the welcome screen$/' 
    features/registration.feature:15:in `Then I should be redirected to the welcome screen' 

。また、コールバック内でconsole.log()への呼び出しも行いません。

誰でもこれを見ましたか?もしそうなら、回避策はありますか?より多くの情報が必要な場合は、私が提供してくれることを喜んでお聞かせください。

答えて

0

robots at ThoughtbotとCoderwallの人によると、spec/supportに入れられたヘルパーメソッドでこれを行うことができます。そこから

# spec/support/wait_for_ajax.rb 
module WaitForAjax 
    def wait_for_ajax 
    Timeout.timeout(Capybara.default_wait_time) do 
     loop until finished_all_ajax_requests? 
    end 
    end 

    def finished_all_ajax_requests? 
    page.evaluate_script('jQuery.active').zero? 
    end 
end 

、あなたは自分のテストフレームワークにロードする必要があります。: 彼らは彼らのモジュールWaitForAjaxの名前RSpecのために、それはスペック/ configファイルに、または単にモジュールファイルの末尾にコードのビットをタックのいずれかによって行うことができます。

RSpec.configure do |config| 
    config.include WaitForAjax, type: :feature 
end 

あなたはおそらくあなたの設定ファイルでspec/support/**/*.rbを必要とすることを確認しますが、とにかくこれを行う必要があります。もちろん

http://www.elabs.se/blog/53-why-wait_until-was-removed-from-capybara

、上記のブログ記事によると、これは、またはあなたは自分のウェルカムページに固有のだセレクタを探している場合、あなたは、あなたのページを構造化する方法によっては、まったく必要としてもしなくてもよいです。

関連する問題