我有一个用户模型:
class User < ActiveRecord::Base
attr_accessible :email, :name
# admin only
attr_accessible :email, :name, :admin, :as => :admin
以及以下型号规格:
describe "accessible attributes" do
let(:new_user){ FactoryGirl.create(:user) }
@admin_attrs = { admin: true, name: "ben", email: "xyz@test.com"}
it "can not be set on create" do
# variant 1 - test fails, AM::MAS::Error NOT thrown
expect do
User.new(@admin_attrs)
end.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
# variant 2 - test passes, AM::MAS::Error NOT thrown
expect do
User.new(admin: true, name: "ben", email: "xyz@test.com", password: "123xyz", password_confirmation: "123xyz")
end.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
end
我无法弄清楚为什么我的测试变体 1 失败,但变体 2 通过了。我得到的错误信息是:
1) User accessible attributes can not be set on create
Failure/Error: expect do
expected ActiveModel::MassAssignmentSecurity::Error but nothing was raised
它们基本上是相同的测试。我究竟做错了什么?事实上,如果我从控制台执行测试 1,它确实会抛出 MAS::Error,正如预期的那样。我很困惑。