Ruby - Airbourne Rspec API测试

问题描述 投票:0回答:1

我正在尝试编写api测试,但我无法弄清楚如何做到这一点。我将卷曲转换为红宝石并得到了如下的块

require 'net/http'
require 'uri'

uri = URI.parse("https://example.com/api/v2/tests.json")
request = Net::HTTP::Get.new(uri)
request.basic_auth("[email protected]", "Abcd1234")

req_options = {
  use_ssl: uri.scheme == "https",
}

response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http|
  http.request(request)
end

我写了如下测试

describe 'Test to GET' do
  it 'should return 200' do
  
  expect_json_types(name: :string)
  expect_json(name: 'test')
    expect_status(200)
  end
end

我的问题如何使用api调用来测试它。我应该将它添加到单独的文件中还是在上面描述的同一文件中。我之前没有使用Ruby,也无法在线找到任何东西。

ruby-on-rails ruby api rspec rspec-rails
1个回答
1
投票

您正在使用airborne,它使用rest_client进行API调用。为了使用空中的匹配器(expect_json等),您需要在测试中进行API调用。这意味着您的测试应该如下所示:

describe 'Test to GET' do
  it 'should return 200' do
    authorization_token = Base64.encode64('[email protected]:Abcd1234')
    get(
      "https://example.com/api/v2/tests.json",
      { 'Authorization' => "Basic #{authorization_token}" }
    )
    expect_json_types(name: :string)
    expect_json(name: 'test')
    expect_status(200)
  end
end
© www.soinside.com 2019 - 2024. All rights reserved.