-
-
Notifications
You must be signed in to change notification settings - Fork 912
/
accept_nested_attributes_for_matcher_spec.rb
84 lines (70 loc) · 2.98 KB
/
accept_nested_attributes_for_matcher_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require 'spec_helper'
describe Shoulda::Matchers::ActiveRecord::AcceptNestedAttributesForMatcher do
before do
define_model :child, :parent_id => :integer
define_model :parent do
has_many :children
end
end
let(:parent) { Parent.new }
let(:matcher) { accept_nested_attributes_for(:children) }
it "should accept an existing declaration" do
Parent.accepts_nested_attributes_for :children
matcher.matches?(parent).should be_true
end
it "should reject a missing declaration" do
matcher.matches?(parent).should be_false
matcher.failure_message.should == "Expected Parent to accept nested attributes for children (is not declared)"
end
context "allow_destroy" do
it "should accept a valid truthy value" do
Parent.accepts_nested_attributes_for :children, :allow_destroy => true
matcher.allow_destroy(true).matches?(parent).should be_true
end
it "should accept a valid falsey value" do
Parent.accepts_nested_attributes_for :children, :allow_destroy => false
matcher.allow_destroy(false).matches?(parent).should be_true
end
it "should reject an invalid truthy value" do
Parent.accepts_nested_attributes_for :children, :allow_destroy => true
matcher.allow_destroy(false).matches?(parent).should be_false
matcher.failure_message.should =~ /should not allow destroy/
end
it "should reject an invalid falsey value" do
Parent.accepts_nested_attributes_for :children, :allow_destroy => false
matcher.allow_destroy(true).matches?(parent).should be_false
matcher.failure_message.should =~ /should allow destroy/
end
end
context "limit" do
it "should accept a correct value" do
Parent.accepts_nested_attributes_for :children, :limit => 3
matcher.limit(3).matches?(parent).should be_true
end
it "should reject a false value" do
Parent.accepts_nested_attributes_for :children, :limit => 3
matcher.limit(2).matches?(parent).should be_false
matcher.failure_message.should =~ /limit should be 2, got 3/
end
end
context "update_only" do
it "should accept a valid truthy value" do
Parent.accepts_nested_attributes_for :children, :update_only => true
matcher.update_only(true).matches?(parent).should be_true
end
it "should accept a valid falsey value" do
Parent.accepts_nested_attributes_for :children, :update_only => false
matcher.update_only(false).matches?(parent).should be_true
end
it "should reject an invalid truthy value" do
Parent.accepts_nested_attributes_for :children, :update_only => true
matcher.update_only(false).matches?(parent).should be_false
matcher.failure_message.should =~ /should not be update only/
end
it "should reject an invalid falsey value" do
Parent.accepts_nested_attributes_for :children, :update_only => false
matcher.update_only(true).matches?(parent).should be_false
matcher.failure_message.should =~ /should be update only/
end
end
end