mirror of
https://github.com/thoughtbot/factory_bot.git
synced 2022-11-09 11:43:51 -05:00
Avoid time travel in tests
By being a little bit less strict in our assertions, we can still prove the behaviour is correct, without having to freeze / travel time.
This commit is contained in:
parent
eabb1a35ee
commit
eb6bccb3e3
4 changed files with 21 additions and 26 deletions
|
@ -148,12 +148,10 @@ describe "defaulting `created_at`" do
|
|||
factory :thing_with_timestamp
|
||||
factory :thing_without_timestamp
|
||||
end
|
||||
|
||||
travel_to(Time.now)
|
||||
end
|
||||
|
||||
it "defaults created_at for objects with created_at" do
|
||||
expect(build_stubbed(:thing_with_timestamp).created_at).to eq Time.now
|
||||
expect(build_stubbed(:thing_with_timestamp).created_at).to be_about_now
|
||||
end
|
||||
|
||||
it "is doesn't mark the object as changed" do
|
||||
|
@ -179,9 +177,10 @@ describe "defaulting `created_at`" do
|
|||
|
||||
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
|
||||
expect(stub.created_at).to be_about_now
|
||||
past_time = 3.days.ago
|
||||
stub.created_at = past_time
|
||||
expect(stub.created_at).to eq past_time
|
||||
end
|
||||
|
||||
it "behaves the same as a non-stubbed created_at" do
|
||||
|
@ -213,12 +212,10 @@ describe "defaulting `updated_at`" do
|
|||
factory :thing_with_timestamp
|
||||
factory :thing_without_timestamp
|
||||
end
|
||||
|
||||
travel_to(Time.now)
|
||||
end
|
||||
|
||||
it "defaults updated_at for objects with updated_at" do
|
||||
expect(build_stubbed(:thing_with_timestamp).updated_at).to eq Time.current
|
||||
expect(build_stubbed(:thing_with_timestamp).updated_at).to be_about_now
|
||||
end
|
||||
|
||||
it "is doesn't mark the object as changed" do
|
||||
|
@ -232,8 +229,9 @@ describe "defaulting `updated_at`" do
|
|||
end
|
||||
|
||||
it "allows overriding updated_at for objects with updated_at" do
|
||||
stubbed = build_stubbed(:thing_with_timestamp, updated_at: 3.days.ago)
|
||||
expect(stubbed.updated_at).to eq 3.days.ago
|
||||
past_time = 3.days.ago
|
||||
stubbed = build_stubbed(:thing_with_timestamp, updated_at: past_time)
|
||||
expect(stubbed.updated_at).to eq past_time
|
||||
end
|
||||
|
||||
it "doesn't allow setting updated_at on an object that doesn't define it" do
|
||||
|
@ -244,9 +242,10 @@ describe "defaulting `updated_at`" do
|
|||
|
||||
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
|
||||
expect(stub.updated_at).to be_about_now
|
||||
past_time = 3.days.ago
|
||||
stub.updated_at = past_time
|
||||
expect(stub.updated_at).to eq past_time
|
||||
end
|
||||
|
||||
it "behaves the same as a non-stubbed updated_at" do
|
||||
|
|
|
@ -23,7 +23,6 @@ describe FactoryBot::Strategy::Stub do
|
|||
it_should_behave_like "strategy with strategy: :build", :build_stubbed
|
||||
|
||||
context "asking for a result" do
|
||||
before { travel_to(Time.now) }
|
||||
let(:result_instance) do
|
||||
define_class("ResultInstance") do
|
||||
attr_accessor :id, :created_at
|
||||
|
@ -39,12 +38,10 @@ describe FactoryBot::Strategy::Stub do
|
|||
it { expect(subject.result(evaluation)).not_to be_destroyed }
|
||||
|
||||
it "assigns created_at" do
|
||||
created_at = subject.result(evaluation).created_at
|
||||
expect(created_at).to eq Time.now
|
||||
created_at1 = subject.result(evaluation).created_at
|
||||
created_at2 = subject.result(evaluation).created_at
|
||||
|
||||
travel_to(2.days.from_now)
|
||||
|
||||
expect(subject.result(evaluation).created_at).to eq created_at
|
||||
expect(created_at1).to equal created_at2
|
||||
end
|
||||
|
||||
include_examples "disabled persistence method", :connection
|
||||
|
|
|
@ -4,7 +4,6 @@ require "rspec/its"
|
|||
require "simplecov"
|
||||
|
||||
require "factory_bot"
|
||||
require "active_support/testing/time_helpers"
|
||||
|
||||
Dir["spec/support/**/*.rb"].each { |f| require File.expand_path(f) }
|
||||
|
||||
|
@ -17,16 +16,11 @@ RSpec.configure do |config|
|
|||
end
|
||||
|
||||
config.include DeclarationMatchers
|
||||
config.include ActiveSupport::Testing::TimeHelpers
|
||||
|
||||
config.before do
|
||||
FactoryBot.reload
|
||||
end
|
||||
|
||||
config.after do
|
||||
travel_back
|
||||
end
|
||||
|
||||
config.around do |example|
|
||||
begin
|
||||
previous_use_parent_strategy = FactoryBot.use_parent_strategy
|
||||
|
|
5
spec/support/matchers/be_about_now.rb
Normal file
5
spec/support/matchers/be_about_now.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
RSpec::Matchers.define :be_about_now do
|
||||
match do |actual|
|
||||
expect(actual).to be_within(2.seconds).of(Time.now)
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue