如何处理工厂和attr_accessible
?
我的例子:
# model
class SomeModel
attr_accessible :name, full_name, other_name
end
#spec
require 'spec_helper'
describe "test" do
it do
create(:some_model, name: "test name", user: User.first) #factory
end
end
#error
ruby(17122,0x12a055000) malloc: *** error for object 0x8: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
我认为错误是因为user_id
属于attr_accessible
属性。
答案 0 :(得分:2)
好的,但无论如何,如果您使用关联定义工厂,它应该使用attr_protected
分配记录
factory :some_model do |sm|
sm.name "John"
sm.full_name "John Smith"
sm.other_name "some other"
sm.association :user, :factory => :user
end
而不是
describe "test" do
it "should create models with associations" do
Factory(:some_model, name: "test name", user: User.first) #factory
end
end
答案 1 :(得分:0)
由于attr_accessible,这似乎与我得到的不同。 但是,您只需将:用户添加到attr_accessible
即可答案 2 :(得分:0)
你不是只需要SomeModel
吗?
belongs_to :user