Rspec期望触发两次

Rspec期望触发两次,rspec,Rspec,在Rspec(3.9版)中测试自定义类时出现一些奇怪的行为 代码 class AlertMaster def initialize(params = {}) ... end def send_alert 1.upto(2) do |attempt| begin self.alert_me break rescue => e

在Rspec(3.9版)中测试自定义类时出现一些奇怪的行为

代码

class AlertMaster
    def initialize(params = {})
        ...
    end

    def send_alert
        1.upto(2) do |attempt|
            begin
                self.alert_me
                break
            rescue => e
                if attempt == 2
                    puts ">> here"
                    puts attempt
                    Rails.logger.fatal "Alert error - #{e.message}"
                end
            end
        end
    end
end
RSpec

    describe "send_alert" do
        before do
            @at = AlertMaster.new
        end
        describe "catches appropriately" do
            describe "first time succeeds" do
                it "should not log error" do
                    expect(@at).to receive(:alert_me).and_return(true)
                    expect(Rails.logger).to_not receive(:fatal)

                    @at.send_alert
                end
            end

            describe "first time fails, second time succeeds" do
                it "should not log error" do
                    expect(@at).to receive(:alert_me).and_raise("oops")
                    expect(@at).to receive(:alert_me).and_return(true)
                    expect(Rails.logger).to_not receive(:fatal)

                    @at.send_alert
                end
            end

            ####### FAILING SPEC
            describe "first time fails, second time fails" do
                it "should log error" do
                    expect(@at).to receive(:alert_me).and_raise("oops")
                    expect(Rails.logger).to receive(:fatal).with("Alert error - oops")

                    @at.send_alert
                end
            end
        end
    end
最后一个规范失败,因为调用了两次
Rails.logger
。控制台日志显示了我在最后一次尝试时正确输入的
put
语句。规格输出失败:

>> here
2

...

Failure/Error: expect(@at).to receive(:alert_me).and_raise("oops")
     
       (#<AlertMaster:0x00007ffca7edd180 ...).alert_me(*(any args))
           expected: 1 time with any arguments
           received: 2 times with any arguments
>这里
2.
...
失败/错误:期望(@at.)接收(:警报)和发出(“oops”)
(#
最后一个规范失败,因为Rails.logger被调用了两次

否,上一个规范失败,因为调用了两次
alert\u me
,但您只希望调用一次

Failure/Error: expect(@at).to receive(:alert_me).and_raise("oops")
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                     
       (#<AlertMaster:0x00007ffca7edd180 ...).alert_me(*(any args))
           expected: 1 time with any arguments
           received: 2 times with any arguments
最后一个规范失败,因为Rails.logger被调用了两次

否,上一个规范失败,因为调用了两次
alert\u me
,但您只希望调用一次

Failure/Error: expect(@at).to receive(:alert_me).and_raise("oops")
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                     
       (#<AlertMaster:0x00007ffca7edd180 ...).alert_me(*(any args))
           expected: 1 time with any arguments
           received: 2 times with any arguments