2012-04-11 18:33:21 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Shoulda::Matchers::ActiveRecord::AcceptNestedAttributesForMatcher do
|
2012-12-20 05:04:27 +00:00
|
|
|
it 'accepts an existing declaration' do
|
|
|
|
accepting_children.should accept_nested_attributes_for(:children)
|
2012-04-11 18:33:21 +00:00
|
|
|
end
|
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
it 'rejects a missing declaration' do
|
|
|
|
matcher = children_matcher
|
2012-04-11 18:33:21 +00:00
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
matcher.matches?(rejecting_children).should be_false
|
2012-04-11 18:33:21 +00:00
|
|
|
|
2013-12-24 11:24:27 +00:00
|
|
|
matcher.failure_message.
|
2013-08-23 18:44:57 +00:00
|
|
|
should eq 'Expected Parent to accept nested attributes for children (is not declared)'
|
2012-04-11 18:33:21 +00:00
|
|
|
end
|
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
context 'allow_destroy' do
|
|
|
|
it 'accepts a valid truthy value' do
|
|
|
|
matching = accepting_children(:allow_destroy => true)
|
|
|
|
|
|
|
|
matching.should children_matcher.allow_destroy(true)
|
2012-04-11 18:33:21 +00:00
|
|
|
end
|
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
it 'accepts a valid falsey value' do
|
|
|
|
matching = accepting_children(:allow_destroy => false)
|
|
|
|
|
|
|
|
matching.should children_matcher.allow_destroy(false)
|
2012-04-11 18:33:21 +00:00
|
|
|
end
|
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
it 'rejects an invalid truthy value' do
|
|
|
|
matcher = children_matcher
|
|
|
|
matching = accepting_children(:allow_destroy => true)
|
|
|
|
|
|
|
|
matcher.allow_destroy(false).matches?(matching).should be_false
|
2013-12-24 11:24:27 +00:00
|
|
|
matcher.failure_message.should =~ /should not allow destroy/
|
2012-04-11 18:33:21 +00:00
|
|
|
end
|
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
it 'rejects an invalid falsey value' do
|
|
|
|
matcher = children_matcher
|
|
|
|
matching = accepting_children(:allow_destroy => false)
|
|
|
|
|
|
|
|
matcher.allow_destroy(true).matches?(matching).should be_false
|
2013-12-24 11:24:27 +00:00
|
|
|
matcher.failure_message.should =~ /should allow destroy/
|
2012-04-11 18:33:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
context 'limit' do
|
|
|
|
it 'accepts a correct value' do
|
|
|
|
accepting_children(:limit => 3).should children_matcher.limit(3)
|
2012-04-11 18:33:21 +00:00
|
|
|
end
|
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
it 'rejects a false value' do
|
|
|
|
matcher = children_matcher
|
|
|
|
rejecting = accepting_children(:limit => 3)
|
|
|
|
|
|
|
|
matcher.limit(2).matches?(rejecting).should be_false
|
2013-12-24 11:24:27 +00:00
|
|
|
matcher.failure_message.should =~ /limit should be 2, got 3/
|
2012-04-11 18:33:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
context 'update_only' do
|
|
|
|
it 'accepts a valid truthy value' do
|
2013-03-15 06:32:53 +00:00
|
|
|
accepting_children(:update_only => true).
|
|
|
|
should children_matcher.update_only(true)
|
2012-04-11 18:33:21 +00:00
|
|
|
end
|
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
it 'accepts a valid falsey value' do
|
2013-03-15 06:32:53 +00:00
|
|
|
accepting_children(:update_only => false).
|
|
|
|
should children_matcher.update_only(false)
|
2012-04-11 18:33:21 +00:00
|
|
|
end
|
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
it 'rejects an invalid truthy value' do
|
|
|
|
matcher = children_matcher.update_only(false)
|
|
|
|
rejecting = accepting_children(:update_only => true)
|
|
|
|
|
|
|
|
matcher.matches?(rejecting).should be_false
|
2013-12-24 11:24:27 +00:00
|
|
|
matcher.failure_message.should =~ /should not be update only/
|
2012-04-11 18:33:21 +00:00
|
|
|
end
|
|
|
|
|
2012-12-20 05:04:27 +00:00
|
|
|
it 'rejects an invalid falsey value' do
|
|
|
|
matcher = children_matcher.update_only(true)
|
|
|
|
rejecting = accepting_children(:update_only => false)
|
|
|
|
|
|
|
|
matcher.matches?(rejecting).should be_false
|
2013-12-24 11:24:27 +00:00
|
|
|
matcher.failure_message.should =~ /should be update only/
|
2012-04-11 18:33:21 +00:00
|
|
|
end
|
|
|
|
end
|
2012-12-20 05:04:27 +00:00
|
|
|
|
|
|
|
def accepting_children(options = {})
|
|
|
|
define_model :child, :parent_id => :integer
|
|
|
|
define_model :parent do
|
|
|
|
has_many :children
|
|
|
|
accepts_nested_attributes_for :children, options
|
|
|
|
end.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def children_matcher
|
|
|
|
accept_nested_attributes_for(:children)
|
|
|
|
end
|
|
|
|
|
|
|
|
def rejecting_children
|
|
|
|
define_model :child, :parent_id => :integer
|
|
|
|
define_model :parent do
|
|
|
|
has_many :children
|
|
|
|
end.new
|
|
|
|
end
|
2012-04-11 18:33:21 +00:00
|
|
|
end
|