在rspec验收测试中用于葡萄API的哈希参数

问题描述 投票:0回答:1

我的rails应用程序提供用葡萄写的api。我正在使用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"]}}

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

ruby-on-rails ruby-on-rails-4 rspec-rails
1个回答
0
投票

最后,它发现在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
© www.soinside.com 2019 - 2024. All rights reserved.