2012-02-28 51 views
2

我在水豚和rspec下,這是測試對象的銷燬鏈接的最佳方式? 我必須點擊此鏈接用水豚和rspec測試破壞行爲

<a href="/categories/1" data-confirm="Are you sure?" data-method="delete" rel="nofollow">Destroy</a> 

,但我不能與選擇它:

it "should destroy a category" do 
    visit categories_path 
    find(:xpath, '//link[@href="/categories/1"]').click 
    # how to handle javascript pop up for confirmation? 
end 

任何暗示? 謝謝!

回答

13
expect { click_link 'Destroy' }.to change(Category, :count).by(-1) 
4
it "should destroy a category" do 
expect { click_link('Destroy') }.to change(Category, :count).by(-1) 
end 
1

,因爲它看起來就好像這是一個要求規範,可以更緊密地與模擬用戶:

it "should destroy a category" do 
    visit categories_path 
    find(:xpath, '//link[@href="/categories/1"]').click 
    sleep 1.seconds 
    alert = page.driver.browser.switch_to.alert 
    expect { alert.accept }.to change(Category, :count).by(-1) 
end 

乾杯!