2013-02-17 28 views
2

這是間歇性故障測試:爲什麼這會請求spec失敗,或者我該如何調試?

context "as a regular_user" do 
    before :each do 
     @user = FactoryGirl.create(:user, :role => 'regular_user') 
     visit new_user_session_path unless current_path == new_user_session_path 
     fill_in "Email", :with => @user.email 
     fill_in "Password", :with => @user.password 
     click_button "Sign In" 
    end 

    it "removes thing from favorites while on thing index page" do 
     @things = FactoryGirl.create_list(:thing, 5) 
     @user.set_mark :favorite, @things.first 
     @user.reload.favorite_things 
     visit things_path unless current_path == things_path 
     expect{ 
     first(:link, "Remove from favorites").click # Sometimes this fails 
     @user.reload.favorite_things 
     }.to change { @user.favorite_things.count }.by(-1) 
     page.should have_content("Sort by") 
    end 
    end 

我使用的寶石markable提供了收藏夾功能。

當我使用rspec運行規格時(似乎不是一旦失敗,它總是失敗,反之亦然),似乎沒有任何成功/失敗的模式。當規格運行通過guardspork也被使用)時,可能會有一種模式,因爲如果我啓動guard並且測試通過,那麼它總會在guard的實例正在運行時通過... Sames進行失敗 - if我開始guard並且它失敗,那麼在guard的實例中的所有後續運行也將失敗。

剛纔我跑rspec spec/requests/users_spec.rb:148運行一個規範,它失敗:

1) Users as a regular_user removes thing from favorites while on thing index page 
    Failure/Error: click_link "Remove from favorites" # Sometimes this fails 
    Capybara::ElementNotFound: 
     Unable to find link "Remove from favorites" 
    # ./spec/requests/users_spec.rb:155:in `block (4 levels) in <top (required)>' 
    # ./spec/requests/users_spec.rb:153:in `block (3 levels) in <top (required)>' 

我以後正確的,又跑到rspec spec/requests/users_spec.rb:148,它成功了:

Finished in 0.83754 seconds 
1 example, 0 failures 

我試着加入index.html.erb視圖文件的<% @things.each do |thing| %>部分中的「asdfasdf」,並使用page.should have_content("asdfasdf")檢查該問題,並且有時會失敗。我試圖在expect塊之前添加page.should have_selector('.favoritesblock', visible: true)favoritesblock存在於視圖中相同的@things塊內),並且有時會失敗。但是,它始終無法在<% @things.each do |thing| %>循環之外找到文本。

我在first(:link, "Remove from favorites").click # Sometimes this fails行之前添加了save_and_open_page,並且能夠生成規範失敗的實例。該頁面只包含兩件事(我的FactoryGirl電話應創建5),他們都沒有'從收藏夾中刪除'的鏈接(取決於事物是否被收藏,它顯示「添加到收藏夾」或「從收藏夾中刪除「)。

此基礎上,我試圖修改規範中的一個位的前兩行,但它並沒有幫助(有時還是失敗):

5.times { FactoryGirl.create(:thing) } 
@user.set_mark :favorite, Thing.first 

我所做的一切通知,當它成功,它首先顯示「從收藏夾中刪除」鏈接,但它並不總是顯示所有5件事情。

所以,我需要知道爲什麼這個規範間歇性失敗,或者我能做些什麼來找出導致問題的原因。有任何想法嗎?

+1

您可以發佈您工廠的事?還有對模型的任何限制 - 可能有時候東西沒有被創建? – micapam 2013-02-18 00:31:18

+0

是的!謝謝!問題是我有一個'active'字段,我在我的工廠中設置爲'f.active {[true,false] .sample}'。所以,有時它會成爲一件活躍的事情,有時它會使事情失去活力。只要測試使第一件事情無效,規範就沒有什麼可以標記爲最喜歡的。我的修改是將我的規範改爲'@things = FactoryGirl.create_list(:thing,5,:active => true)',這樣它只會爲這個規範創建活躍的東西。 – 2013-02-18 01:03:21

+0

很高興你有這種排序:)我不得不處理類似的問題,使用加工商。 – micapam 2013-02-18 04:45:54

回答

1

我設置Factory的方式是導致rspec間歇性故障的原因。

該項目有一個布爾場,active,這無論是顯示在網站上的東西(如果它true)或不顯示在網站上的東西(如果它false)。

我很聰明,我的工廠,並將該字段設置爲f.active { [true, false].sample }。所以,有時工廠會創造積極的東西,有時會創造不活躍的東西。我有目的地創建了我的工廠,以這種方式行事,因爲我希望默認工廠儘可能廣泛地創建可能的現實世界事物。我只需要記住某些方面需要手動設置 - 就像在這種情況下,我只需要創建活動的東西。

的修復方法就是改變我的規格僅創建活動的事情:

@things = FactoryGirl.create_list(:thing, 5, :active => true) 
+0

我剛剛在我的用戶工廠遇到了這個問題:'factory:user do sequence(:email){| n | 「foo#{n}@burningpony.com」} sequence(:username){| n | 「foo#{n}@burningpony.com」}'。非常感謝! – aceofbassgreg 2013-03-20 00:38:00

相關問題