From eb6bccb3e3cdeef6948cfb7b187e084982318075 Mon Sep 17 00:00:00 2001 From: Oliver Peate Date: Wed, 31 Jul 2019 18:26:25 +0100 Subject: [PATCH] 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. --- spec/acceptance/build_stubbed_spec.rb | 27 +++++++++++++------------- spec/factory_bot/strategy/stub_spec.rb | 9 +++------ spec/spec_helper.rb | 6 ------ spec/support/matchers/be_about_now.rb | 5 +++++ 4 files changed, 21 insertions(+), 26 deletions(-) create mode 100644 spec/support/matchers/be_about_now.rb diff --git a/spec/acceptance/build_stubbed_spec.rb b/spec/acceptance/build_stubbed_spec.rb index 9dd933c..2af6bf5 100644 --- a/spec/acceptance/build_stubbed_spec.rb +++ b/spec/acceptance/build_stubbed_spec.rb @@ -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 diff --git a/spec/factory_bot/strategy/stub_spec.rb b/spec/factory_bot/strategy/stub_spec.rb index 3a83f83..4ce2711 100644 --- a/spec/factory_bot/strategy/stub_spec.rb +++ b/spec/factory_bot/strategy/stub_spec.rb @@ -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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2e28555..0220f6a 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -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 diff --git a/spec/support/matchers/be_about_now.rb b/spec/support/matchers/be_about_now.rb new file mode 100644 index 0000000..ef5f771 --- /dev/null +++ b/spec/support/matchers/be_about_now.rb @@ -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