Skip to content

Commit

Permalink
Upgrade to RSpec 3.
Browse files Browse the repository at this point in the history
  • Loading branch information
johncarney committed Jul 16, 2014
1 parent 111d9b1 commit 890381b
Show file tree
Hide file tree
Showing 14 changed files with 212 additions and 175 deletions.
2 changes: 1 addition & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
--format nested
--format documentation
--color
7 changes: 4 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ source 'http://rubygems.org'
gemspec

gem 'rails', '~> 3.2.6'
gem 'rspec', '~> 2.8'
gem 'rspec-rails', '~> 2.8'
gem 'rspec', '~> 3.0'
gem 'rspec-rails', '~> 3.0'
gem 'rspec-collection_matchers'
gem 'timecop'
gem 'rspec_tag_matchers'
gem 'ruby-debug', :platforms => [:ruby_18, :jruby]
gem 'debugger', :platforms => [:ruby_19]
gem 'appraisal'
gem 'sqlite3'
gem 'nokogiri'

group :mongoid do
gem 'mongoid', '~> 2.3.0'
Expand Down
5 changes: 3 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
require 'rspec'
require 'rspec/collection_matchers'

require 'active_model'
require 'active_model/validations'
require 'active_record'
require 'action_view'
require 'timecop'
require 'rspec_tag_matchers'

require 'validates_timeliness'

require 'support/test_model'
require 'support/model_helpers'
require 'support/config_helper'
require 'support/tag_matcher'

ValidatesTimeliness.setup do |c|
c.extend_orms = [ :active_record ]
Expand Down Expand Up @@ -85,9 +86,9 @@ def birth_date=(value)

RSpec.configure do |c|
c.mock_with :rspec
c.include(RspecTagMatchers)
c.include(ModelHelpers)
c.include(ConfigHelper)
c.include(TagMatcher)
c.before do
reset_validation_setup_for(Person)
reset_validation_setup_for(PersonWithShim)
Expand Down
8 changes: 4 additions & 4 deletions spec/support/model_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
35 changes: 35 additions & 0 deletions spec/support/tag_matcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'nokogiri'

module TagMatcher
extend RSpec::Matchers::DSL

matcher :have_tag do |selector|
match do |subject|
matches = doc(subject).search(selector)

if @inner_text
matches = matches.select { |element| element.inner_text == @inner_text }
end

matches.any?
end

chain :with_inner_text do |inner_text|
@inner_text = inner_text
end

private

def body(subject)
if subject.respond_to?(:body)
subject.body
else
subject.to_s
end
end

def doc(subject)
@doc ||= Nokogiri::HTML(body(subject))
end
end
end
20 changes: 10 additions & 10 deletions spec/validates_timeliness/attribute_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

describe ValidatesTimeliness::AttributeMethods do
it 'should define _timeliness_raw_value_for instance method' do
PersonWithShim.new.should respond_to(:_timeliness_raw_value_for)
expect(PersonWithShim.new).to respond_to(:_timeliness_raw_value_for)
end

describe ".timeliness_validated_attributes" do
Expand All @@ -12,10 +12,10 @@
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

context "attribute write method" do
class PersonWithCache
include TestModel
Expand All @@ -31,13 +31,13 @@ class PersonWithCache
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._timeliness_raw_value_for('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
Expand All @@ -48,11 +48,11 @@ class PersonWithCache

write_method = RUBY_VERSION < '1.9' ? 'birth_date=' : :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.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
Expand All @@ -70,7 +70,7 @@ class PersonWithParser
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
Expand All @@ -80,7 +80,7 @@ class PersonWithParser

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
Loading

0 comments on commit 890381b

Please sign in to comment.