Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add the more complex validate_unique_matcher with specs

  • Loading branch information...
commit a5bbd44a4625eb8206944a4d90e97050e79baca2 1 parent f85bb45
Joseph HALTER authored
View
2  lib/rspec_sequel/base.rb
@@ -3,7 +3,7 @@ module RspecSequel
class Base
def initialize(attribute, options={})
raise ArgumentError, "not enough params for matcher, required attribute is missing" if attribute.nil?
- @attribute = attribute.to_sym
+ @attribute = attribute
@options = options
@description = []
end
View
33 lib/rspec_sequel/matchers/validate_unique.rb
@@ -0,0 +1,33 @@
+module RspecSequel
+ module Matchers
+
+ class ValidateUniqueMatcher < RspecSequel::Validation
+ def description
+ desc = "validate uniqueness of #{@attribute.inspect}"
+ desc << " with option(s) #{hash_to_nice_string @options}" unless @options.empty?
+ desc
+ end
+
+ def validation_type
+ :validates_unique
+ end
+
+ def valid_options
+ [:message]
+ end
+
+ def args_to_called_attributes(args)
+ called_attributes = []
+ until args.empty?
+ called_attributes << args.shift
+ end
+ called_attributes
+ end
+ end
+
+ def validate_unique(*args)
+ ValidateUniqueMatcher.new(*args)
+ end
+
+ end
+end
View
6 lib/rspec_sequel/validation.rb
@@ -46,7 +46,7 @@ def valid?(db, i, c, attribute, options)
i.class.columns # ensure colums are read again after .dup
i.stub!(validation_type).and_return{|*args|
called_options = args.last.is_a?(Hash) ? args.pop : {}
- called_attributes = [args.pop].flatten
+ called_attributes = args_to_called_attributes(args)
called_additionnal = args.shift if additionnal_param_required?
if !args.empty?
@suffix << "but called with too many params"
@@ -67,6 +67,10 @@ def valid?(db, i, c, attribute, options)
end
called_count==1
end
+
+ def args_to_called_attributes(args)
+ [args.pop].flatten
+ end
end
View
79 spec/validate_unique_matcher_spec.rb
@@ -0,0 +1,79 @@
+require File.dirname(__FILE__) + "/spec_helper"
+
+describe "validate_unique_matcher" do
+
+ before :all do
+ define_model :item do
+ plugin :validation_helpers
+ def validate
+ validates_unique [:id, :name], :name, :message => "Hello"
+ end
+ end
+ end
+
+ subject{ Item }
+
+ describe "arguments" do
+ it "should require attribute" do
+ lambda{
+ @matcher = validate_unique
+ }.should raise_error(ArgumentError)
+ end
+ it "should refuse additionnal parameters" do
+ lambda{
+ @matcher = validate_unique :name, :id
+ }.should raise_error(ArgumentError)
+ end
+ end
+
+ describe "messages" do
+ describe "without option" do
+ it "should contain a description" do
+ @matcher = validate_unique :name
+ @matcher.description.should == "validate uniqueness of :name"
+ end
+ it "should set failure messages" do
+ @matcher = validate_unique :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_unique :name, :message => "Hello"
+ @matcher.description.should == 'validate uniqueness of :name with option(s) :message => "Hello"'
+ end
+ it "should set failure messages" do
+ @matcher = validate_unique :price, :message => "Hello"
+ @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_unique :name, :message => "Hello world"
+ @matcher.matches? subject
+ explanation = ' but called with option(s) :message => "Hello" 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_unique :name, :allow_nil => true
+ @matcher.matches? subject
+ explanation = " but option :allow_nil 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_unique(:name) }
+ it{ should validate_unique([:id, :name]) }
+ it{ should validate_unique(:name, :message => "Hello") }
+ it{ should_not validate_unique(:id) }
+ it{ should_not validate_unique(:price) }
+ it{ should_not validate_unique(:name, :allow_nil => true) }
+ end
+
+end
Please sign in to comment.
Something went wrong with that request. Please try again.