diff --git a/lib/factory_bot.rb b/lib/factory_bot.rb index 356b4e9..e5d5f65 100644 --- a/lib/factory_bot.rb +++ b/lib/factory_bot.rb @@ -66,6 +66,15 @@ module FactoryBot Linter.new(factories_to_lint, **options).lint! end + # Set the starting value for ids when using the build_stubbed strategy + # + # Arguments: + # * starting_id +Integer+ + # The new starting id value. + def self.build_stubbed_starting_id=(starting_id) + Strategy::Stub.next_id = starting_id - 1 + end + class << self delegate :callback_names, :callbacks, diff --git a/lib/factory_bot/strategy/stub.rb b/lib/factory_bot/strategy/stub.rb index bc5f309..312bd07 100644 --- a/lib/factory_bot/strategy/stub.rb +++ b/lib/factory_bot/strategy/stub.rb @@ -24,6 +24,10 @@ module FactoryBot :update_columns, ].freeze + def self.next_id=(id) + @@next_id = id + end + def association(runner) runner.run(:build_stubbed) end diff --git a/spec/acceptance/build_stubbed_spec.rb b/spec/acceptance/build_stubbed_spec.rb index 4e3bec4..bad3bae 100644 --- a/spec/acceptance/build_stubbed_spec.rb +++ b/spec/acceptance/build_stubbed_spec.rb @@ -279,3 +279,21 @@ describe "defaulting `id`" do expect(FactoryBot.build_stubbed(:post, id: 12).id).to eq 12 end end + +describe "configuring the starting id" do + it "defines which id build_stubbed instances start with" do + define_model("Post") + + FactoryBot.define do + factory :post + end + + FactoryBot.build_stubbed_starting_id = 1000 + + expect(FactoryBot.build_stubbed(:post).id).to eq 1000 + + FactoryBot.build_stubbed_starting_id = 3000 + + expect(FactoryBot.build_stubbed(:post).id).to eq 3000 + end +end