change spec config setup to use class method with_config

This commit is contained in:
Adam Meehan 2011-01-29 16:33:37 +11:00
parent 3793ef2ed4
commit 38457ec334
7 changed files with 48 additions and 76 deletions

View File

@ -1,4 +1,6 @@
module ConfigHelper module ConfigHelper
extend ActiveSupport::Concern
# Justin French tip # Justin French tip
def with_config(preference_name, temporary_value) def with_config(preference_name, temporary_value)
old_value = ValidatesTimeliness.send(preference_name) old_value = ValidatesTimeliness.send(preference_name)
@ -7,4 +9,18 @@ module ConfigHelper
ensure ensure
ValidatesTimeliness.send(:"#{preference_name}=", old_value) ValidatesTimeliness.send(:"#{preference_name}=", old_value)
end end
module ClassMethods
def with_config(preference_name, temporary_value)
original_config_value = ValidatesTimeliness.send(preference_name)
before(:all) do
ValidatesTimeliness.send(:"#{preference_name}=", temporary_value)
end
after(:all) do
ValidatesTimeliness.send(:"#{preference_name}=", original_config_value)
end
end
end
end end

View File

@ -41,6 +41,8 @@ describe ValidatesTimeliness::AttributeMethods do
end end
context "with plugin parser" do context "with plugin parser" do
with_config(:use_plugin_parser, true)
class PersonWithParser class PersonWithParser
include TestModel include TestModel
include TestModelShim include TestModelShim
@ -52,10 +54,6 @@ describe ValidatesTimeliness::AttributeMethods do
validates_datetime :birth_datetime validates_datetime :birth_datetime
end end
before :all do
ValidatesTimeliness.use_plugin_parser = true
end
it 'should parse a string value' do it 'should parse a string value' do
Timeliness::Parser.should_receive(:parse) Timeliness::Parser.should_receive(:parse)
r = PersonWithParser.new r = PersonWithParser.new
@ -67,10 +65,6 @@ describe ValidatesTimeliness::AttributeMethods do
r.birth_datetime = '2010-01-01 12:00' r.birth_datetime = '2010-01-01 12:00'
r.birth_datetime.zone == Time.zone.name r.birth_datetime.zone == Time.zone.name
end end
after :all do
ValidatesTimeliness.use_plugin_parser = false
end
end end
end end

View File

@ -114,17 +114,10 @@ describe ValidatesTimeliness::Conversion do
end end
describe "with custom dummy date" do describe "with custom dummy date" do
before do
@original_dummy_date = ValidatesTimeliness.dummy_date_for_time_type
ValidatesTimeliness.dummy_date_for_time_type = [2010, 1, 1]
end
it 'should return dummy time with custom dummy date' do it 'should return dummy time with custom dummy date' do
dummy_time(Time.utc(1999, 11, 22, 12, 34, 56)).should == Time.utc(2010, 1, 1, 12, 34, 56) with_config(:dummy_date_for_time_type, [2010, 1, 1] ) do
end dummy_time(Time.utc(1999, 11, 22, 12, 34, 56)).should == Time.utc(2010, 1, 1, 12, 34, 56)
end
after do
ValidatesTimeliness.dummy_date_for_time_type = @original_dummy_date
end end
end end
end end
@ -209,9 +202,7 @@ describe ValidatesTimeliness::Conversion do
describe "#parse" do describe "#parse" do
context "use_plugin_parser setting is true" do context "use_plugin_parser setting is true" do
around do |example| with_config(:use_plugin_parser, true)
with_config(:use_plugin_parser, true, &example)
end
it 'should use timeliness' do it 'should use timeliness' do
Timeliness::Parser.should_receive(:parse) Timeliness::Parser.should_receive(:parse)
@ -220,9 +211,7 @@ describe ValidatesTimeliness::Conversion do
end end
context "use_plugin_parser setting is false" do context "use_plugin_parser setting is false" do
around do |example| with_config(:use_plugin_parser, false)
with_config(:use_plugin_parser, false, &example)
end
it 'should use Time.zone.parse attribute is timezone aware' do it 'should use Time.zone.parse attribute is timezone aware' do
@timezone_aware = true @timezone_aware = true

View File

@ -4,9 +4,7 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do
include ActionView::Helpers::DateHelper include ActionView::Helpers::DateHelper
attr_reader :person, :params attr_reader :person, :params
before :all do with_config(:use_plugin_parser, true)
ValidatesTimeliness.use_plugin_parser = true
end
before do before do
@person = Person.new @person = Person.new
@ -177,8 +175,4 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do
output.should_not have_tag('select[id=person_birth_time_6i] option[selected=selected]') output.should_not have_tag('select[id=person_birth_time_6i] option[selected=selected]')
end end
end end
after :all do
ValidatesTimeliness.use_plugin_parser = false
end
end end

View File

