ruby-on-rails - Ruby - Airbourne Rspec API 测试
问题描述
我正在尝试编写一个 api 测试,但我不知道该怎么做。我将 curl 转换为 ruby 并得到了如下所示的块
require 'net/http'
require 'uri'
uri = URI.parse("https://example.com/api/v2/tests.json")
request = Net::HTTP::Get.new(uri)
request.basic_auth("test@gmail.com", "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,也无法在网上找到任何东西。
解决方案
您正在使用使用rest_client进行 API 调用的airborne 。为了使用 airborne 的匹配器(等),您需要在测试中进行 API 调用。这意味着您的测试应如下所示:expect_json
describe 'Test to GET' do
it 'should return 200' do
authorization_token = Base64.encode64('test@gmail.com: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