【发布时间】:2019-03-13 00:13:22
【问题描述】:
我正在尝试编写一个 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,也无法在网上找到任何东西。
【问题讨论】:
标签: ruby-on-rails ruby api rspec rspec-rails