2010-08-12 41 views
3

這裏是我的代碼:Rspec的:無法理解 「and_raise」 方法

account_double.should_receive(:withdraw).and_raise 

我得到的輸出:

1) Statement uses the customer's name in the header 
Failure/Error: Unable to find matching line from backtrace 
Exception 
# ./spec/codebreaker/test.rb:8:in `generate' 
# ./spec/codebreaker/test.rb:20 
# /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `inject' 

它找不到異常。我怎麼能通過這個測試?

我不明白的圖片

回答

6

嘗試添加參數and_raise - 無論是一個Exception類或一個字符串傳遞給RuntimeError

從文檔:

告訴模擬募集一個異常而不是返回一個值。 可以是任何Exception類, 任何Exception類的實例或String(在這種情況下,RuntimeError將以該String作爲其消息被引發 )。

account_double.should_receive(:withdraw).and_raise(Exception) 
+0

爲什麼會出現這種'and_raise'傾錯誤的rspec的記錄?它不應該在內部失敗並由應用程序處理嗎?我覺得這就是測試的內容,但是當我運行一個預期或存根加註的規格時,我會得到一個巨大的回溯。 – Volte 2016-10-17 20:15:41