0
votes

I'm building a Rails backend API and i have a controller that receives post requests. I'm using rspec to make a call to the endpoint and so far it looks like it working. However i am unable to visualise my response. (the controller works and renders the correct JSON). My spec looks like this:

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

This test passes, but i don't seem to be able to retrieve the rendered JSON. i've tried putting the following in the describe block:

puts response_body
puts response.body
puts page.body

Does anyone know the correct syntax?

2
should be a controller specapneadiving
Why, what does that change? I tried but i still get the same errorsMischa
in requests specs, you should only use visit, and then interact with the browserapneadiving
Ok, but i still can't get a response even when i change to :controllerMischa
@apneadiving: "Capybara is no longer supported in request specs as of Capybara 2.0.0." - relishapp.com/rspec/rspec-rails/docs/request-specs/request-specAndy Waite

2 Answers

1
votes

Within the describe block, your test needs to be within an it or specify block.

Edit - I see you're using the rspec_api_documentation gem. I'm not familiar with this, but it looks like you're mixing together two styles of syntax -- the gem's DSL, and RSpec's own DSL. The gem's README does not show describe being used. I would suggest sticking with plain RSpec until you get it passing, then try introduce the gem.

1
votes

So i figured it out with some help from a friend. This is the correct syntax with use of the 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