Skip to content

Commit c791452

Browse files
m-gizziyuenmichelle1stephanieminn
authored
Added a case for build_class to handle class names with underscores passed as a string (#1642)
Co-authored-by: Michelle Yuen <[email protected]> Co-authored-by: Stephanie Minn <[email protected]>
1 parent b5738ef commit c791452

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

lib/factory_bot/factory.rb

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ def initialize(name, options = {})
2222
def build_class
2323
@build_class ||= if class_name.is_a? Class
2424
class_name
25+
elsif class_name.to_s.safe_constantize
26+
class_name.to_s.safe_constantize
2527
else
2628
class_name.to_s.camelize.constantize
2729
end

spec/factory_bot/factory_spec.rb

+9
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ def self.to_s
215215

216216
expect(factory.name).to eq name
217217
end
218+
219+
it "sets build_class correctly with a class with an underscore" do
220+
name = :settings
221+
define_class("Admin_Settings_1")
222+
settings_class = Admin_Settings_1
223+
factory = FactoryBot::Factory.new(name, class: "Admin_Settings_1")
224+
225+
expect(factory.build_class).to eq settings_class
226+
end
218227
end
219228

220229
describe FactoryBot::Factory, "for namespaced class" do

0 commit comments

Comments
 (0)