1

我试图在我附加的规范中覆盖以下代码部分。这个项目更喜欢在 VCR 上使用存根,并且外部 API 请求被阻止。有没有人对如何解决这个问题以获得所需的报道有任何想法?

我已将睡眠测试注释掉,因为它失败了。

代码

  def first_page
    client.list_orders(created_before: created_before,
                       created_after: created_after,
                       max_results_per_page: max_results_per_page)
  rescue Excon::Error::ServiceUnavailable => error
    log_error(error, 'amazon_mws.errors.orders.first_page') unless (self.retries -= 1).positive?
    add_api_delay
    retry
  end

  def next_page(next_token)
    client.list_orders_by_next_token(next_token)
  rescue Excon::Error::ServiceUnavailable => error
    log_error(error, 'amazon_mws.errors.orders.next_page') unless (self.retries -= 1).positive?
    add_api_delay
    retry
  end

  def add_api_delay
    sleep(Configurations.amazon_mws['orders']['response_timer_in_seconds'])
  end

规格

  describe '.poll_for_order_items' do
    context 'multiple pages' do
      before do
        stub_request(:any, /.*amazonservices.com.*/)
          .with(body: /^.*(&Action=ListOrderItems&).*$/)
          .to_return(body: next_page, status: 200, headers: { 'Content-Type': 'text/xml' })

        stub_request(:any, /.*amazonservices.com.*/)
          .with(body: /^.*(&Action=ListOrderItemsByNextToken&).*$/)
          .to_return(body: first_page, status: 200, headers: { 'Content-Type': 'text/xml' })
      end

      context 'with sleep stubbed' do
        before do
          allow(subject).to receive(:add_api_delay).and_return(true)
        end

        it 'returns array collection' do
          expect(subject.poll_for_order_items).to be_kind_of(Array)
        end

        it 'returns array data' do
          expect(subject.poll_for_order_items.length).to be > 0
        end
      end

      # context 'with sleep set' do
      #   it 'should call sleep' do
      #     allow(Kernel).to receive(:sleep).and_return(1)
      #     expect(subject).to receive(:sleep).and_return(1)
      #     subject.poll_for_order_items
      #   end
      # end
    end

    context 'single page' do
      before(:each) do
        stub_request(:any, /.*amazonservices.com.*/).to_return(body: first_page, status: 200, headers: { 'Content-Type': 'text/xml' })
        allow(subject).to receive(:add_api_delay).and_return(true)
      end

      it 'returns array' do
        expect(subject.poll_for_order_items).to be_kind_of(Array)
      end

      it 'returns array data' do
        expect(subject.poll_for_order_items.length).to be > 0
      end
    end

    context 'collector' do
      before(:each) do
        stub_request(:any, /.*amazonservices.com.*/).to_return(body: first_page_single, status: 200, headers: { 'Content-Type': 'text/xml' })
        allow(subject).to receive(:add_api_delay).and_return(true)
      end

      it 'returns array when non array returned' do
        expect(subject.poll_for_order_items).to be_kind_of(Array)
      end
    end
  end

  describe 'raises errors' do
    context '.parse' do
      let(:error_raised) { I18n.t('amazon_mws.errors.parse_payload') }

      before do
        stub_request(:any, /.*amazonservices.com.*/)
          .with(body: /^.*(&Action=ListOrderItems&).*$/)
          .to_return(body: {}.to_json, status: 200, headers: { 'Content-Type': 'text/xml' })
      end

      it 'raises error' do
        expect { subject.new } .to raise_error(StandardError)
      end
    end

    context '.first_page' do
      let(:error_raised) { I18n.t('amazon_mws.errors.order_items.first_page') }

      before do
        allow(MWS).to receive_message_chain(:orders, :list_order_items).and_raise(error_raised)
      end

      it 'raises error on first listing page' do
        expect { subject.new } .to raise_error(StandardError)
      end
    end

    context '.next_page' do
      let(:error_raised) { I18n.t('amazon_mws.errors.order_items.next_page') }

      before do
        stub_request(:any, /.*amazonservices.com.*/)
          .with(body: /^.*(&Action=ListOrderItems&).*$/)
          .to_return(body: next_page, status: 200, headers: { 'Content-Type': 'text/xml' })

        stub_request(:any, /.*amazonservices.com.*/)
          .with(body: /^.*(&Action=ListOrderItemsByNextToken&).*$/)
          .and_raise(error_raised)

        allow(subject).to receive(:add_api_delay).and_return(true)
      end

      it 'raises error on nexst listing page' do
        expect { subject.poll_for_order_items } .to raise_error(StandardError)
      end
    end
  end

覆盖报告 在此处输入图像描述

4

2 回答 2

2

我只看到您期望它会引发错误。如果它超时,那么它应该至少调用log_error一次add_api_delay。由于这些基本上是副作用,您可能只需要测试它们在这种情况下被调用并且覆盖率报告应该知道它。

尝试添加期望,例如:expect(subject).to have_received(:log_error).at_least(:once)

编辑:在带外谈论它,我们了解到大多数覆盖问题可以通过确保引发错误来解决。

