Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add validate_integer_matcher with specs

  • Loading branch information...
commit d4f34113cd67bea3536561c85e63c2c5cfddfbb5 1 parent ab70057
Joseph HALTER authored
2  lib/rspec_sequel/association.rb
View
@@ -4,7 +4,7 @@ class Association < Base
def description
desc = "have a #{association_type} association #{@attribute.inspect}"
- desc << " with #{hash_to_nice_string @options}" unless @options.empty?
+ desc << " with option(s) #{hash_to_nice_string @options}" unless @options.empty?
desc
end
21 lib/rspec_sequel/matchers/validate_integer.rb
View
@@ -0,0 +1,21 @@
+module RspecSequel
+ module Matchers
+
+ class ValidateIntegerMatcher < RspecSequel::Validation
+ def description
+ desc = "validate that #{@attribute.inspect} is a valid Integer"
+ desc << " with option(s) #{hash_to_nice_string @options}" unless @options.empty?
+ desc
+ end
+
+ def validation_type
+ :validates_integer
+ end
+ end
+
+ def validate_integer(*args)
+ ValidateIntegerMatcher.new(*args)
+ end
+
+ end
+end
2  spec/have_many_to_many_matcher_spec.rb
View
@@ -27,7 +27,7 @@
describe "with options" do
it "should contain a description" do
@matcher = have_many_to_many :items, :class_name => "Item"
- @matcher.description.should == 'have a many_to_many association :items with :class_name => "Item"'
+ @matcher.description.should == 'have a many_to_many association :items with option(s) :class_name => "Item"'
end
it "should set failure messages" do
@matcher = have_many_to_many :items, :class_name => "Item"
2  spec/have_many_to_one_matcher_spec.rb
View
@@ -27,7 +27,7 @@
describe "with options" do
it "should contain a description" do
@matcher = have_many_to_one :item, :class_name => "Item"
- @matcher.description.should == 'have a many_to_one association :item with :class_name => "Item"'
+ @matcher.description.should == 'have a many_to_one association :item with option(s) :class_name => "Item"'
end
it "should set failure messages" do
@matcher = have_many_to_one :item, :class_name => "Item"
2  spec/have_one_to_many_matcher_spec.rb
View
@@ -27,7 +27,7 @@
describe "with options" do
it "should contain a description" do
@matcher = have_one_to_many :comments, :class_name => "Comment"
- @matcher.description.should == 'have a one_to_many association :comments with :class_name => "Comment"'
+ @matcher.description.should == 'have a one_to_many association :comments with option(s) :class_name => "Comment"'
end
it "should set failure messages" do
@matcher = have_one_to_many :comments, :class_name => "Comment"
77 spec/validate_integer_matcher_spec.rb
View
@@ -0,0 +1,77 @@
+require File.dirname(__FILE__) + "/spec_helper"
+
+describe "validate_integer_matcher" do
+
+ before :all do
+ define_model :item do
+ plugin :validation_helpers
+ def validate
+ validates_integer [:id, :name], :allow_nil => true
+ end
+ end
+ end
+
+ subject{ Item }
+
+ describe "arguments" do
+ it "should require attribute" do
+ lambda{
+ @matcher = validate_integer
+ }.should raise_error(ArgumentError)
+ end
+ it "should refuse additionnal parameters" do
+ lambda{
+ @matcher = validate_integer :name, :id
+ }.should raise_error(ArgumentError)
+ end
+ end
+
+ describe "messages" do
+ describe "without option" do
+ it "should contain a description" do
+ @matcher = validate_integer :name
+ @matcher.description.should == "validate that :name is a valid Integer"
+ end
+ it "should set failure messages" do
+ @matcher = validate_integer :name
+ @matcher.matches? subject
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
+ end
+ end
+ describe "with options" do
+ it "should contain a description" do
+ @matcher = validate_integer :name, :allow_nil => true
+ @matcher.description.should == "validate that :name is a valid Integer with option(s) :allow_nil => true"
+ end
+ it "should set failure messages" do
+ @matcher = validate_integer :price, :allow_nil => true
+ @matcher.matches? subject
+ @matcher.failure_message.should == "expected Item to " + @matcher.description
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description
+ end
+ it "should explicit used options if different than expected" do
+ @matcher = validate_integer :name, :allow_blank => true
+ @matcher.matches? subject
+ explanation = " but called with option(s) :allow_nil => true instead"
+ @matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
+ end
+ it "should warn if invalid options are used" do
+ @matcher = validate_integer :name, :allow_anything => true
+ @matcher.matches? subject
+ explanation = " but option :allow_anything is not valid"
+ @matcher.failure_message.should == "expected Item to " + @matcher.description + explanation
+ @matcher.negative_failure_message.should == "expected Item to not " + @matcher.description + explanation
+ end
+ end
+ end
+
+ describe "matchers" do
+ it{ should validate_integer(:name) }
+ it{ should validate_integer(:name, :allow_nil => true) }
+ it{ should_not validate_integer(:price) }
+ it{ should_not validate_integer(:name, :allow_blank => true) }
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.