我正在阅读 Michael Hartl 的 Rails 教程。我已经来到第 9 章,练习 1。它要求您添加一个测试以验证该类的admin
属性User
是否不可访问。这是注释掉无关部分的 User 类:
class User < ActiveRecord::Base
attr_accessible :name, :email, :password, :password_confirmation
attr_protected :admin
# before_save methods
# validations
# private methods
end
这是我用来验证该admin
属性不可访问的测试。
describe User do
before do
@user = User.new(
name: "Example User",
email: "user@example.com",
password: "foobar123",
password_confirmation: "foobar123")
end
subject { @user }
describe "accessible attributes" do
it "should not allow access to admin" do
expect do
@user.admin = true
end.should raise_error(ActiveModel::MassAssignmentSecurity::Error)
end
end
end
测试失败。它表示没有引发错误,尽管该admin
属性受到保护。我怎样才能让测试通过?