代码之家  ›  专栏  ›  技术社区  ›  Daniel Viglione

你什么时候会在Rspec中使用间谍而不是替身?

  •  0
  • Daniel Viglione  · 技术社区  · 8 年前

    我能理解测试双精度在Rspec中的应用。您可能需要测试一个方法,该方法有一个执行昂贵网络查询的对象。因此,您可以使用fill(test double)来返回昂贵操作所需的值:

    class Contact
      def update
        @api_result = AmazonAPI.perform_expensive_task
        self.status = @api_result['status']
        self.last_checked = @api_result['last_checked']
        save!
      end
    end
    
    describe Contact do
      subject { Contact.new }
    
      describe '#update' do
        it "updates contact with api criteria" do 
          api = double('Amazon API')
          allow(api).to receive(:perform_expensive_task).and_return({ status: 1, last_checked: Time.now })
          subject.update
          expect(subject.status).to eq 1
        end
      end
    end
    

    但后来我遇到了间谍。我看不出这有什么用。这是教程中提供的示例:

    let(:order) do
      spy('Order', process_line_items: nil, charge_credit_card: true, send_email: true)
    end
    
    before(:example) do
      order.process_line_items
      order.charge_credit_card
      order.send_email
    end
    
    it 'calls #process_line_items on the order' do
      expect(order).to have_received(:process_line_items)
    end
    
    it 'calls #charge_credit_card on the order' do
      expect(order).to have_received(:charge_credit_card)
    end
    
    it 'calls #send_email on the order' do
      expect(order).to have_received(:send_email)
    end
    

    这个特定的示例显式地调用了三个方法,稍后它将检查是否调用了这三个方法。当然,它叫他们。它在测试中做得很好。在现实中,我应该什么时候使用间谍?

    2 回复  |  直到 8 年前
        1
  •  1
  •   rewritten    8 年前

    间谍跟踪对其进行的呼叫(特别是它发送的消息)。因此,每当需要断言对协作者进行了特定调用时,就使用间谍。

    间谍是保证你的目标正确合作的工具。

    更新

    https://stackoverflow.com/a/52717158/384417

    一个带有代码的简单用例是logger:

    class SomeCommand
      def call(arg:, other:)
        if arg <= 0
          logger.warn("args should be positive")
        else
          logger.debug("all fine")
        end
        # more
      end
    
      def logger
        Rails.logger # for instance
      end
    end
    
    describe SomeCommand
      let(:logger) { spy('Logger') }
    
      # replace collaborator
      before { allow(subject).to receive(:logger) { logger } }
    
      context 'with negative value' do
        it 'warns' do
          subject.call(arg: -1, other: 6)
          expect(logger).to have_received(:warn)
          expect(logger).not_to have_received(:debug)
        end
      end
    
      context 'with positive value' do
        it 'logs as debug' do
          subject.call(arg: 1, other: 6)
          expect(logger).not_to have_received(:warn)
          expect(logger).to have_received(:debug)
        end
      end
    end
    
        2
  •  0
  •   Greg    8 年前

    rewritten wrote

    间谍给你一些灵活性。如果需要检查是否调用了该方法,则可以使用before中的mock执行此操作:

    before do
      expect(foo).to receive(:do_stuff)
    end
    
    specify do
      bar.run
    end
    

    但是 before supposed to separate setup, run and test stages . 您可以这样做:

    specify do 
      expect(foo).to receive(:do_stuff)
      bar.run
    end
    

    但这样做看起来更好

    before do
      bar.run
    end
    
    specify do
      expect(foo).to have_received(:do_stuff)
    end
    

    当有更多的东西需要检查时,你会有更干净的代码

    before { bar.run }
    
    specify do
      expect(foo).to have_received(:do_stuff)
    end
    
    context 'with some special conditions' do
      before { set_up_special_conditions }
    
      specify do
        expect(foo).not_to have_received(:do_stuff)
      end
    end
    

    这也许没什么大不了的,你还可以忍受

    specify do
      bar.run
      expect(foo).to have_received(:do_stuff)
    end
    
    context 'with some special conditions' do
      before { set_up_special_conditions } # * check the note at the bottom
    
      specify do
        bar.run
        expect(foo).not_to have_received(:do_stuff)
      end
    end
    

    但我认为定义上下文的一个好方法是只有本质的区别( set_up_special_conditions expect(foo).not_to have_received(:do_stuff) 在示例中)明确提到。任何与上下文“上面”没有区别的内容都不应出现在更具体的上下文“下面”。它有助于管理更大的规格。

    之前 以这种方式定义的块 rspec docs 我不确定这个订单能不能保证。那一刻我无法确认。但为了演示的目的,我们可以假装 before { set_up_special_conditions } 将在之前运行 before { bar.run }