1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00
thoughtbot--shoulda-matchers/spec/shoulda/active_model/allow_value_matcher_spec.rb

63 lines
1.7 KiB
Ruby
Raw Normal View History

require 'spec_helper'
2009-01-15 18:04:42 -05:00
describe Shoulda::Matchers::ActiveModel::AllowValueMatcher do
2009-01-15 18:04:42 -05:00
context "an attribute with a format validation" do
before do
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
it "should allow a good value" do
@model.should allow_value("abcde").for(:attr)
2009-01-15 18:04:42 -05:00
end
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
before do
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
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
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
context "an attribute with several validations" do
before do
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
it "should allow a good value" do
@model.should allow_value("12345").for(:attr)
end
bad_values = [nil, "", "abc", "0", "50001", "123456"]
bad_values.each do |value|
it "should not allow a bad value (#{value.inspect})" do
@model.should_not allow_value(value).for(:attr)
end
end
end
2009-01-15 18:04:42 -05:00
end