Factory_bot: Namespaced models and deprecation warnings by class lookup

Created on 15 May 2018  路  5Comments  路  Source: thoughtbot/factory_bot

I have models which are namespaced such as this:

class Vehicle < ActiveRecord::Base; end

class Vehicle::Car < Vehicle; end
class Vehicle::Train < Vehicle; end
class Vehicle::Jet < Vehicle; end

When creating factories for these models, they were set up in the following way:

factory :vehicle_car, class: Vehicle::Car do; end
factory :vehicle_train, class: Vehicle::Train do; end
factory :vehicle_jet, class: Vehicle::Jet do; end

This produces the following deprecation warning:

DEPRECATION WARNING: Looking up factories by class is deprecated and will be removed in 5.0. Use symbols instead and set FactoryBot.allow_class_lookup = false.

Is there a format for writing a symbol to name these factories such that I do not need to use the class name to comply with the deprecation warning?

documentation

Most helpful comment

I added some documentation for using strings instead of classes in https://github.com/thoughtbot/factory_bot/commit/ef5c4ba49a182d051e39fe8ea3ef33591fff53ce

All 5 comments

Could anyone pay attention to this issue, please?
It is important for my current project. :smile:

@soxat after posting here, i noticed that this repo doesn't have very expedient replies so i tried stackoverflow to see if anyone could help there. here's that post:

https://stackoverflow.com/questions/50437961/factorybot-namespaced-models-without-class-name

in FactoryBot 4.10 its viable to add the class name as a string, so instead of Vehicle::Car (from my example above) you could simply do 'vehicle/car' which seems to work without deprecation warnings.

@michaelfich Thanks!

Thanks for the help. I am going to leave this issue open because there is definitely room for documentation improvements here.

I added some documentation for using strings instead of classes in https://github.com/thoughtbot/factory_bot/commit/ef5c4ba49a182d051e39fe8ea3ef33591fff53ce

Was this page helpful?
0 / 5 - 0 ratings