工厂机器人错误“私有方法`新'调用User:Class(NoMethodError)”

时间:2018-01-04 09:58:11

标签: ruby-on-rails ruby factory-bot

我是工厂机器人的新手,我尝试使用工厂机器人创建示例数据,但我收到了此错误

如何解决此错误?

特征/支持/ factories.rb:

require 'factory_bot'


FactoryBot.define do
  factory :user do
    email "xxx123@xyz.co"
    password "asdf123"
    password_confirmation "asdf123"
  end
end
FactoryBot.define do
  factory :post do
    user = FactoryBot.create(:user)
  end
end

1 个答案:

答案 0 :(得分:0)

要求'factory_bot'

FactoryBot.define do
  factory :user do
    email "xxx123@xyz.co"
    password "asdf123"
    password_confirmation "asdf123"
  end
end

FactoryBot.define do
  factory :post do
    user
  end
end

,如FactoryBot文档

中所述
  

协会

     

可以在工厂内建立关联。如果工厂名称与关联名称相同,则可以省略工厂名称。

factory :post do
  # ...
  author
end
  

您还可以指定其他工厂或覆盖属性:

factory :post do
  # ...
  association :author, factory: :user, last_name: "Writely"
end