rspec 验收测试中 Grape API 的哈希参数

Hash params for Grape API in rspec acceptance test

我的 rails 应用程序提供 api 用 grape 编写的服务。我正在使用 rspec 验收测试来测试 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

我的验收规范

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

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

终于在rspec_api_documentationreadme中找到了。您可以使用 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