我在寫我的測試時遇到了一些困難,看看我的rescue
子句中的方法是否被調用。這裏是我的代碼:如何構建我的測試,以便測試我的救援條款?
def run
begin
fail BounceLimitError, 'Reached 5% Bounce Rate' if Reverification::Process.bounce_limit_reached?
rescue
Reverification::Process.start_polling_queues
end
resend_soft_bounced_notifications
send_notifications
end
測試文件:
it 'should not invoke notifications sending methods when bounce limit is > 5%' do
Reverification::Process.stubs(:bounce_limit_reached?).returns(true)
Reverification::Mailer.expects(:send_notifications).never
Reverification::Mailer.expects(:resend_soft_bounced_notifications).never
assert_raise Reverification::Mailer::BounceLimitError do
Reverification::Process.expects(:start_polling_queues)
Reverification::Mailer.run
end
end
我擡起頭來的文件和assert_raise
是要走的路,但這個測試產生不滿意的期望。有人能幫我弄清楚如何讓這個測試成功嗎?
-----編輯------
我試着用下面的代碼推薦的變化:
def run
fail BounceLimitError, 'Reached 5% Bounce Rate' if Reverification::Process.bounce_limit_reached?
resend_soft_bounced_notifications
send_notifications
rescue
Reverification::Process.start_polling_queues
end
it 'should not invoke notifications sending methods when bounce limit is > 5%' do
Reverification::Process.stubs(:bounce_limit_reached?).returns(true)
Reverification::Mailer.expects(:send_notifications).never
Reverification::Mailer.expects(:resend_soft_bounced_notifications).never
Reverification::Process.expects(:start_polling_queues)
Reverification::Mailer.run
end
當我執行測試的錯誤,現在是,這個例外是拋出。
1) Error:
run#test_0001_should not invoke notifications sending methods when bounce limit is > 5%:
Reverification::Mailer::BounceLimitError: Reached 5% Bounce Rate
lib/reverification/mailer.rb:18:in `run'
test/lib/reverification/mailer_test.rb:38:in `block (2 levels) in <class:MailerTest>'
測試通過這麼說是因爲拋出了一個錯誤,但是我怎樣才能讓它通過?
爲什麼要使用'rescue'?爲什麼不僅僅是'Reverification :: Process.start_polling_queues if Reverification :: Process.bounce_limit_reached?' – PJSCopeland