2010-12-13 22:28:59 +00:00
|
|
|
require 'spec_helper'
|
2009-01-21 01:20:30 +00:00
|
|
|
|
2011-05-06 11:51:12 +00:00
|
|
|
describe Shoulda::Matchers::ActiveModel::ValidatePresenceOfMatcher do
|
2009-01-21 01:20:30 +00:00
|
|
|
|
|
|
|
context "a required attribute" do
|
2010-12-13 22:28:59 +00:00
|
|
|
before do
|
2009-01-24 21:23:11 +00:00
|
|
|
define_model :example, :attr => :string do
|
2009-01-21 01:20:30 +00:00
|
|
|
validates_presence_of :attr
|
|
|
|
end
|
|
|
|
@model = Example.new
|
|
|
|
end
|
|
|
|
|
2010-12-13 22:28:59 +00:00
|
|
|
it "should require a value" do
|
|
|
|
@model.should validate_presence_of(:attr)
|
2009-01-21 01:20:30 +00:00
|
|
|
end
|
|
|
|
|
2010-12-13 22:28:59 +00:00
|
|
|
it "should not override the default message with a blank" do
|
|
|
|
@model.should validate_presence_of(:attr).with_message(nil)
|
2009-01-21 01:20:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-01-24 16:19:34 +00:00
|
|
|
context "a required attribute on a class using ActiveModel::Validations" do
|
|
|
|
before do
|
|
|
|
define_active_model_class("Example", :accessors => [:attr]) do
|
|
|
|
validates_presence_of :attr
|
|
|
|
end
|
|
|
|
@model = Example.new
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should require a value" do
|
|
|
|
@model.should validate_presence_of(:attr)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not override the default message with a blank" do
|
|
|
|
@model.should validate_presence_of(:attr).with_message(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-21 01:20:30 +00:00
|
|
|
context "an optional attribute" do
|
2010-12-13 22:28:59 +00:00
|
|
|
before do
|
2009-01-24 21:23:11 +00:00
|
|
|
@model = define_model(:example, :attr => :string).new
|
2009-01-21 01:20:30 +00:00
|
|
|
end
|
|
|
|
|
2010-12-13 22:28:59 +00:00
|
|
|
it "should not require a value" do
|
|
|
|
@model.should_not validate_presence_of(:attr)
|
2009-01-21 01:20:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-01-24 16:19:34 +00:00
|
|
|
context "an optional attribute on a class using ActiveModel::Validations" do
|
|
|
|
before do
|
|
|
|
@model = define_active_model_class("Example", :accessors => [:attr]).new
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not require a value" do
|
|
|
|
@model.should_not validate_presence_of(:attr)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-01-21 01:20:30 +00:00
|
|
|
context "a required has_many association" do
|
2010-12-13 22:28:59 +00:00
|
|
|
before do
|
2009-01-24 21:23:11 +00:00
|
|
|
define_model :child
|
|
|
|
@model = define_model :parent do
|
2009-01-21 01:20:30 +00:00
|
|
|
has_many :children
|
|
|
|
validates_presence_of :children
|
|
|
|
end.new
|
|
|
|
end
|
|
|
|
|
2010-12-13 22:28:59 +00:00
|
|
|
it "should require the attribute to be set" do
|
|
|
|
@model.should validate_presence_of(:children)
|
2009-01-21 01:20:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "an optional has_many association" do
|
2010-12-13 22:28:59 +00:00
|
|
|
before do
|
2009-01-24 21:23:11 +00:00
|
|
|
define_model :child
|
|
|
|
@model = define_model :parent do
|
2009-01-21 01:20:30 +00:00
|
|
|
has_many :children
|
|
|
|
end.new
|
|
|
|
end
|
|
|
|
|
2010-12-13 22:28:59 +00:00
|
|
|
it "should not require the attribute to be set" do
|
|
|
|
@model.should_not validate_presence_of(:children)
|
2009-01-21 01:20:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "a required has_and_belongs_to_many association" do
|
2010-12-13 22:28:59 +00:00
|
|
|
before do
|
2009-01-24 21:23:11 +00:00
|
|
|
define_model :child
|
|
|
|
@model = define_model :parent do
|
2009-01-21 01:20:30 +00:00
|
|
|
has_and_belongs_to_many :children
|
|
|
|
validates_presence_of :children
|
|
|
|
end.new
|
2011-09-12 10:25:31 +00:00
|
|
|
create_table "children_parents", :id => false do |t|
|
|
|
|
t.integer :child_id
|
|
|
|
t.integer :parent_id
|
|
|
|
end
|
2009-01-21 01:20:30 +00:00
|
|
|
end
|
|
|
|
|
2010-12-13 22:28:59 +00:00
|
|
|
it "should require the attribute to be set" do
|
|
|
|
@model.should validate_presence_of(:children)
|
2009-01-21 01:20:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "an optional has_and_belongs_to_many association" do
|
2010-12-13 22:28:59 +00:00
|
|
|
before do
|
2009-01-24 21:23:11 +00:00
|
|
|
define_model :child
|
|
|
|
@model = define_model :parent do
|
2009-01-21 01:20:30 +00:00
|
|
|
has_and_belongs_to_many :children
|
|
|
|
end.new
|
2011-09-12 10:25:31 +00:00
|
|
|
create_table "children_parents", :id => false do |t|
|
|
|
|
t.integer :child_id
|
|
|
|
t.integer :parent_id
|
|
|
|
end
|
2009-01-21 01:20:30 +00:00
|
|
|
end
|
|
|
|
|
2010-12-13 22:28:59 +00:00
|
|
|
it "should not require the attribute to be set" do
|
|
|
|
@model.should_not validate_presence_of(:children)
|
2009-01-21 01:20:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|