0

我正在尝试编写一个 RSpec 测试,使用 Boxr gem ( https://github.com/cburnette/boxr ) 对 api.box.com 的请求进行存根。我创建了一个存根,它使用 #to_rack 方法转到一个假 Box 类

stub_request(:any, %r{^https?://api.box.com}).to_rack(FakeBox)

和我的 FakeBox 课程:

require 'sinatra/base'

class FakeBox < Sinatra::Base
  post /oauth2/ do
    debugger
    content_type :json
    status 200
    '{ "access_token": "<some_token>", "expires_in": 3979, "restricted_to": [],' +
     '"refresh_token": "<some_refresh>",' +
     '"token_type": "bearer" }'
  end
end

每当我调用Net::HTTP.get('https://api.box.com/oauth2/token')HTTPClient.new.post(...)直接在控制器或测试方法中都会触发这个假动作。

Boxr::get_tokens(params[:code]) 但是,当我调用我在 Boxr gem 本身中放置一个调试器时,它不会被触发:

def self.auth_post(uri, body)
    uri = Addressable::URI.encode(uri)

    res = BOX_CLIENT.post(uri, body: body)
    debugger

    if(res.status==200)
      body_json = Oj.load(res.body)
      return BoxrMash.new(body_json)
    else
      raise BoxrError.new(status: res.status, body: res.body, header: res.header)
    end
  end

BOX_CLIENT 在这里定义:

module Boxr
  Oj.default_options = {:mode => :compat }

  #The root folder in Box is always identified by 0
  ROOT = 0

  #HTTPClient is high-performance, thread-safe, and supports persistent HTTPS connections
  #http://bibwild.wordpress.com/2012/04/30/ruby-http-performance-shootout-redux/
  BOX_CLIENT = HTTPClient.new
  BOX_CLIENT.cookie_manager = nil
  BOX_CLIENT.send_timeout = 3600 #one hour; needed for lengthy uploads
  BOX_CLIENT.agent_name = "Boxr/#{Boxr::VERSION}"
  BOX_CLIENT.transparent_gzip_decompression = true 
  #BOX_CLIENT.ssl_config.add_trust_ca("/Users/cburnette/code/ssh-keys/dev_root_ca.pem")

  def self.turn_on_debugging(device=STDOUT)
    BOX_CLIENT.debug_dev = device
    BOX_CLIENT.transparent_gzip_decompression = false
    nil
  end

  def self.turn_off_debugging
    BOX_CLIENT.debug_dev = nil
    BOX_CLIENT.transparent_gzip_decompression = true
    nil
  end
end

当调试器被触发时,我得到的响应显示BOX_CLIENT.post(uri, body: body)访问了该站点并在我期望它触发模拟时返回了一个错误。在该断点处的调试控制台中,我进入HTTPClient.new.post(uri, body: body)并得到了我期望的存根响应。

当我进入HTTPClient.new.class调试器控制台时,我得到:WebMockHTTPClient。BOX_CLIENT.class-> HTTP客户端。这似乎是我问题的根源。

我无法修改宝石。有谁知道如何解决这个问题?

4

1 回答 1

-1

只需require: falsegem 'webmock', require: falseGemfile中删除

于 2016-03-25T21:27:15.387 回答