2015-06-01 25 views
0

我正在构建一个Rails后端API,并且我有一个接收发布请求的控制器。我正在使用rspec调用端点,到目前为止它看起来像在工作。但是我无法想象我的回应。 (控制器工作并呈现正确的JSON)。我的规格如下所示:rails/rspec如何获得响应时,身体的API发布的请求?

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 

有谁知道正确的语法?

+0

应该是控制器规格 – apneadiving

+0

为什么,是什么改变?我尝试过,但我仍然在请求规范中得到了相同的错误 – Mischa

+0

,您应该只使用'visit',然后与浏览器进行交互 – apneadiving

回答

0

所以我找到了一些朋友的帮助。这是正确的语法与使用RSpec的API文档/ 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 
+0

找出一些帮助,请标记为正确答案。 –

+0

我会,如果我可以,必须等待2天。 (现在好了1小时) – Mischa

+0

啊,我没有意识到有延迟执行。 –

1

describe区块内,您的测试需要位于itspecify区块内。

编辑 - 我看到你正在使用rspec_api_documentation gem。我对此并不熟悉,但它看起来像是混合了两种格式的语法 - 宝石的DSL和RSpec自己的DSL。宝石的自述文件不显示正在使用的describe。我会建议坚持使用普通的RSpec,直到它通过,然后尝试引入宝石。

+0

想要但我正在处理一个现有的项目。通过 – Mischa

相关问题