2013-08-10 3 views
5

मुझे इस मुद्दे के आसपास अपने सिर को लपेटने में बहुत मुश्किल समय है, इसलिए किसी भी मदद की सराहना की जाएगी।ककड़ी में jQuery अजाक्स डिबगिंग + कैपिबरा + रेल में पैंटॉमजेएस

मैं बस अपनी परियोजना पर एक साधारण अजाक्स-आधारित पंजीकरण फॉर्म का परीक्षण करना चाहता हूं। यदि फॉर्म सबमिशन सफल है, तो आपको स्वागत पृष्ठ पर रीडायरेक्ट किया जाता है। यदि ऐसा नहीं है, तो आपको प्रत्येक अपमानजनक फ़ील्ड से संबंधित उचित सत्यापन त्रुटियां मिलती हैं।

कुछ अजीब कारणों से, कैपिबरा रीडायरेक्ट का पालन नहीं कर रहा है। अजाक्स कॉल किया जा रहा है और मुझे डेटाबेस में पंजीकृत एक नया खाता दिखाई देता है, लेकिन 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 

जावास्क्रिप्ट:

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.') 
}) 

ठीक है, इसलिए यह विशेष परीक्षण ठीक तब तक चलता है जब तक हम उस बिंदु तक नहीं पहुंच जाते जहां हमें उपयोगकर्ता को स्वागत स्क्रीन पर रीडायरेक्ट करना होता है। मैंने onSuccess, onFailure कॉलबैक के अंदर क्या हो रहा है, यह देखने के लिए संभवतः मैं सबकुछ कोशिश कर सकता हूं, लेकिन इसका कोई फायदा नहीं हुआ। ऐसा लगता है कि कोड को भी निष्पादित नहीं किया जा रहा है।

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 और कोडरवाल के लोगों के अनुसार, आप इसे एक सहायक विधि के साथ कर सकते हैं, 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

बेशक

, ऊपर ब्लॉग पोस्ट के अनुसार, इस या बिल्कुल, निर्भर करता है कि आप अपना पृष्ठ संरचित अगर आप सिर्फ एक चयनकर्ता अपने स्वागत पृष्ठ के लिए अद्वितीय है कि देखने की जरूरत हो नहीं हो सकता है।

संबंधित मुद्दे