Rspec Can';无法通过端点规范

Rspec Can';无法通过端点规范,rspec,rails-api,Rspec,Rails Api,我正在尝试测试以下代码 # spec/controllers/api/v1/item_types_controller_spec.rb let(:valid_session) { {} } let(:invalid_attributes) { { } } describe "with invalid params" do it "assigns a newly created but unsaved item_type as @item_type" do pos

我正在尝试测试以下代码

# spec/controllers/api/v1/item_types_controller_spec.rb

let(:valid_session) { {} }

let(:invalid_attributes) {
        { }
}

describe "with invalid params" do
  it "assigns a newly created but unsaved item_type as @item_type" do
    post :create, {type: 'item_types', data: { attributes: invalid_attributes}} , valid_session
    expect(assigns(:item_type)).to be_a_new(ItemType)
  end
end

# app/controllers/api/v1/item_types_controller.rb

module Api
  module V1
    class ItemTypesController < ApplicationController
      def create
        @item_type = ItemType.new(item_type_params)

        if @item_type.save
          render json: @item_type, status: :created, location: api_v1_item_type_url(@item_type)
        else
          render json: @item_type.errors, status: :unprocessable_entity
        end
      end

      private

      def item_type_params
        params.require(:data).permit(attributes: [ :name ] )
      end
    end
  end
end

class ItemType < ApplicationRecord
  validates_presence_of :name
end
我不知道发生了什么,因为我的测试显然是在传递带有空散列的属性的数据。任何关于如何编写测试以使其通过并允许我测试通过无效属性的输入都将受到欢迎


注意:当我编辑
无效的_参数
以包括
{name:'}
时,测试通过。我本以为我可以传递一个空散列?

可能的重复不是重复,因为它与空数组无关,它与一个空散列有关。在对rails进行更多搜索之后,这是一个问题,最好的解决方法是使用默认的fetch,而不是require-
params.fetch(:data,{}).permit(attributes:[:name])
可能的重复项不是重复项,因为它与空数组无关,它与空哈希有关。在对rails进行更多搜索之后,这是一个问题,最好的解决方法是使用默认的fetch,而不是require-
params.fetch(:data,{}).permit(attributes:[:name])
Failures:

  1) Api::V1::ItemTypesController POST create with invalid params assigns a newly created but unsaved item_type as @item_type
     Failure/Error: post :create, {type: 'item_types', data: { attributes: invalid_attributes}} , valid_session
     ActionController::ParameterMissing:
       param is missing or the value is empty: data