问题描述
在我的Rails 6应用程序中,我试图测试允许管理员用户更新用户数据而不提供用户密码的控制器方法。所有操作都在ActiveAdmin中运行。
admin/users.rb
controller do
def update
model = :user
%w[password password_confirmation].each { |p| params[model].delete(p) } if params[model][:password].blank?
super
end
end
基于this页,我尝试编写规范:
admin/users_spec.rb
describe 'PUT update' do
let(:user) { create(:user,:random_email) }
let(:valid_attributes) do
ActionController::Parameters.new(
{
user: {
email: 'michael.kelso@example.com',password: '',},)
end
before do
put :update,params: { id: user.id,user: valid_attributes }
end
it 'update user' do
expect(user.reload.email).to eq('michael.kelso@example.com')
end
end
end
我应该怎么做才能使测试变成绿色?
解决方法
您仍然可以为activeadmin添加强大的参数。
使用方法allow_params方法,例如:permit_params :title,:content,:publisher_id
activeadmin强参数文档: https://github.com/activeadmin/activeadmin/blob/master/docs/2-resource-customization.md#setting-up-strong-parameters