我正在使用 rspec 创建一个带有 TDD 的 Rails 应用程序。我收到一个无法删除的错误:
Failure/Error: invalid_user = User.new(@attr.merge("provider" => ""))
ActiveModel::MassAssignmentSecurity::Error:
Can't mass-assign protected attributes: uid, info
这是我的用户规格:
user_spec.rb
require 'spec_helper'
describe User do
before(:each) do
@attr = {"provider" => "providerexample", "uid" => "uidexample", "info" => {"name" => "Example"}}
end
it "should create a new instance given valid attributes" do
user = User.create_with_omniauth(@attr)
end
it "should require a provider" do
invalid_user = User.new(@attr.merge("provider" => ""))
invalid_user.should_not be_valid
end
it "should require a uid" do
invalid_user = User.new(@attr.merge("uid" => ""))
invalid_user.should_not be_valid
end
end
和我的user.rb
class User < ActiveRecord::Base
attr_accessible :name, :credits, :email, :provider
validates :name, :provider, :uid, :presence => true
def self.create_with_omniauth(auth)
create! do |user|
user.provider = auth["provider"]
user.uid = auth["uid"]
user.name = auth["info"]["name"]
end
end
结尾
mass-assign
如果我通过添加uid
和info
来调试错误attr_accessible
,我仍然会收到以下错误unknown attribute: info
。