before do stub_request(:any, /.*amazonservices.com.*/) .with(body: /^.*(&Action=ListOrders&).*$/) .to_raise(Excon::Error::ServiceUnavailable) end

于 2017-01-11T23:15:21.037 回答
1

经过以上@wobh的研究和启发,我想出了以下...

  describe '.poll_for_order_items' do
    context 'multiple pages' do
      before do
        stub_request(:any, /.*amazonservices.com.*/)
          .with(body: /^.*(&Action=ListOrderItems&).*$/)
          .to_return(body: next_page, status: 200, headers: { 'Content-Type': 'text/xml' })

        stub_request(:any, /.*amazonservices.com.*/)
          .with(body: /^.*(&Action=ListOrderItemsByNextToken&).*$/)
          .to_return(body: first_page, status: 200, headers: { 'Content-Type': 'text/xml' })
      end

      context 'with sleep stubbed' do
        before do
          allow(subject).to receive(:add_api_delay).and_return(true)
        end

        it 'returns array collection' do
          expect(subject.poll_for_order_items).to be_kind_of(Array)
        end

        it 'returns array data' do
          expect(subject.poll_for_order_items.length).to be > 0
        end
      end
    end

    context 'single page' do
      before(:each) do
        stub_request(:any, /.*amazonservices.com.*/).to_return(body: first_page, status: 200, headers: { 'Content-Type': 'text/xml' })
        allow(subject).to receive(:add_api_delay).and_return(true)
      end

      it 'returns array' do
        expect(subject.poll_for_order_items).to be_kind_of(Array)
      end

      it 'returns array data' do
        expect(subject.poll_for_order_items.length).to be > 0
      end
    end

    context 'collector' do
      before(:each) do
        stub_request(:any, /.*amazonservices.com.*/).to_return(body: first_page_single, status: 200, headers: { 'Content-Type': 'text/xml' })
        allow(subject).to receive(:add_api_delay).and_return(true)
      end

      it 'returns array when non array returned' do
        expect(subject.poll_for_order_items).to be_kind_of(Array)
      end
    end
  end

  describe 'raises errors' do
    context '.parse' do
      let(:error_raised) { I18n.t('amazon_mws.errors.parse_payload', error: 'TEST') }

      before do
        stub_request(:any, /.*amazonservices.com.*/)
          .with(body: /^.*(&Action=ListOrderItems&).*$/)
          .to_return(body: {}.to_json, status: 200, headers: { 'Content-Type': 'text/xml' })
      end

      it 'raises error' do
        expect { subject.poll_for_order_items } .to raise_error(StandardError)
      end

      it 'raises correct error message' do
        expect { subject.poll_for_order_items } .to raise_error(/payload/)
      end
    end

    context '.first_page' do
      let(:error_raised) { I18n.t('amazon_mws.errors.order_items.first_page', error: 'TEST') }

      before do
        stub_request(:any, /.*amazonservices.com.*/)
          .with(body: /^.*(&Action=ListOrderItems&).*$/)
          .to_raise(Excon::Error::ServiceUnavailable)
        subject.instance_variable_set(:@retry_limit, 1)
        allow(subject).to receive(:add_api_delay).and_return(true)
      end

      it 'raises error on first listing page' do
        expect { subject.poll_for_order_items } .to raise_error(StandardError)
      end

      it 'raises correct error message' do
        expect { subject.poll_for_order_items } .to raise_error(/order item first/)
      end
    end

    context '.next_page' do
      let(:error_raised) { I18n.t('amazon_mws.errors.order_items.next_page', error: 'TEST') }

      before do
        stub_request(:any, /.*amazonservices.com.*/)
          .with(body: /^.*(&Action=ListOrderItems&).*$/)
          .to_return(body: next_page, status: 200, headers: { 'Content-Type': 'text/xml' })

        stub_request(:any, /.*amazonservices.com.*/)
          .with(body: /^.*(&Action=ListOrderItemsByNextToken&).*$/)
          .to_raise(Excon::Error::ServiceUnavailable)
        subject.instance_variable_set(:@retry_limit, 1)
        allow(subject).to receive(:add_api_delay).and_return(true)
      end

      it 'raises error on next listing page' do
        expect { subject.poll_for_order_items } .to raise_error(StandardError)
      end

      it 'raises correct error message' do
        expect { subject.poll_for_order_items } .to raise_error(/order item next/)
      end
    end

    context '.add_api_delay' do
      before do
        stub_request(:any, /.*amazonservices.com.*/)
          .with(body: /^.*(&Action=ListOrderItems&).*$/)
          .to_raise(Excon::Error::ServiceUnavailable)

        subject.instance_variable_set(:@retries, 2)
        subject.instance_variable_set(:@retry_limit, 1)
        subject.instance_variable_set(:@retry_delay, 1)
      end

      it 'sleeps retry api calls' do
        allow(Kernel).to receive(:sleep).and_return(1)
        expect(subject).to receive(:sleep).and_return(1)
        expect { subject.poll_for_order_items } .to raise_error(StandardError)
      end
    end
  end

这导致了 100% 的覆盖率!

于 2017-01-13T08:08:21.173 回答