ruby-on-rails - Stub Net::HTTP在 rspec 中创建了响应

标签 ruby-on-rails rspec net-http

我是在另一个网站上上传图像文件,他们提供了相同的上传API。

文件上传过程分为2部分

  1. 创建图片 ID (POST)
  2. 上传图片(PUT)

当我创建图像 id 时,它会在响应中返回 id。为此,我添加了以下方法来实现此功能。

  def create_image_id
    response = create_via_api(data)
    # we get response in the following format
    # #<Net::HTTPCreated 201 Created readbody=true>
    find_id_from_response(response)
  end

  def create_via_api(data)
    access_token.post(IMAGE_CREATE_URL, data, 'Accept' => ACCEPT_HEADER)
  end

  # We are getting id from the response headers in the following format
  # location: SITE_URL/imageResources/5ac2acb2
  def find_id_from_response(response)
    id = response.to_hash['location'].first
    return unless id
    id.split('/').last.chomp
  end

现在我必须为 create_image_id 方法编写测试用例。

对于测试用例来说,与第三方 API 进行通信并不是一个好的做法。所以我对 POST 响应进行 stub ,说,

  allow(Image).to receive(:find_id_from_response).and_return('1234')

所以它总是返回 id 为 123,以便我可以将测试用例编写为

  expect(image. create_image_id).to eq 1234

如您所见,find_id_from_response 采用参数 (#)。

注意:这是响应的 header

  [36] pry(#)> response.to_hash
  {
  "content-type"=>["text/plain"],
  "x-frame-options"=>["SAMEORIGIN"],
  "location"=>["www.example.com/imageResources/cd87b8ef"],
  "vary"=>["Accept-Encoding"],
  "cache-control"=>["max-age=0, no-cache"],
  "pragma"=>["no-cache"],
  "date"=>["Tue, 15 Nov 2016 12:01:56 GMT"],
  "connection"=>["close"]
  }

我尝试了以下

  [28] pry()> Net::HTTPCreated.new(1, 201, 'Created')
  => #<Net::HTTPCreated 201 Created readbody=false>
  [29] pry()> a = Net::HTTPCreated.new(1, 201, 'Created')
  => #<Net::HTTPCreated 201 Created readbody=false>
  [30] pry()>)> a.to_hash
  => {}

它返回空哈希。那么如何 stub create_via_api 的响应呢?

请告诉我您需要的任何信息。

最佳答案

I don't think有一种简单的方法返回 http 响应对象。因此,您可以使用 rspec mocks

response_hash = {
  "content-type"=>["text/plain"],
  "x-frame-options"=>["SAMEORIGIN"],
  "location"=>["www.example.com/imageResources/cd87b8ef"],
  "vary"=>["Accept-Encoding"],
  "cache-control"=>["max-age=0, no-cache"],
  "pragma"=>["no-cache"],
  "date"=>["Tue, 15 Nov 2016 12:01:56 GMT"],
  "connection"=>["close"]
}
response_double = double
allow(response_double).to receive(:to_hash).and_return(response_hash)
allow(instance).to receive(:create_via_api).and_return(response_double)
expect(instance.create_via_api("data").to_hash['content-type']).to eq(["text/plain"])

另一个选择是使用记录和保存 api 调用进行测试的工具,并使用缓存的响应来执行所有后续测试,例如 vcr .

关于ruby-on-rails - Stub Net::HTTP在 rspec 中创建了响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40611204/

相关文章:

ruby - 通过 open-uri 优雅地使用 Kernel#open for https

ruby-on-rails - Rails 查询其所有关联记录在某些特定字段中为零或某些特定值的所有记录

ruby-on-rails - 在 Rails 中检查和验证非模型参数的位置

Emacs:如何运行 RSpec?

ruby-on-rails - ruby Class.new 在 rspec 中返回 nil

ruby - 如何使用 Net::Http 为 Goo.gl 缩短服务设置 header ?

ruby-on-rails - 是否可以在日期属性范围内设置 CanCan 能力?

ruby-on-rails - 从 belongs_to 关联访问海量信息

ruby-on-rails - Rails 和 RSpec : Testing controllers with the same name in different namespace (module)

ssl - 如何在 GO 中创建带有 ca 证书的 tls 客户端?