[英]How to create factories with attr_accessible?
如何處理工廠和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
屬性中。
好的,但無論如何,如果您使用關聯定義工廠,它應該使用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
這看起來與我因attr_accessible得到的不同。 但是,您可以簡單地將:user添加到attr_accessible
難道你不只是在SomeModel
需要它嗎?
belongs_to :user
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.