@ -33,16 +33,14 @@ describe ValidatesTimeliness, 'ActiveRecord' do
end end
context "with plugin parser" do context "with plugin parser" do
with_config(:use_plugin_parser, true)
class EmployeeWithParser < ActiveRecord::Base class EmployeeWithParser < ActiveRecord::Base
set_table_name 'employees' set_table_name 'employees'
validates_date :birth_date validates_date :birth_date
validates_datetime :birth_datetime validates_datetime :birth_datetime
end end
before :all do
ValidatesTimeliness.use_plugin_parser = true
end
it 'should parse a string value' do it 'should parse a string value' do
Timeliness::Parser.should_receive(:parse) Timeliness::Parser.should_receive(:parse)
r = EmployeeWithParser.new r = EmployeeWithParser.new
@ -54,11 +52,6 @@ describe ValidatesTimeliness, 'ActiveRecord' do
r.birth_datetime = '2010-06-01 12:00' r.birth_datetime = '2010-06-01 12:00'
r.birth_datetime.utc_offset.should == 10.hours r.birth_datetime.utc_offset.should == 10.hours
end end
after :all do
Time.zone = 'Australia/Melbourne'
ValidatesTimeliness.use_plugin_parser = false
end
end end
end end

View File

@ -12,20 +12,20 @@ Mongoid.configure do |config|
config.persist_in_safe_mode = false config.persist_in_safe_mode = false
end end
class Article
::ValidatesTimeliness.use_plugin_parser = true
include Mongoid::Document
field :publish_date, :type => Date
field :publish_time, :type => Time
field :publish_datetime, :type => DateTime
validates_date :publish_date, :allow_nil => true
validates_time :publish_time, :allow_nil => true
validates_datetime :publish_datetime, :allow_nil => true
::ValidatesTimeliness.use_plugin_parser = false
end
describe ValidatesTimeliness, 'Mongoid' do describe ValidatesTimeliness, 'Mongoid' do
class Article
::ValidatesTimeliness.use_plugin_parser = true
include Mongoid::Document
field :publish_date, :type => Date
field :publish_time, :type => Time
field :publish_datetime, :type => DateTime
validates_date :publish_date, :allow_nil => true
validates_time :publish_time, :allow_nil => true
validates_datetime :publish_datetime, :allow_nil => true
::ValidatesTimeliness.use_plugin_parser = false
end
context "validation methods" do context "validation methods" do
it 'should be defined on the class' do it 'should be defined on the class' do
Article.should respond_to(:validates_date) Article.should respond_to(:validates_date)
@ -52,9 +52,7 @@ describe ValidatesTimeliness, 'Mongoid' do
end end
context "with plugin parser" do context "with plugin parser" do
before :all do with_config(:use_plugin_parser, false)
ValidatesTimeliness.use_plugin_parser = true
end
it 'should parse a string value' do it 'should parse a string value' do
Timeliness::Parser.should_receive(:parse) Timeliness::Parser.should_receive(:parse)
@ -67,10 +65,6 @@ describe ValidatesTimeliness, 'Mongoid' do
r.publish_datetime = '2010-01-01 12:00' r.publish_datetime = '2010-01-01 12:00'
r.publish_datetime.should == Time.utc(2010,1,1,12,0) r.publish_datetime.should == Time.utc(2010,1,1,12,0)
end end
after :all do
ValidatesTimeliness.use_plugin_parser = false
end
end end
end end

View File

@ -117,9 +117,7 @@ describe ValidatesTimeliness::Validator do
let(:person) { PersonWithFormatOption.new } let(:person) { PersonWithFormatOption.new }
before(:all) do with_config(:use_plugin_parser, true)
ValidatesTimeliness.use_plugin_parser = true
end
it "should be valid when value matches format" do it "should be valid when value matches format" do
person.birth_date = '11-12-1913' person.birth_date = '11-12-1913'
@ -132,10 +130,6 @@ describe ValidatesTimeliness::Validator do
person.valid? person.valid?
person.errors[:birth_date].should include('is not a valid date') person.errors[:birth_date].should include('is not a valid date')
end end
after(:all) do
ValidatesTimeliness.use_plugin_parser = false
end
end end
describe "restriction value errors" do describe "restriction value errors" do
@ -146,19 +140,17 @@ describe ValidatesTimeliness::Validator do
end end
it "should be added when ignore_restriction_errors is false" do it "should be added when ignore_restriction_errors is false" do
ValidatesTimeliness.ignore_restriction_errors = false with_config(:ignore_restriction_errors, false) do
person.valid? person.valid?
person.errors[:birth_date].first.should match("Error occurred validating birth_date for :is_at restriction") person.errors[:birth_date].first.should match("Error occurred validating birth_date for :is_at restriction")
end
end end
it "should not be added when ignore_restriction_errors is true" do it "should not be added when ignore_restriction_errors is true" do
ValidatesTimeliness.ignore_restriction_errors = true with_config(:ignore_restriction_errors, true) do
person.valid? person.valid?
person.errors[:birth_date].should be_empty person.errors[:birth_date].should be_empty
end end
after :all do
ValidatesTimeliness.ignore_restriction_errors = false
end end
end end