我在尝试测试创建新用户的 POST API 调用之一时遇到问题:
class API::V1::Users::APITest < ActiveSupport::TestCase
include Rack::Test::Methods
def app
Rails.application
end
test 'POST /api/v1/users can create a user' do
post "/api/v1/users", params: { username: "new_user", email: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e7898290a7898290c9898290" rel="noreferrer noopener nofollow">[email protected]</a>",
first_name: "first", last_name: "last" },
assert last_response.ok?, "#{last_response.inspect}"
end
end
它返回一个响应,指出参数“username”、“email”、“first_name”和“last_name”丢失:
#<Rack::MockResponse:0x007ffc34108b40 @original_headers={"Content-Type"=>"application/json", "Content-Length"=>"94", "Cache-Control"=>"no-cache", "X-Request-Id"=>"8a5b0a14-522c-4407-830c-28dc6402f52c", "X-Runtime"=>"0.009955", "Vary"=>"Origin"}, @errors="", @status=400, @header={"Content-Type"=>"application/json", "Content-Length"=>"94", "Cache-Control"=>"no-cache", "X-Request-Id"=>"8a5b0a14-522c-4407-830c-28dc6402f52c", "X-Runtime"=>"0.009955", "Vary"=>"Origin"}, @writer=#<Proc:0x007ffc34108708@/[FOLDERS]/vendor/bundle/gems/rack-2.0.1/lib/rack/response.rb:32 (lambda)>, @block=nil, @length=94, @body=["{\"error\":\"username is missing, email is missing, first_name is missing, last_name is missing\"}"]>
MongoDB 用户模型
class User
include Mongoid::Document
field :username, type: String
field :email, type: String
field :first_name, type: String
field :last_name, type: String
field :picture, type: String
field :video, type: String
field :twitter, type: String
field :phone, type: String
end
但是,我相信我已经正确设置了 POST 方法,因为当我在 Grape Swagger UI 中运行它时,它运行成功:
默认值
module API
module V1
module Defaults
extend ActiveSupport::Concern
included do
prefix "api"
version "v1", using: :path
default_format :json
format :json
helpers do
def permitted_params
@permitted_params ||= declared(params, include_missing: false)
end
def logger
Rails.logger
end
end
end
end
end
end
API
module API
module V1
class Users < Grape::API
include API::V1::Defaults
resource :users do
desc 'Create a user'
params do
requires :username, type: String, desc: 'Unique username'
requires :email, type: String, desc: 'Email'
requires :first_name, type: String, desc: 'First name'
requires :last_name, type: String, desc: 'Last name'
end
post do
User.create!({
username: params[:username],
email: params[:email],
first_name: params[:first_name],
last_name: params[:last_name]
})
end
end
end
end
end
Successful Swagger UI POST call
我做错了什么?我的语法正确吗?
最佳答案
试试这个。您传递了 params 哈希,但我认为它没有被使用。相反,您应该将其作为常规参数哈希传递,而无需顶级 namespace :
test 'POST /api/v1/users can create a user' do
post "/api/v1/users", username: "new_user",
email: "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5e303b291e303b2970303b29" rel="noreferrer noopener nofollow">[email protected]</a>",
first_name: "first",
last_name: "last"
assert last_response.ok?, "#{last_response.inspect}"
end
告诉我它是否有效。
关于ruby-on-rails - 如何使用 Grape 和 MongoDB 在 Ruby on Rails 中正确测试 'post'(创建)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39937391/