Rspec 3 syntax

This commit is contained in:
Adam Meehan 2015-12-29 14:55:03 +11:00
parent 1ddf0f1b0f
commit 08b16f155c
13 changed files with 150 additions and 149 deletions

3
.rspec
View File

@ -1,2 +1,3 @@
--format nested
--format documentation
--color
--require byebug

11
Gemfile
View File

@ -2,16 +2,15 @@ source 'http://rubygems.org'
gemspec
gem 'rails', '~> 4.0.0'
gem 'rspec', '~> 2.8'
gem 'rspec-rails', '~> 2.8'
gem 'rails', '~> 4.0.13'
gem 'rspec', '~> 3.0.0'
gem 'rspec-rails', '~> 3.0.0'
gem 'timecop'
gem 'rspec_tag_matchers'
gem 'ruby-debug', :platforms => [:jruby]
gem 'byebug', :platforms => [:ruby_20, :ruby_21, :ruby_22]
gem 'byebug'
gem 'appraisal'
gem 'sqlite3'
gem 'nokogiri', '1.6.6.2'
gem 'nokogiri', '1.6.7'
group :active_record do
gem 'sqlite3-ruby', :require => 'sqlite3'

View File

@ -24,6 +24,7 @@ Time.zone = 'Australia/Melbourne'
LOCALE_PATH = File.expand_path(File.dirname(__FILE__) + '/../lib/generators/validates_timeliness/templates/en.yml')
I18n.load_path.unshift(LOCALE_PATH)
I18n.available_locales = ['en', 'es']
# Extend TestModel as you would another ORM/ODM module
module TestModelShim

View File

@ -3,15 +3,15 @@ module ModelHelpers
# Some test helpers from Rails source
def invalid!(attr_name, values, error = nil)
with_each_person_value(attr_name, values) do |record, value|
record.should be_invalid
record.errors[attr_name].size.should >= 1
record.errors[attr_name].first.should == error if error
expect(record).to be_invalid
expect(record.errors[attr_name].size).to be >= 1
expect(record.errors[attr_name].first).to eq(error) if error
end
end
def valid!(attr_name, values)
with_each_person_value(attr_name, values) do |record, value|
record.should be_valid
expect(record).to be_valid
end
end

View File

@ -5,7 +5,6 @@ module TestModel
include ActiveModel::AttributeMethods
included do
attribute_method_suffix ""
attribute_method_suffix "="
cattr_accessor :model_attributes
end

View File

@ -1,8 +1,8 @@
require 'spec_helper'
describe ValidatesTimeliness::AttributeMethods do
it 'should define _timeliness_raw_value_for instance method' do
PersonWithShim.new.should respond_to(:_timeliness_raw_value_for)
it 'should define read_timeliness_attribute_before_type_cast instance method' do
expect(PersonWithShim.new).to respond_to(:read_timeliness_attribute_before_type_cast)
end
describe ".timeliness_validated_attributes" do
@ -12,7 +12,7 @@ describe ValidatesTimeliness::AttributeMethods do
PersonWithShim.validates_time :birth_time
PersonWithShim.validates_datetime :birth_datetime
PersonWithShim.timeliness_validated_attributes.should == [ :birth_date, :birth_time, :birth_datetime ]
expect(PersonWithShim.timeliness_validated_attributes).to eq([ :birth_date, :birth_time, :birth_datetime ])
end
end
@ -31,13 +31,13 @@ describe ValidatesTimeliness::AttributeMethods do
it 'should cache attribute raw value' do
r = PersonWithCache.new
r.birth_datetime = date_string = '2010-01-01'
r._timeliness_raw_value_for('birth_datetime').should == date_string
expect(r.read_timeliness_attribute_before_type_cast('birth_datetime')).to eq(date_string)
end
it 'should not overwrite user defined methods' do
e = Employee.new
e.birth_date = '2010-01-01'
e.redefined_birth_date_called.should be_true
expect(e.redefined_birth_date_called).to be_truthy
end
it 'should be undefined if model class has dynamic attribute methods reset' do
@ -46,13 +46,13 @@ describe ValidatesTimeliness::AttributeMethods do
r = PersonWithShim.new
r.birth_date = Time.now
write_method = RUBY_VERSION < '1.9' ? 'birth_date=' : :birth_date=
write_method = :birth_date=
PersonWithShim.send(:generated_timeliness_methods).instance_methods.should include(write_method)
expect(PersonWithShim.send(:generated_timeliness_methods).instance_methods).to include(write_method)
PersonWithShim.undefine_attribute_methods
PersonWithShim.send(:generated_timeliness_methods).instance_methods.should_not include(write_method)
expect(PersonWithShim.send(:generated_timeliness_methods).instance_methods).not_to include(write_method)
end
context "with plugin parser" do
@ -70,7 +70,7 @@ describe ValidatesTimeliness::AttributeMethods do
end
it 'should parse a string value' do
Timeliness::Parser.should_receive(:parse)
expect(Timeliness::Parser).to receive(:parse)
r = PersonWithParser.new
r.birth_date = '2010-01-01'
end
@ -80,7 +80,7 @@ describe ValidatesTimeliness::AttributeMethods do
context "before_type_cast method" do
it 'should not be defined if ORM does not support it' do
PersonWithShim.new.should_not respond_to(:birth_datetime_before_type_cast)
expect(PersonWithShim.new).not_to respond_to(:birth_datetime_before_type_cast)
end
end
end

