代码之家  ›  专栏  ›  技术社区  ›  ogirginc

如何使用minitest测试轮询外部API?

  •  0
  • ogirginc  · 技术社区  · 8 年前

    我试着把两者都模仿一下 BaseApi.authenticate BaseApi::TrackableJob (这是API请求)但失败。

    module BaseApis
      class PollTrackableJobWorker
        include Sidekiq::Worker
    
        ...
    
        def perform(job_id, _invoice_id)
          BaseApi.authenticate(1) do
            response = BaseApi::TrackableJob.find(job_id).first
    
            case response['status']
            when 'done' then true
            when 'error' then Bugsnag.notify(response['errors'])
            when 'pending' || 'running' then raise EDocumentNotSentError
            end
          end
        end
      end
    end
    

    笔记:

    1. 省略了与重试机制相关的代码。
    2. 我们使用 mocha gem .

    有人能帮忙吗?

    1 回复  |  直到 8 年前
        1
  •  0
  •   ogirginc    8 年前

    结果,我想的太多了, yields 方法为溶液:

    test 'status is done' do
      response = {
        'type'       => 'trackable_jobs',
        'id'         => '0b6ffad9177aeb594ad54af0',
        'status'     => 'done',
        'errors'     => nil
      }
    
      BaseApi.expects(:authenticate).with(1).yields
      BaseApi::TrackableJob.stubs(:find).returns([response])
    
      assert_equal(true, BaseApis::PollTrackableJobWorker.new.perform('xxx', 123))
    end