1
我有一個頁面,一個登錄的用戶可以通過提交其調用下面的控制方法Ajax請求設置一個最喜歡的項目:導軌 - Rspec的/水豚報告失敗,除非睡眠加入
def create
item = Item.find(params[:id])
if params[:commit] == 'save'
item.unhide!(current_user) if item.is_hidden_by?(current_user)
item.save_as_favourite_for!(current_user)
else
item.remove_as_favourite!(current_user) if item.is_favourite_of?(current_user)
item.hide_for!(current_user)
end
respond_to do |format|
format.html { redirect_back_or items_path }
format.js {render "create", locals: {item: item, index: params[:index].to_i || nil, page:params[:page]} }
end
end
這所有作品但我有一個請求規範,它被定義爲測試前端功能:
describe "saving items", js: true do
let!(:current_user) {FactoryGirl.create(:user, email: "[email protected]", active: true)}
let!(:favourite_item) {FactoryGirl.create(:item)}
before { sign_in current_user }
describe "when saving a item" do
it "should save the user against the item" do
expect do
click_button "save"
# sleep(0.02)
end.to change(favourite_item.savers, :count).by(1)
end
end
end
請注意註釋掉的睡眠(0.02)語句。如果這個語句被註釋掉了,那麼測試(幾乎)總是失敗,但是如果我包含這個調用,並且立即結束控制檯中斷,那麼測試每次都會通過。 (任何更多的時間延遲,它失敗)。
有沒有更好的方法來寫這個測試沒有解決方法,爲什麼它會失敗,沒有延誤?
這與添加睡眠區塊有何不同?不幸的是,無論如何它似乎在這種情況下工作。我會檢查我的測試來檢查,因爲我可能已經對js採取了自由主義的態度:這是真的,但我相信在大多數情況下,js都在發揮作用。 – Richbits 2014-10-17 10:19:54