将补丁与rspec测试一起使用

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

我正在尝试使用Rspec测试补丁来设计用户信息,更新URL看起来像这样# PATCH/PUT /api/users/1,但在以下所有情况下都遇到此错误

错误ArgumentError: wrong number of arguments (given 2, expected 1)

我尝试过的情况

patch :update, {'id'=> @api_user['user']['id'], 'user' => attributes_for(:normal_user)}

patch :update, 'id'=> @api_user['user']['id'], 'user' => attributes_for(:normal_user)

patch :update, 'id'=> @api_user['user']['id'], :params => {'user' => attributes_for(:normal_user)}

我尝试过patch :update, :params => {'user' => create(:normal_user)}。 #这个ID在

但给出此错误

No route matches {:action=>"update", :controller=>"api/users", :user=>#<User id: 227794695, email: "[email protected]", created_at: "2020-05-03 08:51:55", updated_at: "2020-05-03 08:51:55", is_admin: nil, first_name: "test", last_name: "test">}表示,网址应为update/id

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

您不应该在补丁后加上“更新”,因为自行修补程序将自动路由以更新到用户控制器

这是给您2个错误消息的原因,预期有1个参数

这里是示例,供您参考更新

require 'rails_helper'

RSpec.describe 'User request', type: :request do
  it 'should update user email' do
    patch "/api/users/#{@api_user['user']['id']}",
          params: {
            user: {
              email: '[email protected]'
            }
          },
          as: :json
    expect(response).to have_http_status(:success)
  end
end
© www.soinside.com 2019 - 2024. All rights reserved.