View File

@ -12,68 +12,68 @@ describe ValidatesTimeliness::Conversion do
describe "#type_cast_value" do
describe "for date type" do
it "should return same value for date value" do
type_cast_value(Date.new(2010, 1, 1), :date).should == Date.new(2010, 1, 1)
expect(type_cast_value(Date.new(2010, 1, 1), :date)).to eq(Date.new(2010, 1, 1))
end
it "should return date part of time value" do
type_cast_value(Time.mktime(2010, 1, 1, 0, 0, 0), :date).should == Date.new(2010, 1, 1)
expect(type_cast_value(Time.mktime(2010, 1, 1, 0, 0, 0), :date)).to eq(Date.new(2010, 1, 1))
end
it "should return date part of datetime value" do
type_cast_value(DateTime.new(2010, 1, 1, 0, 0, 0), :date).should == Date.new(2010, 1, 1)
expect(type_cast_value(DateTime.new(2010, 1, 1, 0, 0, 0), :date)).to eq(Date.new(2010, 1, 1))
end
it 'should return nil for invalid value types' do
type_cast_value(12, :date).should == nil
expect(type_cast_value(12, :date)).to eq(nil)
end
end
describe "for time type" do
it "should return same value for time value matching dummy date part" do
type_cast_value(Time.utc(2000, 1, 1, 0, 0, 0), :time).should == Time.utc(2000, 1, 1, 0, 0, 0)
expect(type_cast_value(Time.utc(2000, 1, 1, 0, 0, 0), :time)).to eq(Time.utc(2000, 1, 1, 0, 0, 0))
end
it "should return dummy time value with same time part for time value with different date" do
type_cast_value(Time.utc(2010, 1, 1, 0, 0, 0), :time).should == Time.utc(2000, 1, 1, 0, 0, 0)
expect(type_cast_value(Time.utc(2010, 1, 1, 0, 0, 0), :time)).to eq(Time.utc(2000, 1, 1, 0, 0, 0))
end
it "should return dummy time only for date value" do
type_cast_value(Date.new(2010, 1, 1), :time).should == Time.utc(2000, 1, 1, 0, 0, 0)
expect(type_cast_value(Date.new(2010, 1, 1), :time)).to eq(Time.utc(2000, 1, 1, 0, 0, 0))
end
it "should return dummy date with time part for datetime value" do
type_cast_value(DateTime.civil_from_format(:utc, 2010, 1, 1, 12, 34, 56), :time).should == Time.utc(2000, 1, 1, 12, 34, 56)
expect(type_cast_value(DateTime.civil_from_format(:utc, 2010, 1, 1, 12, 34, 56), :time)).to eq(Time.utc(2000, 1, 1, 12, 34, 56))
end
it 'should return nil for invalid value types' do
type_cast_value(12, :time).should == nil
expect(type_cast_value(12, :time)).to eq(nil)
end
end
describe "for datetime type" do
it "should return Date as Time value" do
type_cast_value(Date.new(2010, 1, 1), :datetime).should == Time.local(2010, 1, 1, 0, 0, 0)
expect(type_cast_value(Date.new(2010, 1, 1), :datetime)).to eq(Time.local(2010, 1, 1, 0, 0, 0))
end
it "should return same Time value" do
value = Time.utc(2010, 1, 1, 12, 34, 56)
type_cast_value(Time.utc(2010, 1, 1, 12, 34, 56), :datetime).should == value
expect(type_cast_value(Time.utc(2010, 1, 1, 12, 34, 56), :datetime)).to eq(value)
end
it "should return as Time with same component values" do
type_cast_value(DateTime.civil_from_format(:utc, 2010, 1, 1, 12, 34, 56), :datetime).should == Time.utc(2010, 1, 1, 12, 34, 56)
expect(type_cast_value(DateTime.civil_from_format(:utc, 2010, 1, 1, 12, 34, 56), :datetime)).to eq(Time.utc(2010, 1, 1, 12, 34, 56))
end
it "should return same Time in correct zone if timezone aware" do
@timezone_aware = true
value = Time.utc(2010, 1, 1, 12, 34, 56)
result = type_cast_value(value, :datetime)
result.should == Time.zone.local(2010, 1, 1, 23, 34, 56)
result.zone.should == 'EST'
expect(result).to eq(Time.zone.local(2010, 1, 1, 23, 34, 56))
expect(result.zone).to eq('AEDT')
end
it 'should return nil for invalid value types' do
type_cast_value(12, :datetime).should == nil
expect(type_cast_value(12, :datetime)).to eq(nil)
end
end
@ -82,41 +82,41 @@ describe ValidatesTimeliness::Conversion do
it "should ignore usec on time values when evaluated" do
value = Time.utc(2010, 1, 1, 12, 34, 56, 10000)
type_cast_value(value, :datetime).should == Time.utc(2010, 1, 1, 12, 34, 56)
expect(type_cast_value(value, :datetime)).to eq(Time.utc(2010, 1, 1, 12, 34, 56))
end
it "should ignore usec and return time in correct zone if timezone aware" do
@timezone_aware = true
value = Time.utc(2010, 1, 1, 12, 34, 56, 10000)
result = type_cast_value(value, :datetime)
result.should == Time.zone.local(2010, 1, 1, 23, 34, 56)
result.zone.should == 'EST'
expect(result).to eq(Time.zone.local(2010, 1, 1, 23, 34, 56))
expect(result.zone).to eq('AEDT')
end
end
end
describe "#dummy_time" do
it 'should return Time with dummy date values but same time components' do
dummy_time(Time.utc(2010, 11, 22, 12, 34, 56)).should == Time.utc(2000, 1, 1, 12, 34, 56)
expect(dummy_time(Time.utc(2010, 11, 22, 12, 34, 56))).to eq(Time.utc(2000, 1, 1, 12, 34, 56))
end
it 'should return same value for Time which already has dummy date values' do
dummy_time(Time.utc(2000, 1, 1, 12, 34, 56)).should == Time.utc(2000, 1, 1, 12, 34, 56)
expect(dummy_time(Time.utc(2000, 1, 1, 12, 34, 56))).to eq(Time.utc(2000, 1, 1, 12, 34, 56))
end
it 'should return time component values shifted to current zone if timezone aware' do
@timezone_aware = true
dummy_time(Time.utc(2000, 1, 1, 12, 34, 56)).should == Time.zone.local(2000, 1, 1, 23, 34, 56)
expect(dummy_time(Time.utc(2000, 1, 1, 12, 34, 56))).to eq(Time.zone.local(2000, 1, 1, 23, 34, 56))
end
it 'should return base dummy time value for Date value' do
dummy_time(Date.new(2010, 11, 22)).should == Time.utc(2000, 1, 1, 0, 0, 0)
expect(dummy_time(Date.new(2010, 11, 22))).to eq(Time.utc(2000, 1, 1, 0, 0, 0))
end
describe "with custom dummy date" do
it 'should return dummy time with custom dummy date' do
with_config(:dummy_date_for_time_type, [2010, 1, 1] ) do
dummy_time(Time.utc(1999, 11, 22, 12, 34, 56)).should == Time.utc(2010, 1, 1, 12, 34, 56)
expect(dummy_time(Time.utc(1999, 11, 22, 12, 34, 56))).to eq(Time.utc(2010, 1, 1, 12, 34, 56))
end
end
end
@ -127,56 +127,56 @@ describe ValidatesTimeliness::Conversion do
it 'should return Date object as is' do
value = Date.new(2010,1,1)
evaluate_option_value(value, person).should == value
expect(evaluate_option_value(value, person)).to eq(value)
end
it 'should return Time object as is' do
value = Time.mktime(2010,1,1)
evaluate_option_value(value, person).should == value
expect(evaluate_option_value(value, person)).to eq(value)
end
it 'should return DateTime object as is' do
value = DateTime.new(2010,1,1,0,0,0)
evaluate_option_value(value, person).should == value
expect(evaluate_option_value(value, person)).to eq(value)
end
it 'should return Time value returned from proc with 0 arity' do
value = Time.mktime(2010,1,1)
evaluate_option_value(lambda { value }, person).should == value
expect(evaluate_option_value(lambda { value }, person)).to eq(value)
end
it 'should return Time value returned by record attribute call in proc arity of 1' do
value = Time.mktime(2010,1,1)
person.birth_time = value
evaluate_option_value(lambda {|r| r.birth_time }, person).should == value
expect(evaluate_option_value(lambda {|r| r.birth_time }, person)).to eq(value)
end
it 'should return Time value for attribute method symbol which returns Time' do
value = Time.mktime(2010,1,1)
person.birth_time = value
evaluate_option_value(:birth_time, person).should == value
expect(evaluate_option_value(:birth_time, person)).to eq(value)
end
it 'should return Time value is default zone from string time value' do
value = '2010-01-01 12:00:00'
evaluate_option_value(value, person).should == Time.utc(2010,1,1,12,0,0)
expect(evaluate_option_value(value, person)).to eq(Time.utc(2010,1,1,12,0,0))
end
it 'should return Time value is current zone from string time value if timezone aware' do
@timezone_aware = true
value = '2010-01-01 12:00:00'
evaluate_option_value(value, person).should == Time.zone.local(2010,1,1,12,0,0)
expect(evaluate_option_value(value, person)).to eq(Time.zone.local(2010,1,1,12,0,0))
end
it 'should return Time value in default zone from proc which returns string time' do
value = '2010-01-01 12:00:00'
evaluate_option_value(lambda { value }, person).should == Time.utc(2010,1,1,12,0,0)
expect(evaluate_option_value(lambda { value }, person)).to eq(Time.utc(2010,1,1,12,0,0))
end
it 'should return Time value for attribute method symbol which returns string time value' do
value = '2010-01-01 12:00:00'
person.birth_time = value
evaluate_option_value(:birth_time, person).should == Time.utc(2010,1,1,12,0,0)
expect(evaluate_option_value(:birth_time, person)).to eq(Time.zone.local(2010,1,1,12,0,0))
end
context "restriction shorthand" do
@ -185,17 +185,17 @@ describe ValidatesTimeliness::Conversion do
end
it 'should evaluate :now as current time' do
evaluate_option_value(:now, person).should == Time.now
expect(evaluate_option_value(:now, person)).to eq(Time.now)
end
it 'should evaluate :today as current time' do
evaluate_option_value(:today, person).should == Date.today
expect(evaluate_option_value(:today, person)).to eq(Date.today)
end
it 'should not use shorthand if symbol if is record method' do
time = 1.day.from_now
person.stub!(:now).and_return(time)
evaluate_option_value(:now, person).should == time
allow(person).to receive(:now).and_return(time)
expect(evaluate_option_value(:now, person)).to eq(time)
end
end
end
@ -205,7 +205,7 @@ describe ValidatesTimeliness::Conversion do
with_config(:use_plugin_parser, true)
it 'should use timeliness' do
Timeliness::Parser.should_receive(:parse)
expect(Timeliness::Parser).to receive(:parse)
parse('2000-01-01')
end
end
@ -215,20 +215,20 @@ describe ValidatesTimeliness::Conversion do
it 'should use Time.zone.parse attribute is timezone aware' do
@timezone_aware = true
Time.zone.should_receive(:parse)
expect(Time.zone).to receive(:parse)
parse('2000-01-01')
end
it 'should use value#to_time if use_plugin_parser setting is false and attribute is not timezone aware' do
@timezone_aware = false
value = '2000-01-01'
value.should_receive(:to_time)
expect(value).to receive(:to_time)
parse(value)
end
end
it 'should return nil if value is nil' do
parse(nil).should be_nil
expect(parse(nil)).to be_nil
end
end
end

