我正在使用 strong_parameters
gem 在我的 Controller 中,但我很难理解我将如何测试它。
这是我的设置示例
class UserController < ActionController::Base
include ActiveModel::ForbiddenAttributesProtection
def create
@user = User.new(user_params)
if @user.save
...
end
end
private
def user_params
params.require(:user).permit(:first_name, :last_name, :username, :email)
end
end
我想测试
user_params
确保正确过滤掉恶意键/值对的方法,但无法弄清楚如何去做。有没有其他人经历过这个?
最佳答案
您可以将 params 散列 stub 为
params = ActionController::Parameters.new(your_hash)
这是你的 URL 参数在你的 Controller 中被转换成的类,它为你提供了 require 和 permit 方法。
我个人将功能提取到一个新类中来处理授权策略。
关于ruby-on-rails - RSpec - 测试强参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16574509/