thoughtbot--shoulda-matchers/spec/shoulda/matchers/active_record/accept_nested_attributes_fo...

108 lines
3.1 KiB
Ruby
Raw Normal View History

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)
end
2012-12-20 05:04:27 +00:00
it 'rejects a missing declaration' do
matcher = children_matcher
2012-12-20 05:04:27 +00:00
matcher.matches?(rejecting_children).should be_false
matcher.failure_message.
should eq 'Expected Parent to accept nested attributes for children (is not declared)'
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)
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)
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
matcher.failure_message.should =~ /should not allow destroy/
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
matcher.failure_message.should =~ /should allow destroy/
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)
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
matcher.failure_message.should =~ /limit should be 2, got 3/
end
end
2012-12-20 05:04:27 +00:00
context 'update_only' do
it 'accepts a valid truthy value' do
accepting_children(:update_only => true).
should children_matcher.update_only(true)
end
2012-12-20 05:04:27 +00:00
it 'accepts a valid falsey value' do
accepting_children(:update_only => false).
should children_matcher.update_only(false)
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
matcher.failure_message.should =~ /should not be update only/
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
matcher.failure_message.should =~ /should be update only/
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
end