Skip to content
New issue

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

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

Already on GitHub? # to your account

attributes overriding fails for some cases #1328

Closed
yfxie opened this issue Aug 13, 2019 · 2 comments
Closed

attributes overriding fails for some cases #1328

yfxie opened this issue Aug 13, 2019 · 2 comments

Comments

@yfxie
Copy link

yfxie commented Aug 13, 2019

Steps to reproduce

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  gem 'rails', '5.2.3'
  gem 'factory_bot_rails', '5.0.2'
  gem "sqlite3"
end

require "active_record"
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 :orders, force: true do |t|
    t.integer :abc
    t.string :abc_id
    t.timestamps
  end
end

class Order < ActiveRecord::Base
  enum abc: {
    'apple': 1,
    'banana': 2,
  }
end

FactoryBot.define do
  factory :order do
    abc { 'apple' }
  end
end

class BugTest < Minitest::Test
  # fail
  def test_correct_cases
    record = FactoryBot.build(:order, abc: 'banana', abc_id: '123456')
    assert_equal 'banana', record.abc
    assert_equal '123456', record.abc_id
  end

  # pass
  def test_correct_case1
    record = FactoryBot.build(:order, abc: 'banana')
    assert_equal 'banana', record.abc
  end

  # pass
  def test_correct_case2
    record = FactoryBot.build(:order, abc_id: '123456')
    assert_equal '123456', record.abc_id
  end
end

Expected behavior

all of the test cases should pass

Actual behavior

test_correct_cases is failure

System configuration

factory_bot version: 5.0.2
rails version: 5.2.3
ruby version: 2.6.3

@composerinteralia
Copy link
Collaborator

At a quick glance, this looks like it is related to factory_bot's assumption that abc_id is the foreign key for an abc association. See #1142

@composerinteralia
Copy link
Collaborator

Yes, this is the same as #1142. I am going to close this issue and continue tracking it over there. Thanks for the reproduction script. That was super helpful.

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

No branches or pull requests

2 participants