Skip to content

Commit

Permalink
Added a case for build_class to handle class names with underscores p…
Browse files Browse the repository at this point in the history
…assed as a string (#1642)

Co-authored-by: Michelle Yuen <[email protected]>
Co-authored-by: Stephanie Minn <[email protected]>
  • Loading branch information
3 people authored May 20, 2024
1 parent b5738ef commit c791452
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/factory_bot/factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ def initialize(name, options = {})
def build_class
@build_class ||= if class_name.is_a? Class
class_name
elsif class_name.to_s.safe_constantize
class_name.to_s.safe_constantize
else
class_name.to_s.camelize.constantize
end
Expand Down
9 changes: 9 additions & 0 deletions spec/factory_bot/factory_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@ def self.to_s

expect(factory.name).to eq name
end

it "sets build_class correctly with a class with an underscore" do
name = :settings
define_class("Admin_Settings_1")
settings_class = Admin_Settings_1
factory = FactoryBot::Factory.new(name, class: "Admin_Settings_1")

expect(factory.build_class).to eq settings_class
end
end

describe FactoryBot::Factory, "for namespaced class" do
Expand Down

0 comments on commit c791452

Please sign in to comment.