rails/rspec 向 API 发出 post 请求时如何获得响应?
rails/rspec how to get the response when making a post request with body to API?
我正在构建一个 Rails 后端 API 并且我有一个控制器可以接收 post 请求。我正在使用 rspec 调用端点,到目前为止它看起来工作正常。但是我无法想象我的反应。 (控制器工作并呈现正确的 JSON)。我的规范如下所示:
require "spec_helper"
require "rspec_api_documentation/dsl"
resource "Data" do
header "Accept", "application/json"
header "Content-Type", "application/json"
header "Host", "public.example.com"
describe "receives POST request with body", :type => :request do
params = { type: ["1", "2", "3"], fromDate: "26/05/2015", toDate: "30/05/2015" }
post "/api/v1/data/totals", params.to_json
end
end
此测试通过,但我似乎无法检索呈现的 JSON。我试过将以下内容放在描述块中:
puts response_body
puts response.body
puts page.body
有人知道正确的语法吗?
在 describe
块内,您的测试需要在 it
或 specify
块内。
编辑 - 我看到您正在使用 rspec_api_documentation gem。我对此不熟悉,但看起来您将两种语法风格混合在一起——gem 的 DSL 和 RSpec 自己的 DSL。 gem 的自述文件没有显示正在使用 describe
。我建议坚持使用普通 RSpec 直到你通过它,然后尝试引入 gem.
所以我在朋友的帮助下弄明白了。这是使用 rspec api documentation/dsl:
的正确语法
require "spec_helper"
require "rspec_api_documentation/dsl"
resource "Data" do
header "Accept", "application/json"
header "Content-Type", "application/json"
header "Host", "public.example.com"
describe "Post a request to totals" do
parameter :type,"selected data type"
parameter :fromDate, "start date"
parameter :toDate, "end date"
let(:type) { ["1", "2", "3"] }
let(:fromDate) { "26/05/2015" }
let(:toDate) { "30/05/2015" }
post "/api/v1/data/totals" do
let(:raw_post) { params.to_json }
example_request "with a body" do
expect(response_body).to include(fromDate, toDate)
expect(status).to eq(200)
end
end
end
end
我正在构建一个 Rails 后端 API 并且我有一个控制器可以接收 post 请求。我正在使用 rspec 调用端点,到目前为止它看起来工作正常。但是我无法想象我的反应。 (控制器工作并呈现正确的 JSON)。我的规范如下所示:
require "spec_helper"
require "rspec_api_documentation/dsl"
resource "Data" do
header "Accept", "application/json"
header "Content-Type", "application/json"
header "Host", "public.example.com"
describe "receives POST request with body", :type => :request do
params = { type: ["1", "2", "3"], fromDate: "26/05/2015", toDate: "30/05/2015" }
post "/api/v1/data/totals", params.to_json
end
end
此测试通过,但我似乎无法检索呈现的 JSON。我试过将以下内容放在描述块中:
puts response_body
puts response.body
puts page.body
有人知道正确的语法吗?
在 describe
块内,您的测试需要在 it
或 specify
块内。
编辑 - 我看到您正在使用 rspec_api_documentation gem。我对此不熟悉,但看起来您将两种语法风格混合在一起——gem 的 DSL 和 RSpec 自己的 DSL。 gem 的自述文件没有显示正在使用 describe
。我建议坚持使用普通 RSpec 直到你通过它,然后尝试引入 gem.
所以我在朋友的帮助下弄明白了。这是使用 rspec api documentation/dsl:
的正确语法require "spec_helper"
require "rspec_api_documentation/dsl"
resource "Data" do
header "Accept", "application/json"
header "Content-Type", "application/json"
header "Host", "public.example.com"
describe "Post a request to totals" do
parameter :type,"selected data type"
parameter :fromDate, "start date"
parameter :toDate, "end date"
let(:type) { ["1", "2", "3"] }
let(:fromDate) { "26/05/2015" }
let(:toDate) { "30/05/2015" }
post "/api/v1/data/totals" do
let(:raw_post) { params.to_json }
example_request "with a body" do
expect(response_body).to include(fromDate, toDate)
expect(status).to eq(200)
end
end
end
end