2014-10-27 00:51:42 +00:00
|
|
|
require 'unit_spec_helper'
|
2012-04-11 18:33:21 +00:00
|
|
|
|
2014-12-24 21:37:04 +00:00
|
|
|
describe Shoulda::Matchers::ActiveRecord::AcceptNestedAttributesForMatcher, type: :model do
|
2012-12-20 05:04:27 +00:00
|
|
|
it 'accepts an existing declaration' do
|
2014-01-17 22:01:43 +00:00
|
|
|
expect(accepting_children).to 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
|
|
|
|
2014-01-17 22:01:43 +00:00
|
|
|
expect(matcher.matches?(rejecting_children)).to eq false
|
2012-04-11 18:33:21 +00:00
|
|
|
|
2014-01-17 22:01:43 +00:00
|
|
|
expect(matcher.failure_message).
|
|
|
|
to 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
|
2014-01-17 20:20:44 +00:00
|
|
|
matching = accepting_children(allow_destroy: true)
|
2012-12-20 05:04:27 +00:00
|
|
|
|
2014-01-17 22:01:43 +00:00
|
|
|
expect(matching).to 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
|
2014-01-17 20:20:44 +00:00
|
|
|
matching = accepting_children(allow_destroy: false)
|
2012-12-20 05:04:27 +00:00
|
|
|
|
2014-01-17 22:01:43 +00:00
|
|
|
expect(matching).to 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
|
2014-01-17 20:20:44 +00:00
|
|
|
matching = accepting_children(allow_destroy: true)
|
2012-12-20 05:04:27 +00:00
|
|
|
|
2014-01-17 22:01:43 +00:00
|
|
|
expect(matcher.allow_destroy(false).matches?(matching)).to eq false
|
|
|
|
expect(matcher.failure_message).to match(/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
|
2014-01-17 20:20:44 +00:00
|
|
|
matching = accepting_children(allow_destroy: false)
|
2012-12-20 05:04:27 +00:00
|
|
|
|
2014-01-17 22:01:43 +00:00
|
|
|
expect(matcher.allow_destroy(true).matches?(matching)).to eq false
|
|
|
|
expect(matcher.failure_message).to match(/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
|
2014-01-17 22:01:43 +00:00
|
|
|
expect(accepting_children(limit: 3)).to 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
|
2014-01-17 20:20:44 +00:00
|
|
|
rejecting = accepting_children(limit: 3)
|
2012-12-20 05:04:27 +00:00
|
|
|
|
2014-01-17 22:01:43 +00:00
|
|
|
expect(matcher.limit(2).matches?(rejecting)).to eq false
|
|
|
|
expect(matcher.failure_message).to match(/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
|
2014-01-17 22:01:43 +00:00
|
|
|
expect(accepting_children(update_only: true)).
|
|
|
|
to 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
|
2014-01-17 22:01:43 +00:00
|
|
|
expect(accepting_children(update_only: false)).
|
|
|
|
to 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)
|
2014-01-17 20:20:44 +00:00
|
|
|
rejecting = accepting_children(update_only: true)
|
2012-12-20 05:04:27 +00:00
|
|
|
|
2014-01-17 22:01:43 +00:00
|
|
|
expect(matcher.matches?(rejecting)).to eq false
|
|
|
|
expect(matcher.failure_message).to match(/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)
|
2014-01-17 20:20:44 +00:00
|
|
|
rejecting = accepting_children(update_only: false)
|
2012-12-20 05:04:27 +00:00
|
|
|
|
2014-01-17 22:01:43 +00:00
|
|
|
expect(matcher.matches?(rejecting)).to eq false
|
|
|
|
expect(matcher.failure_message).to match(/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 = {})
|
2014-01-17 20:20:44 +00:00
|
|
|
define_model :child, parent_id: :integer
|
2012-12-20 05:04:27 +00:00
|
|
|
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
|
2014-01-17 20:20:44 +00:00
|
|
|
define_model :child, parent_id: :integer
|
2012-12-20 05:04:27 +00:00
|
|
|
define_model :parent do
|
|
|
|
has_many :children
|
|
|
|
end.new
|
|
|
|
end
|
2012-04-11 18:33:21 +00:00
|
|
|
end
|