如何使用 Grape 和 MongoDB 在 Rails 上的 Ruby 中正确测试 'post'(创建)方法

How to properly test a 'post' (create) method in Ruby on Rails with Grape and MongoDB

我在尝试测试创建新用户的 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: "new@new.new",
                                first_name: "first", last_name: "last" },
    assert last_response.ok?, "#{last_response.inspect}"
  end   
end

它 returns 一个响应说缺少参数 '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 hash,但我认为它没有被使用。相反,您应该将其作为没有顶级命名空间的常规 args 哈希传递:

  test 'POST /api/v1/users can create a user' do
    post "/api/v1/users", username: "new_user",
                          email: "new@new.new",
                          first_name: "first",
                          last_name: "last"

    assert last_response.ok?, "#{last_response.inspect}"
  end

告诉我它是否有效。