這是間歇性故障測試:爲什麼這會請求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
運行規格時(似乎不是一旦失敗,它總是失敗,反之亦然),似乎沒有任何成功/失敗的模式。當規格運行通過guard
(spork
也被使用)時,可能會有一種模式,因爲如果我啓動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件事情。
所以,我需要知道爲什麼這個規範間歇性失敗,或者我能做些什麼來找出導致問題的原因。有任何想法嗎?
您可以發佈您工廠的事?還有對模型的任何限制 - 可能有時候東西沒有被創建? – micapam 2013-02-18 00:31:18
是的!謝謝!問題是我有一個'active'字段,我在我的工廠中設置爲'f.active {[true,false] .sample}'。所以,有時它會成爲一件活躍的事情,有時它會使事情失去活力。只要測試使第一件事情無效,規範就沒有什麼可以標記爲最喜歡的。我的修改是將我的規範改爲'@things = FactoryGirl.create_list(:thing,5,:active => true)',這樣它只會爲這個規範創建活躍的東西。 – 2013-02-18 01:03:21
很高興你有這種排序:)我不得不處理類似的問題,使用加工商。 – micapam 2013-02-18 04:45:54