该文档对于如何:class
选项的行为或它所期望的值,但来源更有帮助。从我们发现的错误信息回溯FactoryBot::Decorator::ClassKeyHash#symbolize_keys https://github.com/thoughtbot/factory_bot/blob/c716ce01b448ce4e0bf855c5a2c63ecb9206322e/lib/factory_bot/decorator/class_key_hash.rb#L18:
def symbolized_key(key)
if key.respond_to?(:to_sym)
key.to_sym
elsif FactoryBot.allow_class_lookup
ActiveSupport::Deprecation.warn "Looking up factories by class is deprecated and will be removed in 5.0. Use symbols instead and set FactoryBot.allow_class_lookup = false", caller
key.to_s.underscore.to_sym
end
end
The key.to_sym
第一个分支中的惯用语是“我想要一个Symbol
or String
". The key.to_s.underscore.to_sym
第二个分支告诉我们期望的格式。
如果你跑Vehicle::Car
通过to_s.underscore
, 你得到'vehicle/car'
所以这些应该有效:
factory :vehicle_car, class: 'vehicle/car' do; end
factory :vehicle_train, class: 'vehicle/train' do; end
factory :vehicle_jet, class: 'vehicle/jet' do; end
或者如果你真的想要Symbol
s(或者有标点符号):
factory :vehicle_car, class: :'vehicle/car' do; end
factory :vehicle_train, class: :'vehicle/train' do; end
factory :vehicle_jet, class: :'vehicle/jet' do; end