在RSpec验收测试中使用Grape API的哈希参数

时间:2018-07-20 13:04:56

标签: ruby-on-rails ruby-on-rails-4 rspec-rails

我的rails应用程序提供用葡萄编写的api。我正在使用rspec验收测试来测试api。

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

我的接受规范

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

我对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"]}}

如何定义参数,以便规范通过?

1 个答案:

答案 0 :(得分:0)

最后,它是在rspec_api_documentation的{​​{3}}中找到的。您可以使用scope的特殊值parameter将其形成为哈希值

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