繁体   English   中英

如何使用attr_accessible创建工厂?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM