如何使用attr_accessible创建工厂? [英] How to create factories with attr_accessible?

查看:73
本文介绍了如何使用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属性中.

I think the error is because user_id is not in attr_accessible attributes.

推荐答案

好,但是无论如何,如果您用关联定义工厂,即使使用attr_protected

ok, but anyway if you define your factory with the association, it should assign the record even with 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创建工厂?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