我有一个用户模型:
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。我很困惑。
答案 0 :(得分:4)
@admin_attrs可能是零,因为它没有在before hook中设置(或通过'let')