更新方法的集成测试(带测试/单元):
test "do the patch" do
user = users(:alex)
get signin_url
assert_response :success
post_via_redirect signin_path, email: user.email, password: 'qwerty'
assert_equal profile_path, path
get edit_user_url(user)
patch_via_redirect user_url(user),
email: 'patch@it.man',
name: 'Patch!',
password: 'qwerty',
password_confirmation: 'qwerty'
assert_equal 'User updated!', flash[:notice]
end
当我运行测试时出现此错误:
1) Error:
UserFlowsTest#test_do_the_patch:
ActionController::ParameterMissing: param not found: user
app/controllers/users_controller.rb:43:in `user_params'
app/controllers/users_controller.rb:31:in `update'
test/integration/user_flows_test.rb:124:in `block in <class:UserFlowsTest>'
users_controller.rb 中的函数:
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = t('activecontroller.actions.user.updated')
sign_in @user
redirect_to @user
else
render :edit
end
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
当我在控制器中使用强参数时,如何测试patch
?
答案 0 :(得分:2)
params.require(:user)
要求在参数哈希的根处有一个:user =>
参数。
试试这个:
patch_via_redirect user_url(user), { user: {
email: 'patch@it.man',
name: 'Patch!',
password: 'qwerty',
password_confirmation: 'qwerty'
} }
糟糕的格式化,但应该明白这一点。