Allow timestamp assignment in stubbed models

This commit is contained in:
Alex Tsui 2017-10-21 20:58:05 -07:00 committed by Daniel Colson
parent 0c17434b4a
commit dc327388fa
2 changed files with 22 additions and 0 deletions

View File

@ -72,6 +72,10 @@ module FactoryBot
def created_at
@created_at ||= Time.now.in_time_zone
end
def created_at=(date)
@created_at = date
end
end
end
@ -83,6 +87,10 @@ module FactoryBot
def updated_at
@updated_at ||= Time.current
end
def updated_at=(date)
@updated_at = date
end
end
end
end

View File

@ -179,6 +179,13 @@ describe "defaulting `created_at`" do
expect { build_stubbed(:thing_without_timestamp, created_at: Time.now) }.
to raise_error(NoMethodError, /created_at=/)
end
it "allows assignment of created_at" do
stub = build_stubbed(:thing_with_timestamp)
expect(stub.created_at).to eq Time.now
stub.created_at = 3.days.ago
expect(stub.created_at).to eq 3.days.ago
end
end
describe "defaulting `updated_at`" do
@ -215,6 +222,13 @@ describe "defaulting `updated_at`" do
build_stubbed(:thing_without_timestamp, updated_at: Time.now)
end.to raise_error(NoMethodError, /updated_at=/)
end
it "allows assignment of updated_at" do
stub = build_stubbed(:thing_with_timestamp)
expect(stub.updated_at).to eq Time.now
stub.updated_at = 3.days.ago
expect(stub.updated_at).to eq 3.days.ago
end
end
describe "defaulting `id`" do