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

Fix build_stubbed timestamps being different #1653

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/factory_bot/strategy/stub.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,14 @@ def clear_changes_information(result_instance)
end

def set_timestamps(result_instance)
timestamp = Time.current

if missing_created_at?(result_instance)
result_instance.created_at = Time.current
result_instance.created_at = timestamp
end

if missing_updated_at?(result_instance)
result_instance.updated_at = Time.current
result_instance.updated_at = timestamp
end
end

Expand Down
18 changes: 18 additions & 0 deletions spec/acceptance/stub_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,24 @@
end
end

describe "a stubbed instance with timestamps" do
include FactoryBot::Syntax::Methods

before do
define_model("ModelWithTimestamps", created_at: :datetime, updated_at: :datetime)

FactoryBot.define do
factory :model_with_timestamps
end
end

subject { build_stubbed(:model_with_timestamps) }

it "assigns the exact same datetime" do
expect(subject.created_at).to eq(subject.updated_at)
end
end

describe "a stubbed instance overriding strategy" do
include FactoryBot::Syntax::Methods

Expand Down