Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial value for sequences having no effect #1724

Open
tgentry-bamboo opened this issue Jan 24, 2025 · 1 comment
Open

Initial value for sequences having no effect #1724

tgentry-bamboo opened this issue Jan 24, 2025 · 1 comment
Labels

Comments

@tgentry-bamboo
Copy link

Description

Hello! I was reading the documentation on restricting the sequence to an initial starting value. Following the documentation as written has no effect.

Reproduction Steps

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"
  git_source(:github) { |repo| "https://github.com/#{repo}.git" }
  gem "factory_bot", "6.4.6"
  gem "activerecord"
  gem "sqlite3"
end

require "active_record"
require "factory_bot"
require "minitest/autorun"
require "logger"

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :posts, force: true do |t|
    t.integer :number_of_characters
  end
end

class Post < ActiveRecord::Base
end

FactoryBot.define do
  factory :post do
    sequence(:body, 1)
  end
end

class FactoryBotTest < Minitest::Test
  def test_factory_bot_stuff
    post = FactoryBot.create(:post)

    assert_equal post.number_of_characters, 1
  end
end

Expected behavior

The sequence starts with the integer 1.

Actual behavior

The sequence starts with the integer 4.

System configuration

6.4.6:
6.1.7.7:
3.1.5:

@neilvcarvalho
Copy link
Member

Hi @tgentry-bamboo,

Thank you for the detailed issue. I tried running your reproduction steps, and even changed it to match your Rails version. I couldn't reproduce the bug you're describing.

Here's the modified version (with version pinning, added logger gem, as it was removed from a Rails dependency, an attribute name fix, and changed expectation to avoid using the default).

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"
  git_source(:github) { |repo| "https://github.com/#{repo}.git" }
  gem "factory_bot", "6.4.6"
  gem "logger"
  gem "activerecord", "6.1.7.7"
  gem "sqlite3", "~> 1.4"
end

require "active_record"
require "factory_bot"
require "minitest/autorun"
require "logger"

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :posts, force: true do |t|
    t.integer :number_of_characters
  end
end

class Post < ActiveRecord::Base
end

FactoryBot.define do
  factory :post do
    sequence(:number_of_characters, 4)
  end
end

class FactoryBotTest < Minitest::Test
  def test_factory_bot_stuff
    post = FactoryBot.create(:post)

    assert_equal post.number_of_characters, 4
  end
end

Could you paste the logs from this test run?

If this happened in a real application and isn't happening on your test run, I suspect other records had been created (or built) before, either in other tests, or as associated records.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants