насмешка ошибки/исключения в rspec (не только его тип)

У меня есть блок кода как это:

def some_method
  begin
    do_some_stuff
  rescue WWW::Mechanize::ResponseCodeError => e
    if e.response_code.to_i == 503
      handle_the_situation
    end
  end
end

Я хочу протестировать то, что продолжается в этом if e.response_code.to_i == 503 раздел. Я могу дразнить do_some_stuff для броска правильного типа исключения:

whatever.should_receive(:do_some_stuff).and_raise(WWW::Mechanize::ResponseCodeError)

но как я дразню сам ошибочный объект для возврата 503, когда он получает "response_code"?

13
задан hoff2 14 January 2010 в 22:18
поделиться

1 ответ

require 'mechanize'

class Foo

  def some_method
    begin
      do_some_stuff
    rescue WWW::Mechanize::ResponseCodeError => e
      if e.response_code.to_i == 503
        handle_the_situation
      end
    end
  end

end

describe "Foo" do

  it "should handle a 503 response" do
    page = stub(:code=>503)
    foo = Foo.new
    foo.should_receive(:do_some_stuff).with(no_args)\
    .and_raise(WWW::Mechanize::ResponseCodeError.new(page))
    foo.should_receive(:handle_the_situation).with(no_args)
    foo.some_method
  end

end
22
ответ дан 1 December 2019 в 22:23
поделиться
Другие вопросы по тегам:

Похожие вопросы: