2010-12-13 22:28:59 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2011-05-06 11:51:12 +00:00
|
|
|
describe Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher do
|
2012-10-16 17:45:06 +00:00
|
|
|
it "should state in its description that it allows only numeric values" do
|
|
|
|
matcher = Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher.new(:attr)
|
|
|
|
matcher.description.should == "only allow numeric values for attr"
|
|
|
|
end
|
2010-12-13 22:28:59 +00:00
|
|
|
|
2012-10-16 17:45:06 +00:00
|
|
|
context "given a numeric attribute" do
|
2010-12-13 22:28:59 +00:00
|
|
|
before do
|
|
|
|
define_model :example, :attr => :string do
|
|
|
|
validates_numericality_of :attr
|
|
|
|
end
|
|
|
|
@model = Example.new
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should only allow numeric values for that attribute" do
|
2012-10-16 17:45:06 +00:00
|
|
|
matcher = new_matcher(:attr)
|
|
|
|
matcher.matches?(@model).should be_true
|
2010-12-13 22:28:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not override the default message with a blank" do
|
2012-10-16 17:45:06 +00:00
|
|
|
matcher = new_matcher(:attr)
|
|
|
|
matcher.with_message(nil)
|
|
|
|
matcher.matches?(@model).should be_true
|
|
|
|
end
|
|
|
|
|
2012-10-23 16:30:18 +00:00
|
|
|
context "when asked to enforce integer values for that attribute" do
|
|
|
|
it "should not match" do
|
|
|
|
matcher = new_matcher(:attr)
|
|
|
|
matcher.only_integer
|
|
|
|
matcher.matches?(@model).should be_false
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should fail with the ActiveRecord :not_an_integer message" do
|
|
|
|
matcher = new_matcher(:attr)
|
|
|
|
matcher.only_integer
|
|
|
|
matcher.matches?(@model)
|
|
|
|
matcher.failure_message.should include 'Expected errors to include "must be an integer"'
|
|
|
|
end
|
2010-12-13 22:28:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-16 17:45:06 +00:00
|
|
|
context "given a numeric attribute which must be integer" do
|
2011-10-27 02:21:06 +00:00
|
|
|
before do
|
|
|
|
define_model :example, :attr => :string do
|
2012-10-16 17:45:06 +00:00
|
|
|
validates_numericality_of :attr, { :only_integer => true }
|
2011-10-27 02:21:06 +00:00
|
|
|
end
|
|
|
|
@model = Example.new
|
|
|
|
end
|
|
|
|
|
2012-10-16 17:45:06 +00:00
|
|
|
it "allows integer values for that attribute" do
|
|
|
|
matcher = new_matcher(:attr)
|
|
|
|
matcher.only_integer
|
|
|
|
matcher.matches?(@model).should be_true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not allow non-integer values for that attribute" do
|
|
|
|
matcher = new_matcher(:attr)
|
|
|
|
matcher.only_integer
|
|
|
|
matcher.matches?(@model).should be_true
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should state in its description that it allows only integer values" do
|
|
|
|
matcher = new_matcher(:attr)
|
|
|
|
matcher.only_integer
|
|
|
|
matcher.description.should == "only allow numeric, integer values for attr"
|
2011-10-27 02:21:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-16 17:45:06 +00:00
|
|
|
context "given a numeric attribute with a custom validation message" do
|
2010-12-13 22:28:59 +00:00
|
|
|
before do
|
|
|
|
define_model :example, :attr => :string do
|
|
|
|
validates_numericality_of :attr, :message => 'custom'
|
|
|
|
end
|
|
|
|
@model = Example.new
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should only allow numeric values for that attribute with that message" do
|
2012-10-16 17:45:06 +00:00
|
|
|
matcher = new_matcher(:attr)
|
|
|
|
matcher.with_message(/custom/)
|
|
|
|
matcher.matches?(@model).should be_true
|
2010-12-13 22:28:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not allow numeric values for that attribute with another message" do
|
2012-10-16 17:45:06 +00:00
|
|
|
matcher = new_matcher(:attr)
|
|
|
|
matcher.with_message(/wrong/)
|
|
|
|
matcher.matches?(@model).should be_false
|
2010-12-13 22:28:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-16 17:45:06 +00:00
|
|
|
context "given a non-numeric attribute" do
|
2010-12-13 22:28:59 +00:00
|
|
|
before do
|
|
|
|
@model = define_model(:example, :attr => :string).new
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not only allow numeric values for that attribute" do
|
2012-10-16 17:45:06 +00:00
|
|
|
matcher = new_matcher(:attr)
|
|
|
|
matcher.matches?(@model).should be_false
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should fail with the ActiveRecord :not_a_number message" do
|
|
|
|
matcher = new_matcher(:attr)
|
|
|
|
matcher.matches?(@model)
|
|
|
|
matcher.failure_message.should include 'Expected errors to include "is not a number"'
|
2010-12-13 22:28:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-16 17:45:06 +00:00
|
|
|
def new_matcher(attr)
|
|
|
|
Shoulda::Matchers::ActiveModel::ValidateNumericalityOfMatcher.new(attr)
|
|
|
|
end
|
2010-12-13 22:28:59 +00:00
|
|
|
end
|