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

Do not overwrite the dafault value #1680

Open
anko20094 opened this issue Jul 20, 2024 · 2 comments · May be fixed by #1709
Open

Do not overwrite the dafault value #1680

anko20094 opened this issue Jul 20, 2024 · 2 comments · May be fixed by #1709
Labels

Comments

@anko20094
Copy link

Description

# db/structure.sql
CREATE TABLE public.realtors (
.......
duplicate_id character varying,
duplicate boolean DEFAULT false NOT NULL,
.......
# spec/factories/realtors.rb

FactoryBot.define do
  factory :realtor do
.......
duplicate_id { pry; Faker::Alphanumeric.alpha(number: 10) }
.......
# in some spec
realtor1 = create(:realtor, id: 6, duplicate: true, duplicate_id: 'dup1')
[1] pry> realtor1.duplicate_id
=> nil

So, it does not set any value from factory neither from wrote manually

Reproduction Steps

Create the realtors table with the duplicate_id column.
Define a factory for realtor with a duplicate_id attribute.
Create a realtor object in a test, specifying a value for duplicate_id.
Check the value of duplicate_id on the created object; it is nil instead of the expected value.

Expected behavior

The duplicate_id should be set to the value specified manually in the test when creating a realtor object.

Actual behavior

The duplicate_id is set to nil, ignoring both the factory-generated value and the manually specified value.

System configuration

factory_bot_rails (6.2.0):
rails (7.0.8):
ruby (3.2.1):

@anko20094 anko20094 added the bug label Jul 20, 2024
@DoodlingDev
Copy link

a quick script we threw together to test this out...

require "bundler/inline"

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

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

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")

ActiveRecord::Schema.define do
  create_table :realtors, force: true do |t|
    t.boolean :duplicate, default: false, null: false
    t.string :duplicate_id
  end
end

class Realtor < ActiveRecord::Base
end

FactoryBot.define do
  factory :realtor do
    duplicate { false }
    duplicate_id { "This should exist" }
  end
end

class RealtorTest < Minitest::Test
  def test_factory_assigned_value_should_be_present
    realtor = FactoryBot.create(:realtor, duplicate: true)
    assert_equal "This should exist", realtor.duplicate_id
  end

  def test_create_assigned_value_should_be_present
    realtor = FactoryBot.create(:realtor, duplicate: true, duplicate_id: "Something Else")
    assert_equal "Something Else", realtor.duplicate_id
  end
end

@unused
Copy link

unused commented Oct 25, 2024

The issue with with aliases is popping up every now and then, recent with #1142 - where one suggestion is to use transient to work around this edge case.

# following factory passes above example
FactoryBot.define do
  factory :realtor do
    # duplicate { false }
    # duplicate_id { "This should exist" }

    transient do
      duplicate { false }
      duplicate_id { "This should exist" }
    end

    initialize_with do
      new(attributes.merge(duplicate:, duplicate_id:))
    end
  end
end

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

Successfully merging a pull request may close this issue.

3 participants