Factory_bot 无法创建 "project" 工厂
Factory_bot can't create "project" factory
我正在维护的项目中编写多个工厂。除了 project
工厂外,它们都工作。
当我在 rails 控制台中 运行 工厂时,出现以下错误
FactoryBot.create(:project)
NoMethodError: undefined method `name' for :project:Symbol from /home/user/.rvm/gems/ruby-2.3.6/gems/factory_bot-4.11.1/lib/factory_bot/declaration/implicit.rb:11:in `=='
似乎不知何故,factory_bot 无法将符号 :project 转换为它的项目 class。
Ruby版本为2.3.6。 Factory_bot 是 4.11.1。 Factory_bot_rails 也是 4.11.1。
这是我的项目工厂文件:
require 'faker'
FactoryBot.define do
factory :project do
name { Faker::Company.name }
product_type { Faker::Number.between(0, 1) }
sale_type { Faker::Number.between(0, 1) }
description { Faker::Lorem.sentence }
street { Faker::Address.street_name }
neighborhood { Faker::Address.community }
zip_code { Faker::Address.zip }
country { Faker::Address.country_code }
state { Faker::Address.state_abbr }
city { Faker::Address.city }
town { Faker::Address.city }
logo { Faker::Company.logo }
cover { Faker::Company.logo }
send_from_email { Faker::Internet.email }
parking_lot_cost { Faker::Number.decimal(2) }
warehouse_m2_cost { Faker::Number.decimal(2) }
association :client, factory: user
inventory_dimension_value { Faker::Number.digit }
total_m2 { Faker::Number.decimal(2) }
sales_volume_total { Faker::Number.decimal(2) }
total_units { Faker::Number.digit }
current_m2_sold { Faker::Number.decimal(2) }
sales_volume_current_sold { Faker::Number.decimal(2) }
current_units_sold { Faker::Number.digit }
sales_volume_to_sell { Faker::Number.decimal(2) }
next_list_acum { Faker::Number.decimal(2) }
from_email { Faker::Internet.email }
saved_next_list_acum { Faker::Number.decimal(2) }
settings_show_distributions_cols { Faker::Boolean.boolean }
settings_price_list_v2_enabled { Faker::Boolean.boolean }
api_token { Faker::Lorem.word }
end
end
编辑:这是项目的数据库模式table
Table "public.projects"
Column | Type | Collation | Nullable | Default
----------------------------------+-----------------------------+-----------+----------+--------------------------------------
id | integer | | not null | nextval('projects_id_seq'::regclass)
name | character varying | | |
product_type | integer | | |
sale_type | integer | | |
description | text | | |
street | character varying | | |
neighborhood | character varying | | |
zip_code | character varying | | |
country | character varying | | |
state | character varying | | |
city | character varying | | |
town | character varying | | |
logo | character varying | | |
cover | character varying | | |
send_from_email | text | | |
parking_lot_cost | double precision | | | 0.0
warehouse_m2_cost | double precision | | | 0.0
created_at | timestamp without time zone | | not null |
updated_at | timestamp without time zone | | not null |
client_id | integer | | |
inventory_dimension_value | integer | | |
total_m2 | double precision | | | 0.0
sales_volume_total | double precision | | | 0.0
total_units | integer | | | 0
current_m2_sold | double precision | | | 0.0
sales_volume_current_sold | double precision | | | 0.0
current_units_sold | integer | | | 0
sales_volume_to_sell | double precision | | | 0.0
next_list_acum | double precision | | | 0.0
from_email | character varying | | |
saved_next_list_acum | double precision | | |
settings_show_distributions_cols | boolean | | | true
settings_price_list_v2_enabled | boolean | | | false
api_token | character varying | | |
尝试在工厂描述中声明 class 名称。
FactoryBot.define do
factory :project, class: 'Project' do
...
我使用您的相同代码在我的现有项目中创建了一个工厂,但得到了完全相同的错误。然后我将工厂名称更改为 :projector
并再次出现相同的错误。因此这不是受保护的名称问题。
我的最佳猜测:
1) 添加项目后是否确定迁移了测试数据库?
2) 您确定您的应用正在连接到您列出的同一个数据库吗?
我觉得这可能是因为如果我向我的工厂添加一个属性,但它没有对应的 table,错误是读取:
NoMethodError: undefined method `name=' for #<Report:0x00007fc5bb065c10>
因此,工厂无法常量化您给它的符号,这很可能意味着它没有将其识别为 constant/class。
看起来你指的是 association :client, factory: user
而不是 association :client, factory: :user
(:user
应该是一个符号)。
这是一条毫无用处的错误消息。我们在 https://github.com/thoughtbot/factory_bot/pull/1219, and we have an open issue to improve it more: https://github.com/thoughtbot/factory_bot/issues/1227.
中稍微改进了错误消息
我正在维护的项目中编写多个工厂。除了 project
工厂外,它们都工作。
当我在 rails 控制台中 运行 工厂时,出现以下错误
FactoryBot.create(:project)
NoMethodError: undefined method `name' for :project:Symbol from /home/user/.rvm/gems/ruby-2.3.6/gems/factory_bot-4.11.1/lib/factory_bot/declaration/implicit.rb:11:in `=='
似乎不知何故,factory_bot 无法将符号 :project 转换为它的项目 class。
Ruby版本为2.3.6。 Factory_bot 是 4.11.1。 Factory_bot_rails 也是 4.11.1。
这是我的项目工厂文件:
require 'faker'
FactoryBot.define do
factory :project do
name { Faker::Company.name }
product_type { Faker::Number.between(0, 1) }
sale_type { Faker::Number.between(0, 1) }
description { Faker::Lorem.sentence }
street { Faker::Address.street_name }
neighborhood { Faker::Address.community }
zip_code { Faker::Address.zip }
country { Faker::Address.country_code }
state { Faker::Address.state_abbr }
city { Faker::Address.city }
town { Faker::Address.city }
logo { Faker::Company.logo }
cover { Faker::Company.logo }
send_from_email { Faker::Internet.email }
parking_lot_cost { Faker::Number.decimal(2) }
warehouse_m2_cost { Faker::Number.decimal(2) }
association :client, factory: user
inventory_dimension_value { Faker::Number.digit }
total_m2 { Faker::Number.decimal(2) }
sales_volume_total { Faker::Number.decimal(2) }
total_units { Faker::Number.digit }
current_m2_sold { Faker::Number.decimal(2) }
sales_volume_current_sold { Faker::Number.decimal(2) }
current_units_sold { Faker::Number.digit }
sales_volume_to_sell { Faker::Number.decimal(2) }
next_list_acum { Faker::Number.decimal(2) }
from_email { Faker::Internet.email }
saved_next_list_acum { Faker::Number.decimal(2) }
settings_show_distributions_cols { Faker::Boolean.boolean }
settings_price_list_v2_enabled { Faker::Boolean.boolean }
api_token { Faker::Lorem.word }
end
end
编辑:这是项目的数据库模式table
Table "public.projects"
Column | Type | Collation | Nullable | Default
----------------------------------+-----------------------------+-----------+----------+--------------------------------------
id | integer | | not null | nextval('projects_id_seq'::regclass)
name | character varying | | |
product_type | integer | | |
sale_type | integer | | |
description | text | | |
street | character varying | | |
neighborhood | character varying | | |
zip_code | character varying | | |
country | character varying | | |
state | character varying | | |
city | character varying | | |
town | character varying | | |
logo | character varying | | |
cover | character varying | | |
send_from_email | text | | |
parking_lot_cost | double precision | | | 0.0
warehouse_m2_cost | double precision | | | 0.0
created_at | timestamp without time zone | | not null |
updated_at | timestamp without time zone | | not null |
client_id | integer | | |
inventory_dimension_value | integer | | |
total_m2 | double precision | | | 0.0
sales_volume_total | double precision | | | 0.0
total_units | integer | | | 0
current_m2_sold | double precision | | | 0.0
sales_volume_current_sold | double precision | | | 0.0
current_units_sold | integer | | | 0
sales_volume_to_sell | double precision | | | 0.0
next_list_acum | double precision | | | 0.0
from_email | character varying | | |
saved_next_list_acum | double precision | | |
settings_show_distributions_cols | boolean | | | true
settings_price_list_v2_enabled | boolean | | | false
api_token | character varying | | |
尝试在工厂描述中声明 class 名称。
FactoryBot.define do
factory :project, class: 'Project' do
...
我使用您的相同代码在我的现有项目中创建了一个工厂,但得到了完全相同的错误。然后我将工厂名称更改为 :projector
并再次出现相同的错误。因此这不是受保护的名称问题。
我的最佳猜测:
1) 添加项目后是否确定迁移了测试数据库?
2) 您确定您的应用正在连接到您列出的同一个数据库吗?
我觉得这可能是因为如果我向我的工厂添加一个属性,但它没有对应的 table,错误是读取:
NoMethodError: undefined method `name=' for #<Report:0x00007fc5bb065c10>
因此,工厂无法常量化您给它的符号,这很可能意味着它没有将其识别为 constant/class。
看起来你指的是 association :client, factory: user
而不是 association :client, factory: :user
(:user
应该是一个符号)。
这是一条毫无用处的错误消息。我们在 https://github.com/thoughtbot/factory_bot/pull/1219, and we have an open issue to improve it more: https://github.com/thoughtbot/factory_bot/issues/1227.
中稍微改进了错误消息