From 711ec2e7d945e62c426de17007f26a375ae23c7f Mon Sep 17 00:00:00 2001 From: James Edwards-Jones Date: Thu, 18 Jan 2018 15:05:47 +0000 Subject: Improve rspec matcher for delayed scheduled migrations Failure message includes difference in arguments and in scheduled times. --- spec/support/background_migrations_matchers.rb | 56 +++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 5 deletions(-) diff --git a/spec/support/background_migrations_matchers.rb b/spec/support/background_migrations_matchers.rb index f4127efc6ae..3d93ed61c60 100644 --- a/spec/support/background_migrations_matchers.rb +++ b/spec/support/background_migrations_matchers.rb @@ -1,14 +1,60 @@ RSpec::Matchers.define :be_scheduled_delayed_migration do |delay, *expected| + include ActionView::Helpers::DateHelper + match do |migration| - BackgroundMigrationWorker.jobs.any? do |job| - job['args'] == [migration, expected] && - job['at'].to_i == (delay.to_i + Time.now.to_i) + @delay = delay + @migration = migration + @expected = expected + + jobs_with_expected_args.any? do |job| + job['at'].to_i == expected_time end end + def expected_time + (@delay.to_i + Time.now.to_i) + end + + def expected_args + [migration_name, @expected] + end + + def migration_name + @migration.is_a?(Class) ? @migration.to_s.demodulize : @migration + end + + def jobs_with_expected_args + BackgroundMigrationWorker.jobs.select do |job| + job['args'] == expected_args + end + end + + def found_args + BackgroundMigrationWorker.jobs.map {|job| job['args'] } + end + + def found_times + jobs_with_expected_args.map {|job| job['at'].to_i } + .map {|time| time_from_now(time) } + end + + def expected_time_in_words + time_from_now(expected_time) + end + + def time_from_now(time) + distance_of_time_in_words(Time.now, Time.at(time)) + end + failure_message do |migration| - "Migration `#{migration}` with args `#{expected.inspect}` " \ - 'not scheduled in expected time!' + if jobs_with_expected_args.any? + "Migration `#{migration_name}` with args `#{@expected.inspect}`" \ + " should have been scheduled in #{expected_time_in_words} but" \ + " was found in #{found_times}" + else + "Migration `#{migration_name}` not found scheduled with args `#{@expected.inspect}`" \ + " but was found with args #{found_args}" + end end end -- cgit v1.2.1