Skip to content
This repository has been archived by the owner on Oct 22, 2019. It is now read-only.

Commit

Permalink
fixed gem to load files properly
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew committed Dec 1, 2009
1 parent 845a4b8 commit 2d92852
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 67 deletions.
6 changes: 1 addition & 5 deletions init.rb
@@ -1,5 +1 @@
require 'spec/rails/matchers/associations'

require 'rspec_validation_expectations'
require 'rspec_association_expectations'
require 'rspec_other_expectations'
require 'rspec_validation_expectations'
62 changes: 4 additions & 58 deletions lib/rspec_validation_expectations.rb
@@ -1,59 +1,5 @@
def it_should_validate_presence_of(*one_or_more_fields)
model_name = described_type
one_or_more_fields.each do |field|
it "should validate presence of #{field.to_s.humanize.downcase}" do
validations = model_name.reflect_on_all_validations
validations = validations.select { |e| e.macro == :validates_presence_of }
field_names = validations.collect(&:name)
field_names.should include(field)
end
end
end
require 'spec/rails/matchers/associations'

def it_should_validate_numericality_of(*one_or_more_fields)
model_name = described_type
one_or_more_fields.each do |field|
it "should validate numericality of #{field.to_s.humanize.downcase}" do
validations = model_name.reflect_on_all_validations
validations = validations.select { |e| e.macro == :validates_numericality_of }
field_names = validations.collect(&:name)
field_names.should include(field)
end
end
end

def it_should_validate_uniqueness_of(*one_or_more_fields)
options = one_or_more_fields.last.is_a?(Hash) ? one_or_more_fields.pop : {}
model_name = described_type
one_or_more_fields.each do |field|
it "should validate uniqueness of #{field.to_s.humanize.downcase}" do
validations = model_name.reflect_on_all_validations
validations = validations.select { |e| e.macro == :validates_uniqueness_of }
validations.collect(&:name).should include(field)
validations.collect(&:options).should include(options)
end
end
end

def it_should_validate_inclusion_of(field_name, *args)
model_name = described_type
options = args.last.is_a?(Hash) ? args.pop : {}

it "should validate inclusion of #{field_name} as one of #{options[:in].to_sentence(:connector => 'or', :skip_last_comma => true)}" do
validations = model_name.reflect_on_all_validations
validation = validations.detect {|v| v.macro == :validates_inclusion_of && v.name == field_name}

unless validation.nil?
validation.options[:in].sort.should == options[:in].sort
end
end
end

def it_should_be_createable *args
model_name = described_type
attributes = args.last.is_a?(Hash) ? args.last[:with] : {}

it "should be creatable" do
lambda {model_name.create(attributes)}.should change(model_name, :count).by(1)
end
end
require 'rspec_validation_expectations/validation_expectations'
require 'rspec_validation_expectations/association_expectations'
require 'rspec_validation_expectations/other_expectations'
59 changes: 59 additions & 0 deletions lib/rspec_validation_expectations/validation_expectations.rb
@@ -0,0 +1,59 @@
def it_should_validate_presence_of(*one_or_more_fields)
model_name = described_type
one_or_more_fields.each do |field|
it "should validate presence of #{field.to_s.humanize.downcase}" do
validations = model_name.reflect_on_all_validations
validations = validations.select { |e| e.macro == :validates_presence_of }
field_names = validations.collect(&:name)
field_names.should include(field)
end
end
end

def it_should_validate_numericality_of(*one_or_more_fields)
model_name = described_type
one_or_more_fields.each do |field|
it "should validate numericality of #{field.to_s.humanize.downcase}" do
validations = model_name.reflect_on_all_validations
validations = validations.select { |e| e.macro == :validates_numericality_of }
field_names = validations.collect(&:name)
field_names.should include(field)
end
end
end

def it_should_validate_uniqueness_of(*one_or_more_fields)
options = one_or_more_fields.last.is_a?(Hash) ? one_or_more_fields.pop : {}
model_name = described_type
one_or_more_fields.each do |field|
it "should validate uniqueness of #{field.to_s.humanize.downcase}" do
validations = model_name.reflect_on_all_validations
validations = validations.select { |e| e.macro == :validates_uniqueness_of }
validations.collect(&:name).should include(field)
validations.collect(&:options).should include(options)
end
end
end

def it_should_validate_inclusion_of(field_name, *args)
model_name = described_type
options = args.last.is_a?(Hash) ? args.pop : {}

it "should validate inclusion of #{field_name} as one of #{options[:in].to_sentence(:connector => 'or', :skip_last_comma => true)}" do
validations = model_name.reflect_on_all_validations
validation = validations.detect {|v| v.macro == :validates_inclusion_of && v.name == field_name}

unless validation.nil?
validation.options[:in].sort.should == options[:in].sort
end
end
end

def it_should_be_createable *args
model_name = described_type
attributes = args.last.is_a?(Hash) ? args.last[:with] : {}

it "should be creatable" do
lambda {model_name.create(attributes)}.should change(model_name, :count).by(1)
end
end
15 changes: 11 additions & 4 deletions rspec_validation_expectations.gemspec
Expand Up @@ -11,10 +11,17 @@ Gem::Specification.new do |s|
s.files = [
"CHANGELOG",
"MIT-LICENSE",
"README",
"rspec_validation_expectations.gemspec",
"lib/rspec_validation_expectations.rb"
]
"README",
"Rakefile",
"init.rb",
"lib/rspec_validation_expectations.rb",
"lib/rspec_validation_expectations/validation_expectations.rb",
"lib/rspec_validation_expectations/association_expectations.rb",
"lib/rspec_validation_expectations/other_expectations.rb",
"lib/spec/rails/matchers/associations.rb",
"rspec_validation_expectations.gemspec",
"test/test_helper.rb"
]
s.rdoc_options = ["--main", "README"]
s.extra_rdoc_files = ["README"]
end

0 comments on commit 2d92852

Please sign in to comment.