1
0
Fork 0
mirror of https://github.com/thoughtbot/factory_bot.git synced 2022-11-09 11:43:51 -05:00
thoughtbot--factory_bot/spec/acceptance/stub_spec.rb
Daniel Colson 5a3247dd8c Avoid stubbing id for records without primary key
Fixes #1305

Before Rails 6 when the build_stubbed strategy assigned an id on a
record without a primary key column, the `id=` method would no-op, and
the record would end up looking like a new_record (i.e. `new_record?`
would return true because the id was nil).

This problem surfaced because of a [change in Rails 6][rails], which
caused `id=` to raise a potentially confusing
`ActiveModel::MissingAttributeError: can't write unknown attribute ''`
for records without a primary key column.

Since build_stubbed stubbed was calling `id=` for all instances,
regardless of whether they had primary keys, it was raising the above
error.

To avoid this error, we check whether the instance has a primary_key
defined before setting the id.

We also changed `persisted?` and `new_record?` to be less dependent on
the id. That way those methods will work as expected for records without
primary keys.

[rails]: b6828fc915

Co-authored-by: Jesse Bailey <jbailey117@gmail.com>
2019-07-19 15:18:11 -04:00

101 lines
2.1 KiB
Ruby

describe "a stubbed instance" do
include FactoryBot::Syntax::Methods
before do
define_model("User")
define_model("Post", user_id: :integer) do
belongs_to :user
end
FactoryBot.define do
factory :user
factory :post do
user
end
end
end
subject { build_stubbed(:post) }
it "acts as if it came from the database" do
should_not be_new_record
end
it "assigns associations and acts as if it is saved" do
expect(subject.user).to be_kind_of(User)
expect(subject.user).not_to be_new_record
end
end
describe "a stubbed instance overriding strategy" do
include FactoryBot::Syntax::Methods
before do
define_model("User")
define_model("Post", user_id: :integer) do
belongs_to :user
end
FactoryBot.define do
factory :user
factory :post do
association(:user, strategy: :build)
end
end
end
subject { build_stubbed(:post) }
it "acts as if it is saved in the database" do
should_not be_new_record
end
it "assigns associations and acts as if it is saved" do
expect(subject.user).to be_kind_of(User)
expect(subject.user).not_to be_new_record
end
end
describe "a stubbed instance with no primary key" do
it "builds a stubbed instance" do
using_model_without_pk do
FactoryBot.define do
factory :model_without_pk
end
model = FactoryBot.build_stubbed(:model_without_pk)
expect(model).to be_truthy
end
end
it "behaves like a persisted record" do
using_model_without_pk do
FactoryBot.define do
factory :model_without_pk
end
model = FactoryBot.build_stubbed(:model_without_pk)
expect(model).to be_persisted
expect(model).not_to be_new_record
end
end
def using_model_without_pk
define_class("ModelWithoutPk", ActiveRecord::Base)
connection = ActiveRecord::Base.connection
begin
clear_generated_table("model_without_pks")
connection.create_table("model_without_pks", id: false) do |t|
t.column :updated_at, :datetime
end
yield
ensure
clear_generated_table("model_without_pks")
end
end
end