Permalink
Browse files

change spec config setup to use class method with_config

  • Loading branch information...
1 parent 3793ef2 commit 38457ec3341423350f94d7a458dfad94374745e8 @adzap adzap committed Jan 29, 2011
View
16 spec/support/config_helper.rb
@@ -1,4 +1,6 @@
module ConfigHelper
+ extend ActiveSupport::Concern
+
# Justin French tip
def with_config(preference_name, temporary_value)
old_value = ValidatesTimeliness.send(preference_name)
@@ -7,4 +9,18 @@ def with_config(preference_name, temporary_value)
ensure
ValidatesTimeliness.send(:"#{preference_name}=", old_value)
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
View
10 spec/validates_timeliness/attribute_methods_spec.rb
@@ -41,6 +41,8 @@ class PersonWithCache
end
context "with plugin parser" do
+ with_config(:use_plugin_parser, true)
+
class PersonWithParser
include TestModel
include TestModelShim
@@ -52,10 +54,6 @@ class PersonWithParser
validates_datetime :birth_datetime
end
- before :all do
- ValidatesTimeliness.use_plugin_parser = true
- end
-
it 'should parse a string value' do
Timeliness::Parser.should_receive(:parse)
r = PersonWithParser.new
@@ -67,10 +65,6 @@ class PersonWithParser
r.birth_datetime = '2010-01-01 12:00'
r.birth_datetime.zone == Time.zone.name
end
-
- after :all do
- ValidatesTimeliness.use_plugin_parser = false
- end
end
end
View
21 spec/validates_timeliness/conversion_spec.rb
@@ -114,17 +114,10 @@
end
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
- 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
+ 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)
+ end
end
end
end
@@ -209,9 +202,7 @@
describe "#parse" do
context "use_plugin_parser setting is true" do
- around do |example|
- with_config(:use_plugin_parser, true, &example)
- end
+ with_config(:use_plugin_parser, true)
it 'should use timeliness' do
Timeliness::Parser.should_receive(:parse)
@@ -220,9 +211,7 @@
end
context "use_plugin_parser setting is false" do
- around do |example|
- with_config(:use_plugin_parser, false, &example)
- end
+ with_config(:use_plugin_parser, false)
it 'should use Time.zone.parse attribute is timezone aware' do
@timezone_aware = true
View
8 spec/validates_timeliness/extensions/date_time_select_spec.rb
@@ -4,9 +4,7 @@
include ActionView::Helpers::DateHelper
attr_reader :person, :params
- before :all do
- ValidatesTimeliness.use_plugin_parser = true
- end
+ with_config(:use_plugin_parser, true)
before do
@person = Person.new
@@ -177,8 +175,4 @@
output.should_not have_tag('select[id=person_birth_time_6i] option[selected=selected]')
end
end
-
- after :all do
- ValidatesTimeliness.use_plugin_parser = false
- end
end
View
11 spec/validates_timeliness/orm/active_record_spec.rb
@@ -33,16 +33,14 @@ class EmployeeWithCache < ActiveRecord::Base
end
context "with plugin parser" do
+ with_config(:use_plugin_parser, true)
+
class EmployeeWithParser < ActiveRecord::Base
set_table_name 'employees'
validates_date :birth_date
validates_datetime :birth_datetime
end
- before :all do
- ValidatesTimeliness.use_plugin_parser = true
- end
-
it 'should parse a string value' do
Timeliness::Parser.should_receive(:parse)
r = EmployeeWithParser.new
@@ -54,11 +52,6 @@ class EmployeeWithParser < ActiveRecord::Base
r.birth_datetime = '2010-06-01 12:00'
r.birth_datetime.utc_offset.should == 10.hours
end
-
- after :all do
- Time.zone = 'Australia/Melbourne'
- ValidatesTimeliness.use_plugin_parser = false
- end
end
end
View
32 spec/validates_timeliness/orm/mongoid_spec.rb
@@ -12,20 +12,20 @@
config.persist_in_safe_mode = false
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
+ 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
it 'should be defined on the class' do
Article.should respond_to(:validates_date)
@@ -52,9 +52,7 @@ class Article
end
context "with plugin parser" do
- before :all do
- ValidatesTimeliness.use_plugin_parser = true
- end
+ with_config(:use_plugin_parser, false)
it 'should parse a string value' do
Timeliness::Parser.should_receive(:parse)
@@ -67,10 +65,6 @@ class Article
r.publish_datetime = '2010-01-01 12:00'
r.publish_datetime.should == Time.utc(2010,1,1,12,0)
end
-
- after :all do
- ValidatesTimeliness.use_plugin_parser = false
- end
end
end
View
26 spec/validates_timeliness/validator_spec.rb
@@ -117,9 +117,7 @@ class PersonWithFormatOption
let(:person) { PersonWithFormatOption.new }
- before(:all) do
- ValidatesTimeliness.use_plugin_parser = true
- end
+ with_config(:use_plugin_parser, true)
it "should be valid when value matches format" do
person.birth_date = '11-12-1913'
@@ -132,10 +130,6 @@ class PersonWithFormatOption
person.valid?
person.errors[:birth_date].should include('is not a valid date')
end
-
- after(:all) do
- ValidatesTimeliness.use_plugin_parser = false
- end
end
describe "restriction value errors" do
@@ -146,19 +140,17 @@ class PersonWithFormatOption
end
it "should be added when ignore_restriction_errors is false" do
- ValidatesTimeliness.ignore_restriction_errors = false
- person.valid?
- person.errors[:birth_date].first.should match("Error occurred validating birth_date for :is_at restriction")
+ with_config(:ignore_restriction_errors, false) do
+ person.valid?
+ person.errors[:birth_date].first.should match("Error occurred validating birth_date for :is_at restriction")
+ end
end
it "should not be added when ignore_restriction_errors is true" do
- ValidatesTimeliness.ignore_restriction_errors = true
- person.valid?
- person.errors[:birth_date].should be_empty
- end
-
- after :all do
- ValidatesTimeliness.ignore_restriction_errors = false
+ with_config(:ignore_restriction_errors, true) do
+ person.valid?
+ person.errors[:birth_date].should be_empty
+ end
end
end

0 comments on commit 38457ec

Please sign in to comment.