2013-05-25 30 views
0

我試圖用rspec測試一些關聯。 這是我的測試:Rspec測試與期望更改語法的關聯

1)

it 'creates an incubation with proper associations' do 
    expect{post :apply_startup, id: incubator.id, startup: {id: startup.id}} 
    .to change{incubator.pending_startups.count}.from(0).to(1) 
end 

2)

it 'creates an incubation with proper associations' do 
    expect{post :apply_startup, id: incubator.id, startup: {id: startup.id}} 
    .to change{incubator.pending_startups}.from([]).to([startup]) 
end 

這是apply_startup方法的定義:

def apply_startup 
    startup = Startup.find_by_id params[:startup][:id] 
    @incubator.pending_startups << startup if startup 
    redirect_to incubator_path(@incubator) 
end 

首先測試通過,但是第二失敗。

編輯#1(失敗規格的輸出)

2) IncubatorsController#apply creates an incubation with proper associations 
Failure/Error: expect{post :apply, id: incubator.id, startup: {id: startup.id}} 
    result should have initially been [], but was [#<Startup id: 665, ********] 

有人能解釋一下,爲什麼? Thanx

+0

RSpec關於第二次測試失敗的完整輸出是什麼? –

+0

對不起,剛更新了一個問題。 – RunFor

回答

0

看起來你的數據庫沒有在每個規範之間重置。你在使用database_cleaner嗎?

+0

是的,我是。我應該怎麼做才能解決這個問題? – RunFor