View File

@ -111,7 +111,7 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do
@output = date_select(:person, :birth_date, :include_blank => true, :discard_day => true)
should_have_datetime_selected(:birth_date, :year => 2009, :month => 'February')
should_not_have_datetime_selected(:birth_time, :day)
@output.should have_tag("input[id=person_birth_date_3i][type=hidden][value='1']")
expect(@output).to have_tag("input[id=person_birth_date_3i][type=hidden][value='1']")
end
end

View File

@ -1,38 +1,38 @@
require 'spec_helper'
describe ValidatesTimeliness::Extensions::MultiparameterHandler do
describe 'ValidatesTimeliness::Extensions::MultiparameterHandler' do
context "time column" do
it 'should assign a string value for invalid date portion' do
employee = record_with_multiparameter_attribute(:birth_datetime, [2000, 2, 31, 12, 0, 0])
employee.birth_datetime_before_type_cast.should eq '2000-02-31 12:00:00'
expect(employee.birth_datetime_before_type_cast).to eq '2000-02-31 12:00:00'
end
it 'should assign a Time value for valid datetimes' do
employee = record_with_multiparameter_attribute(:birth_datetime, [2000, 2, 28, 12, 0, 0])
employee.birth_datetime_before_type_cast.should eq Time.zone.local(2000, 2, 28, 12, 0, 0)
expect(employee.birth_datetime_before_type_cast).to eq Time.zone.local(2000, 2, 28, 12, 0, 0)
end
it 'should assign a string value for incomplete time' do
employee = record_with_multiparameter_attribute(:birth_datetime, [2000, nil, nil])
employee.birth_datetime_before_type_cast.should eq '2000-00-00'
expect(employee.birth_datetime_before_type_cast).to eq '2000-00-00'
end
end
context "date column" do
it 'should assign a string value for invalid date' do
employee = record_with_multiparameter_attribute(:birth_date, [2000, 2, 31])
employee.birth_date_before_type_cast.should eq '2000-02-31'
expect(employee.birth_date_before_type_cast).to eq '2000-02-31'
end
it 'should assign a Date value for valid date' do
employee = record_with_multiparameter_attribute(:birth_date, [2000, 2, 28])
employee.birth_date_before_type_cast.should eq Date.new(2000, 2, 28)
expect(employee.birth_date_before_type_cast).to eq Date.new(2000, 2, 28)
end
it 'should assign a string value for incomplete date' do
employee = record_with_multiparameter_attribute(:birth_date, [2000, nil, nil])
employee.birth_date_before_type_cast.should eq '2000-00-00'
expect(employee.birth_date_before_type_cast).to eq '2000-00-00'
end
end
@ -41,4 +41,5 @@ describe ValidatesTimeliness::Extensions::MultiparameterHandler do
values.each_with_index {|value, index| hash["#{name}(#{index+1}i)"] = value.to_s }
Employee.new(hash)
end
end

