[英]How to test for mass assignment errors in Rspec and Rails 4?
我最近將Rails應用程序從Rails 3升級到了4,並且此Rspec測試不再通過:
# spec/models/user_spec.rb:
require 'spec_helper'
describe User do
it "should not allow access to admin" do
expect do
User.new(:admin => true)
end.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
end
end
我收到此錯誤:
Failure/Error: end.to raise_error(ActiveModel::MassAssignmentSecurity::Error)
NameError: uninitialized constant ActiveModel::MassAssignmentSecurity
我懷疑這是由於改用Rails 4的強參數造成的 。
現在如何測試批量分配錯誤?
謝謝你的幫助。
正如Baldrick正確指出的那樣,Rails 4不需要在Rspec模型測試中測試質量分配問題。 Rails 4的Strong Parameters的整體思想是將所有功能轉移到控制器上。
要使測試通過,您需要添加config.active_record.mass_assignment_sanitizer =:strict以
config / aplication.rb並將gem https://github.com/rails/protected_attributes添加到Gemfile。
如果要測試強參數,請閱讀本文http://pivotallabs.com/rails-4-testing-strong-parameters/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.