Skip to content

Commit

Permalink
Support models without ID setters in build_stubbed
Browse files Browse the repository at this point in the history
  • Loading branch information
olivier-thatch committed Dec 8, 2023
1 parent bd0a10c commit cae4973
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/factory_bot/strategy/stub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,9 @@ def destroyed?
end

def has_settable_id?(result_instance)
!result_instance.class.respond_to?(:primary_key) ||
result_instance.class.primary_key
result_instance.respond_to?(:id=) &&
(!result_instance.class.respond_to?(:primary_key) ||
result_instance.class.primary_key)
end

def uuid_primary_key?(result_instance)
Expand Down
11 changes: 11 additions & 0 deletions spec/acceptance/stub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,17 @@
end
end

describe "a stubbed instance with no id setter" do
it "builds a stubbed instance" do
FactoryBot.define do
factory :model_hash, class: Hash
end

model = FactoryBot.build_stubbed(:model_hash)
expect(model).to be_truthy
end
end

def using_model(name, primary_key:)
define_class(name, ActiveRecord::Base)

Expand Down

0 comments on commit cae4973

Please sign in to comment.