118 lines
4.5 KiB
Diff
118 lines
4.5 KiB
Diff
From 76e63b2638e1fcc84819bdca49d768a635346f5d Mon Sep 17 00:00:00 2001
|
|
From: Elliot Winkler <elliot.winkler@gmail.com>
|
|
Date: Mon, 24 Jul 2017 22:24:12 -0500
|
|
Subject: [PATCH] Rails 5: Fix validate_inclusion_of tests
|
|
|
|
---
|
|
spec/support/unit/rails_application.rb | 14 ++++++++++
|
|
.../validate_inclusion_of_matcher_spec.rb | 27 ++++++++++---------
|
|
2 files changed, 28 insertions(+), 13 deletions(-)
|
|
|
|
diff --git a/spec/support/unit/rails_application.rb b/spec/support/unit/rails_application.rb
|
|
index 3dc07aa9..8e3ae625 100644
|
|
--- a/spec/support/unit/rails_application.rb
|
|
+++ b/spec/support/unit/rails_application.rb
|
|
@@ -2,6 +2,7 @@
|
|
require_relative '../tests/command_runner'
|
|
require_relative '../tests/database'
|
|
require_relative '../tests/filesystem'
|
|
+require_relative 'helpers/rails_versions'
|
|
|
|
require 'yaml'
|
|
|
|
@@ -75,6 +76,10 @@ def generate
|
|
rails_new
|
|
fix_available_locales_warning
|
|
write_database_configuration
|
|
+
|
|
+ if bundle.version_of("rails") >= 5
|
|
+ add_initializer_for_time_zone_aware_types
|
|
+ end
|
|
end
|
|
|
|
def rails_new
|
|
@@ -97,6 +102,15 @@ def write_database_configuration
|
|
YAML.dump(database.config.to_hash, fs.open('config/database.yml', 'w'))
|
|
end
|
|
|
|
+ def add_initializer_for_time_zone_aware_types
|
|
+ path = 'config/initializers/configure_time_zone_aware_types.rb'
|
|
+ fs.write(path, <<~TEXT)
|
|
+ Rails.application.configure do
|
|
+ config.active_record.time_zone_aware_types = [:datetime, :time]
|
|
+ end
|
|
+ TEXT
|
|
+ end
|
|
+
|
|
def load_environment
|
|
require environment_file_path
|
|
end
|
|
diff --git a/spec/unit/shoulda/matchers/active_model/validate_inclusion_of_matcher_spec.rb b/spec/unit/shoulda/matchers/active_model/validate_inclusion_of_matcher_spec.rb
|
|
index d944c466..62a322d4 100644
|
|
--- a/spec/unit/shoulda/matchers/active_model/validate_inclusion_of_matcher_spec.rb
|
|
+++ b/spec/unit/shoulda/matchers/active_model/validate_inclusion_of_matcher_spec.rb
|
|
@@ -192,20 +192,19 @@ def validation_matcher_scenario_args
|
|
end
|
|
|
|
context 'against a time attribute' do
|
|
- now = Time.now
|
|
+ default_time = Time.zone.local(2000, 1, 1)
|
|
|
|
- define_method(:now) { now }
|
|
+ define_method(:default_time) { default_time }
|
|
|
|
it_behaves_like 'it supports in_array',
|
|
- possible_values: (1..5).map { |n| now + n },
|
|
- reserved_outside_value: described_class::ARBITRARY_OUTSIDE_TIME
|
|
+ possible_values: (1..3).map { |hour| default_time.change(hour: hour) }
|
|
|
|
it_behaves_like 'it supports in_range',
|
|
- possible_values: (now .. now + 5)
|
|
+ possible_values: (default_time.change(hour: 1) .. default_time.change(hour: 3))
|
|
|
|
define_method :build_object do |options = {}, &block|
|
|
build_object_with_generic_attribute(
|
|
- options.merge(column_type: :time, value: now),
|
|
+ options.merge(column_type: :time, value: default_time),
|
|
&block
|
|
)
|
|
end
|
|
@@ -215,7 +214,7 @@ def add_outside_value_to(values)
|
|
end
|
|
|
|
def validation_matcher_scenario_args
|
|
- super.deep_merge(column_type: :time, default_value: now)
|
|
+ super.deep_merge(column_type: :time, default_value: default_time)
|
|
end
|
|
end
|
|
|
|
@@ -434,7 +433,7 @@ def configure_validation_matcher(matcher)
|
|
shared_examples_for 'it supports in_array' do |args|
|
|
possible_values = args.fetch(:possible_values)
|
|
zero = args[:zero]
|
|
- reserved_outside_value = args.fetch(:reserved_outside_value)
|
|
+ reserved_outside_value = args[:reserved_outside_value]
|
|
|
|
define_method(:valid_values) { args.fetch(:possible_values) }
|
|
|
|
@@ -466,12 +465,14 @@ def configure_validation_matcher(matcher)
|
|
expect_not_to_match_on_values(builder, add_outside_value_to(possible_values))
|
|
end
|
|
|
|
- it 'raises an error when valid and given value is our test outside value' do
|
|
- error_class = Shoulda::Matchers::ActiveModel::CouldNotDetermineValueOutsideOfArray
|
|
- builder = build_object_allowing([reserved_outside_value])
|
|
+ if reserved_outside_value
|
|
+ it 'raises an error when valid and given value is our test outside value' do
|
|
+ error_class = Shoulda::Matchers::ActiveModel::CouldNotDetermineValueOutsideOfArray
|
|
+ builder = build_object_allowing([reserved_outside_value])
|
|
|
|
- expect { expect_to_match_on_values(builder, [reserved_outside_value]) }.
|
|
- to raise_error(error_class)
|
|
+ expect { expect_to_match_on_values(builder, [reserved_outside_value]) }.
|
|
+ to raise_error(error_class)
|
|
+ end
|
|
end
|
|
|
|
it_behaves_like 'it supports allow_nil', valid_values: possible_values
|