Search code examples
ruby-on-railsruby-on-rails-4rspec-rails

Hash params for Grape API in rspec acceptance test


My rails app serves api written in grape. I am using rspec acceptance test to test the apis.

params do
  requires :invitation, type: Hash do
    requires :email, type: String, allow_blank: false, desc: "Email"
    requires :first_name, type: String, allow_blank: false, desc: "First Name"
    optional :last_name, type: String, allow_blank: true, desc: "Last Name"
    requires :message, type: String, allow_blank: true, desc: "Message"
  end
end

My acceptance spec

resource "Invite",acceptance: true do
  route '/v1/invite/send',name: "Invite" do
    parameter :first_name,type: String
    parameter :email,type: String
    post 'send invite' do
       context 'valid params' do
         example_request 'failed' do
           puts response_body
           expect(status).to be(200)
         end
       end
    end
  end
end

My output for rspec spec/acceptance/invite_spec.rb

{"error":{"status":400,"message":["Invitation is missing","Email is missing","First name is missing","Primary role is missing","Message is missing"]}}

How do I define param so the spec passes?


Solution

  • Finally it was found in rspec_api_documentation's readme. You can use, scope which is a special value for parameter to form it as a hash

    resource "Invite",acceptance: true do
      route '/v1/invite/send',name: "Invite" do
        parameter :first_name,type: String
        parameter :email,type: String, :scope => [:invitation]
        post 'send invite' do
           context 'valid params' do
             example_request 'failed' do
               puts response_body
               expect(status).to be(200)
             end
           end
        end
      end
    end