0

如何与工厂打交道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属性中。

4

3 回答 3

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
于 2012-06-25T17:41:03.253 回答
0

由于 attr_accessible,这似乎与我得到的不同。但是,您可以简单地将:user添加到 attr_accessible

于 2012-06-25T12:10:18.477 回答
0

你不需要那个SomeModel吗?

belongs_to :user
于 2012-06-25T14:20:05.050 回答