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
extend ActiveSupport::Concern
# Justin French tip
def with_config(preference_name, temporary_value)
old_value = ValidatesTimeliness.send(preference_name)
@ -7,4 +9,18 @@ module ConfigHelper
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 File

@ -41,6 +41,8 @@ describe ValidatesTimeliness::AttributeMethods do
end
context "with plugin parser" do
with_config(:use_plugin_parser, true)
class PersonWithParser
include TestModel
include TestModelShim
@ -52,10 +54,6 @@ describe ValidatesTimeliness::AttributeMethods do
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 @@ describe ValidatesTimeliness::AttributeMethods do
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 File

@ -114,17 +114,10 @@ describe ValidatesTimeliness::Conversion do
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
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
after do
ValidatesTimeliness.dummy_date_for_time_type = @original_dummy_date
end
end
end
@ -209,9 +202,7 @@ describe ValidatesTimeliness::Conversion do
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 @@ describe ValidatesTimeliness::Conversion do
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 File

@ -4,9 +4,7 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do
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 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do
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 File

@ -33,16 +33,14 @@ describe ValidatesTimeliness, 'ActiveRecord' do
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 @@ describe ValidatesTimeliness, 'ActiveRecord' do
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 File

@ -12,7 +12,9 @@ Mongoid.configure do |config|
config.persist_in_safe_mode = false
end
class Article
describe ValidatesTimeliness, 'Mongoid' do
class Article
::ValidatesTimeliness.use_plugin_parser = true
include Mongoid::Document
field :publish_date, :type => Date
@ -22,9 +24,7 @@ class Article
validates_time :publish_time, :allow_nil => true
validates_datetime :publish_datetime, :allow_nil => true
::ValidatesTimeliness.use_plugin_parser = false
end
describe ValidatesTimeliness, 'Mongoid' do
end
context "validation methods" do
it 'should be defined on the class' do
@ -52,9 +52,7 @@ describe ValidatesTimeliness, 'Mongoid' do
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 @@ describe ValidatesTimeliness, 'Mongoid' do
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 File

@ -117,9 +117,7 @@ describe ValidatesTimeliness::Validator do
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 @@ describe ValidatesTimeliness::Validator do
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 @@ describe ValidatesTimeliness::Validator do
end
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.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
with_config(:ignore_restriction_errors, true) do
person.valid?
person.errors[:birth_date].should be_empty
end
after :all do
ValidatesTimeliness.ignore_restriction_errors = false
end
end