2010-12-13 17:28:59 -05:00
|
|
|
require 'spec_helper'
|
2009-01-15 18:04:42 -05:00
|
|
|
|
2011-05-06 07:51:12 -04:00
|
|
|
describe Shoulda::Matchers::ActiveModel::AllowValueMatcher do
|
2009-01-15 18:04:42 -05:00
|
|
|
|
|
|
|
context "an attribute with a format validation" do
|
2010-12-13 17:28:59 -05:00
|
|
|
before do
|
2009-01-24 16:23:11 -05:00
|
|
|
define_model :example, :attr => :string do
|
2009-01-15 18:04:42 -05:00
|
|
|
validates_format_of :attr, :with => /abc/
|
|
|
|
end
|
|
|
|
@model = Example.new
|
|
|
|
end
|
|
|
|
|
2010-12-13 17:28:59 -05:00
|
|
|
it "should allow a good value" do
|
|
|
|
@model.should allow_value("abcde").for(:attr)
|
2009-01-15 18:04:42 -05:00
|
|
|
end
|
2010-12-13 17:28:59 -05:00
|
|
|
|
|
|
|
it "should not allow a bad value" do
|
|
|
|
@model.should_not allow_value("xyz").for(:attr)
|
2009-01-15 18:04:42 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "an attribute with a format validation and a custom message" do
|
2010-12-13 17:28:59 -05:00
|
|
|
before do
|
2009-01-24 16:23:11 -05:00
|
|
|
define_model :example, :attr => :string do
|
2009-01-15 18:04:42 -05:00
|
|
|
validates_format_of :attr, :with => /abc/, :message => 'bad value'
|
|
|
|
end
|
|
|
|
@model = Example.new
|
|
|
|
end
|
|
|
|
|
2010-12-13 17:28:59 -05:00
|
|
|
it "should allow a good value" do
|
|
|
|
@model.should allow_value('abcde').for(:attr).with_message(/bad/)
|
2009-01-15 18:04:42 -05:00
|
|
|
end
|
2010-12-13 17:28:59 -05:00
|
|
|
|
|
|
|
it "should not allow a bad value" do
|
|
|
|
@model.should_not allow_value('xyz').for(:attr).with_message(/bad/)
|
2009-01-15 18:04:42 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-15 09:14:06 -04:00
|
|
|
context "an attribute with several validations" do
|
2010-12-13 17:28:59 -05:00
|
|
|
before do
|
2009-05-15 09:14:06 -04:00
|
|
|
define_model :example, :attr => :string do
|
|
|
|
validates_presence_of :attr
|
|
|
|
validates_length_of :attr, :within => 1..5
|
|
|
|
validates_numericality_of :attr, :greater_than_or_equal_to => 1,
|
|
|
|
:less_than_or_equal_to => 50000
|
|
|
|
end
|
|
|
|
@model = Example.new
|
|
|
|
end
|
|
|
|
|
2010-12-13 17:28:59 -05:00
|
|
|
it "should allow a good value" do
|
|
|
|
@model.should allow_value("12345").for(:attr)
|
2009-05-15 09:14:06 -04:00
|
|
|
end
|
2010-12-13 17:28:59 -05:00
|
|
|
|
2009-05-15 09:14:06 -04:00
|
|
|
bad_values = [nil, "", "abc", "0", "50001", "123456"]
|
|
|
|
bad_values.each do |value|
|
2010-12-13 17:28:59 -05:00
|
|
|
it "should not allow a bad value (#{value.inspect})" do
|
|
|
|
@model.should_not allow_value(value).for(:attr)
|
2009-05-15 09:14:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-15 18:04:42 -05:00
|
|
|
end
|