RSpec重试抛出异常然后返回值

Mat*_*lda 18 ruby rspec ruby-on-rails

我有一个重试块

 def my_method
    app_instances = []
    attempts = 0
    begin 
      app_instances = fetch_and_rescan_app_instances(page_n, policy_id, policy_cpath)
    rescue Exception
      attempts += 1
      retry unless attempts > 2
      raise Exception 
    end
    page_n += 1
  end
Run Code Online (Sandbox Code Playgroud)

其中fetch_and_rescan_app_instances接入网络等都可以抛出异常.

我想编写一个rspec测试,它第一次抛出异常并且第二次调用它时不会抛出异常,所以我可以测试第二次它是不会抛出异常,my_method不会抛出异常exeption.

我知道我可以做stub(:fetch_and_rescan_app_instances).and_return(1,3),第一次它返回1和第二次3,但我不知道如何做第一次抛出异常并返回第二次.

Chr*_*erg 21

您可以计算块中的返回值:

describe "my_method" do
  before do
    my_instance = ...
    @times_called = 0
    my_instance.stub(:fetch_and_rescan_app_instances).and_return do
      @times_called += 1
      raise Exception if @times_called == 1
    end
  end

  it "raises exception first time method is called" do
    my_instance.my_method().should raise_exception
  end

  it "does not raise an exception the second time method is called" do
    begin
      my_instance.my_method()
    rescue Exception
    end
    my_instance.my_method().should_not raise_exception
  end
end
Run Code Online (Sandbox Code Playgroud)

请注意,你真的不应该从中拯救Exception,使用更具体的东西.请参阅:为什么在Ruby中"拯救Exception => e`是一种糟糕的风格?

  • 请注意,当使用 `allow` 语法(通常可能只是 rspec3)时,您省略了 `and_return`:`allow(my_instance).to receive(:fetch_and_rescan_app_instances) do...` (2认同)

dol*_*nko 14

你所做的是限制接收消息的时间(接收计数),即在你的情况下你可以

instance.stub(:fetch_and_rescan_app_instances).once.and_raise(RuntimeError, 'fail')
instance.stub(:fetch_and_rescan_app_instances).once.and_return('some return value')
Run Code Online (Sandbox Code Playgroud)

instance.fetch_and_rescan_app_instances第一次调用会引发RuntimeError,第二次会返回'some return value'.

PS.调用更多信息会导致错误,您可以考虑使用不同的接收计数规范https://www.relishapp.com/rspec/rspec-mocks/docs/message-expectations/receive-counts


xmj*_*mjw 5

这在RSpec3.x中有所改变.似乎最好的方法是将块传递给receive定义此类行为的块.

以下内容来自提示如何创建此类传输失败的文档:

(这种错误每隔一段时间就会被调用......但很容易适应.)

RSpec.describe "An HTTP API client" do
  it "can simulate transient network failures" do
    client = double("MyHTTPClient")

    call_count = 0
    allow(client).to receive(:fetch_data) do
      call_count += 1
      call_count.odd? ? raise("timeout") : { :count => 15 }
    end

    expect { client.fetch_data }.to raise_error("timeout")
    expect(client.fetch_data).to eq(:count => 15)
    expect { client.fetch_data }.to raise_error("timeout")
    expect(client.fetch_data).to eq(:count => 15)
  end
end
Run Code Online (Sandbox Code Playgroud)