View File

@ -4,27 +4,27 @@ describe ValidatesTimeliness, 'HelperMethods' do
let(:record) { Person.new }
it 'should define class validation methods' do
Person.should respond_to(:validates_date)
Person.should respond_to(:validates_time)
Person.should respond_to(:validates_datetime)
expect(Person).to respond_to(:validates_date)
expect(Person).to respond_to(:validates_time)
expect(Person).to respond_to(:validates_datetime)
end
it 'should define instance validation methods' do
record.should respond_to(:validates_date)
record.should respond_to(:validates_time)
record.should respond_to(:validates_datetime)
expect(record).to respond_to(:validates_date)
expect(record).to respond_to(:validates_time)
expect(record).to respond_to(:validates_datetime)
end
it 'should validate instance using class validation defined' do
Person.validates_date :birth_date
record.valid?
record.errors[:birth_date].should_not be_empty
expect(record.errors[:birth_date]).not_to be_empty
end
it 'should validate instance using instance valiation method' do
record.validates_date :birth_date
record.errors[:birth_date].should_not be_empty
expect(record.errors[:birth_date]).not_to be_empty
end
end

View File

@ -6,41 +6,41 @@ describe ValidatesTimeliness, 'ActiveRecord' do
let(:record) { Employee.new }
it 'should be defined for the class' do
ActiveRecord::Base.should respond_to(:validates_date)
ActiveRecord::Base.should respond_to(:validates_time)
ActiveRecord::Base.should respond_to(:validates_datetime)
expect(ActiveRecord::Base).to respond_to(:validates_date)
expect(ActiveRecord::Base).to respond_to(:validates_time)
expect(ActiveRecord::Base).to respond_to(:validates_datetime)
end
it 'should defines for the instance' do
record.should respond_to(:validates_date)
record.should respond_to(:validates_time)
record.should respond_to(:validates_datetime)
expect(record).to respond_to(:validates_date)
expect(record).to respond_to(:validates_time)
expect(record).to respond_to(:validates_datetime)
end
it "should validate a valid value string" do
record.birth_date = '2012-01-01'
record.valid?
record.errors[:birth_date].should be_empty
expect(record.errors[:birth_date]).to be_empty
end
it "should validate a invalid value string" do
record.birth_date = 'not a date'
record.valid?
record.errors[:birth_date].should_not be_empty
expect(record.errors[:birth_date]).not_to be_empty
end
it "should validate a nil value" do
record.birth_date = nil
record.valid?
record.errors[:birth_date].should be_empty
expect(record.errors[:birth_date]).to be_empty
end
end
it 'should determine type for attribute' do
Employee.timeliness_attribute_type(:birth_date).should eq :date
expect(Employee.timeliness_attribute_type(:birth_date)).to eq :date
end
context 'attribute timezone awareness' do
@ -58,17 +58,17 @@ describe ValidatesTimeliness, 'ActiveRecord' do
context 'for column attribute' do
it 'should be detected from column type' do
klass.timeliness_attribute_timezone_aware?(:birth_date).should be_false
klass.timeliness_attribute_timezone_aware?(:birth_time).should be_false
klass.timeliness_attribute_timezone_aware?(:birth_datetime).should be_true
expect(klass.timeliness_attribute_timezone_aware?(:birth_date)).to be_falsey
expect(klass.timeliness_attribute_timezone_aware?(:birth_time)).to be_falsey
expect(klass.timeliness_attribute_timezone_aware?(:birth_datetime)).to be_truthy
end
end
context 'for non-column attribute' do
it 'should be detected from the validation type' do
klass.timeliness_attribute_timezone_aware?(:some_date).should be_false
klass.timeliness_attribute_timezone_aware?(:some_time).should be_false
klass.timeliness_attribute_timezone_aware?(:some_datetime).should be_true
expect(klass.timeliness_attribute_timezone_aware?(:some_date)).to be_falsey
expect(klass.timeliness_attribute_timezone_aware?(:some_time)).to be_falsey
expect(klass.timeliness_attribute_timezone_aware?(:some_datetime)).to be_truthy
end
end
end
@ -88,7 +88,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
it 'should store raw value' do
record.birth_datetime = datetime_string = '2010-01-01 12:30'
record._timeliness_raw_value_for('birth_datetime').should eq datetime_string
expect(record.read_timeliness_attribute_before_type_cast('birth_datetime')).to eq datetime_string
end
end
@ -96,7 +96,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
it 'should store raw value' do
record.birth_date = date_string = '2010-01-01'
record._timeliness_raw_value_for('birth_date').should eq date_string
expect(record.read_timeliness_attribute_before_type_cast('birth_date')).to eq date_string
end
end
@ -104,7 +104,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
it 'should store raw value' do
record.birth_time = time_string = '12:12'
record._timeliness_raw_value_for('birth_time').should eq time_string
expect(record.read_timeliness_attribute_before_type_cast('birth_time')).to eq time_string
end
end
end
@ -122,13 +122,13 @@ describe ValidatesTimeliness, 'ActiveRecord' do
context "for a date column" do
it 'should parse a string value' do
Timeliness::Parser.should_receive(:parse)
expect(Timeliness::Parser).to receive(:parse)
record.birth_date = '2010-01-01'
end
it 'should parse a invalid string value as nil' do
Timeliness::Parser.should_receive(:parse)
expect(Timeliness::Parser).to receive(:parse)
record.birth_date = 'not valid'
end
@ -136,20 +136,20 @@ describe ValidatesTimeliness, 'ActiveRecord' do
it 'should store a Date value after parsing string' do
record.birth_date = '2010-01-01'
record.birth_date.should be_kind_of(Date)
record.birth_date.should eq Date.new(2010, 1, 1)
expect(record.birth_date).to be_kind_of(Date)
expect(record.birth_date).to eq Date.new(2010, 1, 1)
end
end
context "for a time column" do
it 'should parse a string value' do
Timeliness::Parser.should_receive(:parse)
expect(Timeliness::Parser).to receive(:parse)
record.birth_time = '12:30'
end
it 'should parse a invalid string value as nil' do
Timeliness::Parser.should_receive(:parse)
expect(Timeliness::Parser).to receive(:parse)
record.birth_time = 'not valid'
end
@ -157,8 +157,8 @@ describe ValidatesTimeliness, 'ActiveRecord' do
it 'should store a Time value after parsing string' do
record.birth_time = '12:30'
record.birth_time.should be_kind_of(Time)
record.birth_time.should eq Time.utc(2000, 1, 1, 12, 30)
expect(record.birth_time).to be_kind_of(Time)
expect(record.birth_time).to eq Time.utc(2000, 1, 1, 12, 30)
end
end
@ -166,13 +166,13 @@ describe ValidatesTimeliness, 'ActiveRecord' do
with_config(:default_timezone, 'Australia/Melbourne')
it 'should parse a string value' do
Timeliness::Parser.should_receive(:parse)
expect(Timeliness::Parser).to receive(:parse)
record.birth_datetime = '2010-01-01 12:00'
end
it 'should parse a invalid string value as nil' do
Timeliness::Parser.should_receive(:parse)
expect(Timeliness::Parser).to receive(:parse)
record.birth_datetime = 'not valid'
end
@ -180,13 +180,13 @@ describe ValidatesTimeliness, 'ActiveRecord' do
it 'should parse string into Time value' do
record.birth_datetime = '2010-01-01 12:00'
record.birth_datetime.should be_kind_of(Time)
expect(record.birth_datetime).to be_kind_of(Time)
end
it 'should parse string as current timezone' do
record.birth_datetime = '2010-06-01 12:00'
record.birth_datetime.utc_offset.should eq Time.zone.utc_offset
expect(record.birth_datetime.utc_offset).to eq Time.zone.utc_offset
end
end
end
@ -199,7 +199,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
record.reload
record._timeliness_raw_value_for('birth_date').should be_nil
expect(record.read_timeliness_attribute_before_type_cast('birth_date')).to be_nil
end
end
@ -207,13 +207,13 @@ describe ValidatesTimeliness, 'ActiveRecord' do
let(:record) { Employee.new }
it 'should be defined on class if ORM supports it' do
record.should respond_to(:birth_datetime_before_type_cast)
expect(record).to respond_to(:birth_datetime_before_type_cast)
end
it 'should return original value' do
record.birth_datetime = date_string = '2010-01-01'
record.birth_datetime_before_type_cast.should eq date_string
expect(record.birth_datetime_before_type_cast).to eq date_string
end
it 'should return attribute if no attribute assignment has been made' do
@ -221,7 +221,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
Employee.create(:birth_datetime => datetime)
record = Employee.last
record.birth_datetime_before_type_cast.should match(/#{datetime.utc.to_s[0...-4]}/)
expect(record.birth_datetime_before_type_cast).to match(/#{datetime.utc.to_s[0...-4]}/)
end
context "with plugin parser" do
@ -230,7 +230,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
it 'should return original value' do
record.birth_datetime = date_string = '2010-01-31'
record.birth_datetime_before_type_cast.should eq date_string
expect(record.birth_datetime_before_type_cast).to eq date_string
end
end
@ -238,7 +238,7 @@ describe ValidatesTimeliness, 'ActiveRecord' do
context "define_attribute_methods" do
it "returns a falsy value if the attribute methods have already been generated" do
Employee.define_attribute_methods.should be_false
expect(Employee.define_attribute_methods).to be_falsey
end
end
end

View File

@ -8,22 +8,22 @@ describe ValidatesTimeliness::Validator do
describe "Model.validates with :timeliness option" do
it 'should use plugin validator class' do
Person.validates :birth_date, :timeliness => {:is_at => Date.new(2010,1,1), :type => :date}
Person.validators.should have(1).kind_of(ActiveModel::Validations::TimelinessValidator)
expect(Person.validators.select { |v| v.is_a?(ActiveModel::Validations::TimelinessValidator) }.size).to eq(1)
invalid!(:birth_date, Date.new(2010,1,2))
valid!(:birth_date, Date.new(2010,1,1))
end
it 'should use default to :datetime type' do
Person.validates :birth_datetime, :timeliness => {:is_at => Time.mktime(2010,1,1)}
Person.validators.first.type.should == :datetime
expect(Person.validators.first.type).to eq(:datetime)
end
it 'should add attribute to timeliness attributes set' do
PersonWithShim.timeliness_validated_attributes.should_not include(:birth_time)
expect(PersonWithShim.timeliness_validated_attributes).not_to include(:birth_time)
PersonWithShim.validates :birth_time, :timeliness => {:is_at => "12:30"}
PersonWithShim.timeliness_validated_attributes.should include(:birth_time)
expect(PersonWithShim.timeliness_validated_attributes).to include(:birth_time)
end
end
@ -35,10 +35,10 @@ describe ValidatesTimeliness::Validator do
it 'should not be valid attribute is type cast to nil but raw value is non-nil invalid value' do
Person.validates_date :birth_date, :allow_nil => true
record = Person.new
record.stub!(:birth_date).and_return(nil)
record.stub!(:_timeliness_raw_value_for).and_return("Not a date")
record.should_not be_valid
record.errors[:birth_date].first.should == 'is not a valid date'
allow(record).to receive(:birth_date).and_return(nil)
allow(record).to receive(:read_timeliness_attribute_before_type_cast).and_return("Not a date")
expect(record).not_to be_valid
expect(record.errors[:birth_date].first).to eq('is not a valid date')
end
describe ":allow_nil option" do
@ -60,7 +60,7 @@ describe ValidatesTimeliness::Validator do
p = PersonWithShim.new
p.birth_date = 'bogus'
p.should_not be_valid
expect(p).not_to be_valid
end
end
end
@ -84,7 +84,7 @@ describe ValidatesTimeliness::Validator do
p = PersonWithShim.new
p.birth_date = 'bogus'
p.should_not be_valid
expect(p).not_to be_valid
end
end
end
@ -94,8 +94,8 @@ describe ValidatesTimeliness::Validator do
it 'should be split option into :on_or_after and :on_or_before values' do
on_or_after, on_or_before = Date.new(2010,1,1), Date.new(2010,1,2)
Person.validates_date :birth_date, :between => [on_or_after, on_or_before]
Person.validators.first.options[:on_or_after].should == on_or_after
Person.validators.first.options[:on_or_before].should == on_or_before
expect(Person.validators.first.options[:on_or_after]).to eq(on_or_after)
expect(Person.validators.first.options[:on_or_before]).to eq(on_or_before)
invalid!(:birth_date, on_or_after - 1, "must be on or after 2010-01-01")
invalid!(:birth_date, on_or_before + 1, "must be on or before 2010-01-02")
valid!(:birth_date, on_or_after)
@ -107,8 +107,8 @@ describe ValidatesTimeliness::Validator do
it 'should be split option into :on_or_after and :on_or_before values' do
on_or_after, on_or_before = Date.new(2010,1,1), Date.new(2010,1,2)
Person.validates_date :birth_date, :between => on_or_after..on_or_before
Person.validators.first.options[:on_or_after].should == on_or_after
Person.validators.first.options[:on_or_before].should == on_or_before
expect(Person.validators.first.options[:on_or_after]).to eq(on_or_after)
expect(Person.validators.first.options[:on_or_before]).to eq(on_or_before)
invalid!(:birth_date, on_or_after - 1, "must be on or after 2010-01-01")
invalid!(:birth_date, on_or_before + 1, "must be on or before 2010-01-02")
valid!(:birth_date, on_or_after)
@ -120,8 +120,8 @@ describe ValidatesTimeliness::Validator do
it 'should be split option into :on_or_after and :before values' do
on_or_after, before = Date.new(2010,1,1), Date.new(2010,1,3)
Person.validates_date :birth_date, :between => on_or_after...before
Person.validators.first.options[:on_or_after].should == on_or_after
Person.validators.first.options[:before].should == before
expect(Person.validators.first.options[:on_or_after]).to eq(on_or_after)
expect(Person.validators.first.options[:before]).to eq(before)
invalid!(:birth_date, on_or_after - 1, "must be on or after 2010-01-01")
invalid!(:birth_date, before, "must be before 2010-01-03")
valid!(:birth_date, on_or_after)
@ -159,13 +159,13 @@ describe ValidatesTimeliness::Validator do
it "should be valid when value matches format" do
person.birth_date = '11-12-1913'
person.valid?
person.errors[:birth_date].should be_empty
expect(person.errors[:birth_date]).to be_empty
end
it "should not be valid when value does not match format" do
person.birth_date = '1913-12-11'
person.valid?
person.errors[:birth_date].should include('is not a valid date')
expect(person.errors[:birth_date]).to include('is not a valid date')
end
end
@ -179,21 +179,21 @@ describe ValidatesTimeliness::Validator do
it "should be added when ignore_restriction_errors is false" do
with_config(:ignore_restriction_errors, false) do
person.valid?
person.errors[:birth_date].first.should match("Error occurred validating birth_date")
expect(person.errors[:birth_date].first).to match("Error occurred validating birth_date")
end
end
it "should not be added when ignore_restriction_errors is true" do
with_config(:ignore_restriction_errors, true) do
person.valid?
person.errors[:birth_date].should be_empty
expect(person.errors[:birth_date]).to be_empty
end
end
it 'should exit on first error' do
with_config(:ignore_restriction_errors, false) do
person.valid?
person.errors[:birth_date].should have(1).items
expect(person.errors[:birth_date].size).to eq(1)
end
end
end
@ -202,17 +202,17 @@ describe ValidatesTimeliness::Validator do
describe "default" do
it 'should format date error value as yyyy-mm-dd' do
validator = ValidatesTimeliness::Validator.new(:attributes => [:birth_date], :type => :date)
validator.format_error_value(Date.new(2010,1,1)).should == '2010-01-01'
expect(validator.format_error_value(Date.new(2010,1,1))).to eq('2010-01-01')
end
it 'should format time error value as hh:nn:ss' do
validator = ValidatesTimeliness::Validator.new(:attributes => [:birth_time], :type => :time)
validator.format_error_value(Time.mktime(2010,1,1,12,34,56)).should == '12:34:56'
expect(validator.format_error_value(Time.mktime(2010,1,1,12,34,56))).to eq('12:34:56')
end
it 'should format datetime error value as yyyy-mm-dd hh:nn:ss' do
validator = ValidatesTimeliness::Validator.new(:attributes => [:birth_datetime], :type => :datetime)
validator.format_error_value(Time.mktime(2010,1,1,12,34,56)).should == '2010-01-01 12:34:56'
expect(validator.format_error_value(Time.mktime(2010,1,1,12,34,56))).to eq('2010-01-01 12:34:56')
end
end
@ -223,7 +223,7 @@ describe ValidatesTimeliness::Validator do
it 'should use the default format for the type' do
validator = ValidatesTimeliness::Validator.new(:attributes => [:birth_date], :type => :date)
validator.format_error_value(Date.new(2010,1,1)).should == '2010-01-01'
expect(validator.format_error_value(Date.new(2010,1,1))).to eq('2010-01-01')
end
after :all do

View File

@ -3,39 +3,39 @@ require 'spec_helper'
describe ValidatesTimeliness do
it 'should alias use_euro_formats to remove_us_formats on Timeliness gem' do
Timeliness.should respond_to(:remove_us_formats)
expect(Timeliness).to respond_to(:remove_us_formats)
end
it 'should alias to date_for_time_type to dummy_date_for_time_type on Timeliness gem' do
Timeliness.should respond_to(:dummy_date_for_time_type)
expect(Timeliness).to respond_to(:dummy_date_for_time_type)
end
describe "config" do
it 'should delegate default_timezone to Timeliness gem' do
Timeliness.should_receive(:default_timezone=)
expect(Timeliness).to receive(:default_timezone=)
ValidatesTimeliness.default_timezone = :utc
end
it 'should delegate dummy_date_for_time_type to Timeliness gem' do
Timeliness.should_receive(:dummy_date_for_time_type)
Timeliness.should_receive(:dummy_date_for_time_type=)
expect(Timeliness).to receive(:dummy_date_for_time_type)
expect(Timeliness).to receive(:dummy_date_for_time_type=)
array = ValidatesTimeliness.dummy_date_for_time_type
ValidatesTimeliness.dummy_date_for_time_type = array
end
context "parser" do
it 'should delegate add_formats to Timeliness gem' do
Timeliness.should_receive(:add_formats)
expect(Timeliness).to receive(:add_formats)
ValidatesTimeliness.parser.add_formats
end
it 'should delegate remove_formats to Timeliness gem' do
Timeliness.should_receive(:remove_formats)
expect(Timeliness).to receive(:remove_formats)
ValidatesTimeliness.parser.remove_formats
end
it 'should delegate remove_us_formats to Timeliness gem' do
Timeliness.should_receive(:remove_us_formats)
expect(Timeliness).to receive(:remove_us_formats)
ValidatesTimeliness.parser.remove_us_formats
end
end