我正在我正在维护的项目中写几个工厂。除qazxsw poi工厂外,所有这些都有效。
当我在rails控制台中运行工厂时,出现以下错误
project
似乎不知何故,factory_bot无法将symbol:project转换为Project类。
Ruby版本是2.3.6。 Factory_bot是4.11.1。 Factory_bot_rails也是4.11.1。
这是我的项目工厂文件:
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 `=='
编辑:这是项目表的数据库模式
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 | | |
而不是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
我使用相同的代码在我现有的项目中创建了一个工厂,并得到了完全相同的错误。然后我将工厂的名称更改为 FactoryBot.define do
factory :project, class: 'Project' do
...
并再次出现相同的错误。因此,它不是受保护的名称问题。
我最好的猜测:
1)您是否确保在添加项目后迁移测试数据库?
2)您确定您的应用程序正在连接到您列出的同一个数据库吗?
我觉得这可能是原因的原因是,如果我向我的工厂添加一个不属于它的相应表的属性,则错误是读取:
:projector
因此,工厂无法对您给出的符号进行限定,这很可能意味着它不会将其识别为常量/类。