我正在维护的一个项目中写了几家工厂。除了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转换为其Project类。
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 "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 | | |
答案 0 :(得分:1)
尝试在工厂说明中声明类名称。
FactoryBot.define do
factory :project, class: 'Project' do
...
我使用您的相同代码在我现有的项目中创建了一个工厂,并得到了完全相同的错误。然后,我将工厂名称更改为:projector
,并再次遇到相同的错误。因此,这不是受保护的名称问题。
我的最佳猜测:
1)自添加项目以来,您确定要迁移测试数据库吗?
2)您确定您的应用程序正在连接到列出的同一数据库吗?
我认为这可能是原因的原因是,如果我向工厂的对应表中未添加属性,则错误为:
NoMethodError: undefined method `name=' for #<Report:0x00007fc5bb065c10>
因此,工厂无法将获得的符号常量化,这很可能意味着它无法将其识别为常量/类。
答案 1 :(得分:1)
您似乎是指association :client, factory: user
而不是association :client, factory: :user
(:user
应该是符号)。
这是一条毫无用处的错误消息。我们在https://github.com/thoughtbot/factory_bot/pull/1219中改进了错误消息,还有一个开放的问题可以进一步改善:https://github.com/thoughtbot/factory_bot/issues/1227。