2016-02-29 36 views
0

我想寫一個AJAX調用測試。該功能適用​​於生產,但我在Capybara遇到麻煩。似乎find(".selector").click不能在同一場景中調用兩次。水豚沒有找到相同的鏈接兩次使用發現

方案1次傳球,但場景#2給出了第二find(".article__expand").click這個錯誤:

Failure/Error: find(".article__expand").click 
    Capybara::ElementNotFound: 
    Unable to find css ".article__expand" 

我使用一個CSS選擇器,因爲鏈接的身體是一個glyphicon跨度。有沒有解決的辦法?


require "rails_helper" 

RSpec.feature "Users can toggle article descriptions" do 
    let(:article) { FactoryGirl.create(:article, description: "Article description") } 

    context "all users" do 
    before do 
     visit article_path(article) 
    end 

    scenario "description is hidden until clicked" do 
     expect(page).to_not have_content("Article description") 

     find(".article__expand").click 

     expect(page).to have_content("Article description") 
    end 

    scenario "description can be toggled" do 
     expect(page).to_not have_content("Article description") 

     find(".article__expand").click 

     expect(page).to have_content("Article description") 

     find(".article__expand").click 

     expect(page).to_not have_content("Article description") 
    end 
    end 
end 

這裏的電話是怎麼回事。我在resources :articles上使用自定義的description獲得成員。

# views/articles/_article.html.erb 
<div class="article" id="article-<%= article.id %>"> 
    <%= link_to description_article_path(article), remote: true, class: "article__expand" do %> 
    <span class="glyphicon glyphicon-plus"></span> 
    <% end %> 
    <div class="article__info"> 
    </div> 
</div> 

# views/articles/description.js.erb 
$(function() { 
    if ($("#article-<%= @article.id %> .article__description").length) { 
    $("#article-<%= @article.id %> .article__description").toggle(); 
    } else { 
    $.getJSON('/articles/<%= @article.id %>.json', function(data) { 
     var description = ('<p class="article__description">' + data.description + '</p>') 
     $('#article-<%= @article.id %> .article__info').prepend(description); 
    }); 
    } 
}); 
+1

我只是猜測,因爲在這裏我不是CSS專家。但是,如果單擊「.article_expand」,它將被擴展,並且該類應該更改爲「.article_minimize」之類的內容。你有沒有實現任何這樣的功能? – bork

+0

@bork沒有這樣的,我會在視圖中編輯。他們非常簡單,所以我認爲這是一個水豚問題,但我可能是錯的。 – Jane

+0

你正在使用哪種水豚? –

回答

0

調試這些樣的東西,我會建議以下步驟:

gem install launchy

發射說:Launchy是在這裏做一個通用的方法來從Ruby程序中啓動外部應用程序。

在預期失敗之前,在規範中使用save_and_open_page。像下面這樣:

scenario "description can be toggled" do 
    expect(page).to_not have_content("Article description") 
    find(".article__expand").click 
    save_and_open_page 
    expect(page).to have_content("Article description") 
    find(".article__expand").click 
    .... 

這將使它更容易調試。也可以考慮採取不同的方法,使用的水豚的方法,如:

=Clicking links and buttons= 
    click_link('id-of-link') 
    click_link('Link Text') 
    click_button('Save') 
    click('Link Text') # Click either a link or a button 
    click('Button Value') 

這些來自一個超級有益的水豚的cheatsheet這裏找到 - https://gist.github.com/zhengjia/428105

+0

不幸的是,這帶來了比答案更多的問題。我可能會進行更新並嘗試獲得幫助調試,但由於我非常規的自定義'description' get member,我感覺這個問題。 – Jane

+0

好的。也許你也可以更新你的Capybara驅動程序的更多細節? (Selenium,Capybara-Webkit或Poltergeist?)和那個設置。如果你仍然不確定它爲什麼失敗,我會說這是一個問題的可能領域。 –

+0

我正在使用Capybara-Webkit,但基本上使用':js => true',它在控制器級失敗。它無法使用'Model.find'找到實例的ID。從FactoryGirl製作實例並且延遲加載等等,這並沒有什麼意義。混淆! – Jane