本文介绍了测试康康舞能力并获取MassAssignmentSecurity :: Error的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经实现了康康,并且想按照康康Wiki上的建议测试功能。我试图复制用户只能销毁他拥有的项目。
I have implemented cancan and would like to test abilities as recommended on the cancan wiki. I trying to replicate "user can only destroy projects which he owns."
spec / models / ability_spec.rb:
spec/models/ability_spec.rb:
require "cancan/matchers"
require 'spec_helper'
describe Ability do
context "user is investigator" do
it "user can only destroy projects which he owns" do
user = FactoryGirl.create(:user)
ability = Ability.new(user)
ability.should be_able_to(:destroy, Project.new(:user => user))
end
end
end
但是我得到了:
ActiveModel::MassAssignmentSecurity::Error:
Can't mass-assign protected attributes: user
型号:
class User < ActiveRecord::Base
has_many :projects, dependent: :destroy
devise :database_authenticatable, etc...
attr_accessible :email, :password, :password_confirmation, :remember_me, :locale
validates :role, :presence => true
end
class Project < ActiveRecord::Base
belongs_to :user
end
工厂:
FactoryGirl.define do
factory :user do |f|
f.email { Faker::Internet.email }
f.password "secret"
f.role 1
end
end
我理解为什么会出现此错误,并尝试了各种解决方法,但是对工厂的理解不足它。您能帮忙吗?
I understand why this error arrises, and have tried various ways round it, but don't have a good enough understanding of factories to crack it. Can you help?
推荐答案
因此,问题与创建项目时不使用Factory Girl有关。应该是这样:
So the problem was related to not using Factory Girl when creating the project. It should have been:
describe Ability do
context "user is investigator" do
it "user can only destroy projects which he owns" do
user = FactoryGirl.create(:user)
ability = Ability.new(user)
ability.should be_able_to(:destroy, FactoryGirl.create(:project, :user => user))
end
end
end
这篇关于测试康康舞能力并获取MassAssignmentSecurity :: Error的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!