shoulda-matchers 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. data/.gitignore +11 -0
  2. data/.travis.yml +13 -0
  3. data/Appraisals +4 -6
  4. data/CONTRIBUTING.md +38 -0
  5. data/Gemfile +8 -5
  6. data/Gemfile.lock +77 -41
  7. data/NEWS.md +32 -0
  8. data/README.md +84 -0
  9. data/Rakefile +5 -36
  10. data/features/rails_integration.feature +88 -0
  11. data/features/step_definitions/rails_steps.rb +111 -0
  12. data/features/support/env.rb +5 -0
  13. data/gemfiles/3.0.gemfile +14 -0
  14. data/gemfiles/3.0.gemfile.lock +142 -0
  15. data/gemfiles/3.1.gemfile +16 -0
  16. data/gemfiles/3.1.gemfile.lock +164 -0
  17. data/lib/shoulda/matchers/action_controller/assign_to_matcher.rb +6 -9
  18. data/lib/shoulda/matchers/action_controller/filter_param_matcher.rb +1 -3
  19. data/lib/shoulda/matchers/action_controller/redirect_to_matcher.rb +10 -6
  20. data/lib/shoulda/matchers/action_controller/render_template_matcher.rb +1 -4
  21. data/lib/shoulda/matchers/action_controller/render_with_layout_matcher.rb +6 -6
  22. data/lib/shoulda/matchers/action_controller/respond_with_content_type_matcher.rb +11 -10
  23. data/lib/shoulda/matchers/action_controller/respond_with_matcher.rb +0 -2
  24. data/lib/shoulda/matchers/action_controller/route_matcher.rb +17 -14
  25. data/lib/shoulda/matchers/action_controller/set_session_matcher.rb +24 -16
  26. data/lib/shoulda/matchers/action_controller/set_the_flash_matcher.rb +52 -15
  27. data/lib/shoulda/matchers/action_mailer.rb +1 -1
  28. data/lib/shoulda/matchers/action_mailer/{have_sent_email.rb → have_sent_email_matcher.rb} +37 -21
  29. data/lib/shoulda/matchers/active_model.rb +1 -0
  30. data/lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb +9 -10
  31. data/lib/shoulda/matchers/active_model/allow_value_matcher.rb +60 -33
  32. data/lib/shoulda/matchers/active_model/ensure_length_of_matcher.rb +0 -1
  33. data/lib/shoulda/matchers/active_model/helpers.rb +13 -9
  34. data/lib/shoulda/matchers/active_model/validate_confirmation_of_matcher.rb +63 -0
  35. data/lib/shoulda/matchers/active_model/validate_numericality_of_matcher.rb +38 -8
  36. data/lib/shoulda/matchers/active_model/validation_matcher.rb +1 -5
  37. data/lib/shoulda/matchers/active_record.rb +3 -1
  38. data/lib/shoulda/matchers/active_record/accept_nested_attributes_for_matcher.rb +127 -0
  39. data/lib/shoulda/matchers/active_record/association_matcher.rb +19 -7
  40. data/lib/shoulda/matchers/active_record/have_db_column_matcher.rb +20 -5
  41. data/lib/shoulda/matchers/active_record/have_db_index_matcher.rb +4 -10
  42. data/lib/shoulda/matchers/active_record/have_readonly_attribute_matcher.rb +3 -7
  43. data/lib/shoulda/matchers/active_record/query_the_database_matcher.rb +107 -0
  44. data/lib/shoulda/matchers/active_record/serialize_matcher.rb +111 -0
  45. data/lib/shoulda/matchers/integrations/rspec.rb +0 -1
  46. data/lib/shoulda/matchers/version.rb +1 -1
  47. data/shoulda-matchers.gemspec +30 -0
  48. data/spec/fixtures/addresses.yml +3 -0
  49. data/spec/fixtures/friendships.yml +0 -0
  50. data/spec/fixtures/posts.yml +5 -0
  51. data/spec/fixtures/products.yml +0 -0
  52. data/spec/fixtures/taggings.yml +0 -0
  53. data/spec/fixtures/tags.yml +9 -0
  54. data/spec/fixtures/users.yml +6 -0
  55. data/spec/shoulda/action_controller/assign_to_matcher_spec.rb +61 -0
  56. data/spec/shoulda/action_controller/filter_param_matcher_spec.rb +20 -0
  57. data/spec/shoulda/action_controller/redirect_to_matcher_spec.rb +40 -0
  58. data/spec/shoulda/action_controller/render_template_matcher_spec.rb +69 -0
  59. data/spec/shoulda/action_controller/render_with_layout_matcher_spec.rb +47 -0
  60. data/spec/shoulda/action_controller/respond_with_content_type_matcher_spec.rb +28 -0
  61. data/spec/shoulda/action_controller/respond_with_matcher_spec.rb +83 -0
  62. data/spec/shoulda/action_controller/route_matcher_spec.rb +65 -0
  63. data/spec/shoulda/action_controller/set_session_matcher_spec.rb +46 -0
  64. data/spec/shoulda/action_controller/set_the_flash_matcher_spec.rb +124 -0
  65. data/spec/shoulda/action_mailer/have_sent_email_spec.rb +293 -0
  66. data/spec/shoulda/active_model/allow_mass_assignment_of_matcher_spec.rb +95 -0
  67. data/spec/shoulda/active_model/allow_value_matcher_spec.rb +91 -0
  68. data/spec/shoulda/active_model/ensure_exclusion_of_matcher_spec.rb +57 -0
  69. data/spec/shoulda/active_model/ensure_inclusion_of_matcher_spec.rb +71 -0
  70. data/spec/shoulda/active_model/ensure_length_of_matcher_spec.rb +125 -0
  71. data/spec/shoulda/active_model/helpers_spec.rb +100 -0
  72. data/spec/shoulda/active_model/validate_acceptance_of_matcher_spec.rb +43 -0
  73. data/spec/shoulda/active_model/validate_confirmation_of_matcher_spec.rb +48 -0
  74. data/spec/shoulda/active_model/validate_format_of_matcher_spec.rb +38 -0
  75. data/spec/shoulda/active_model/validate_numericality_of_matcher_spec.rb +62 -0
  76. data/spec/shoulda/active_model/validate_presence_of_matcher_spec.rb +121 -0
  77. data/spec/shoulda/active_model/validate_uniqueness_of_matcher_spec.rb +143 -0
  78. data/spec/shoulda/active_record/accept_nested_attributes_for_matcher_spec.rb +84 -0
  79. data/spec/shoulda/active_record/association_matcher_spec.rb +449 -0
  80. data/spec/shoulda/active_record/have_db_column_matcher_spec.rb +185 -0
  81. data/spec/shoulda/active_record/have_db_index_matcher_spec.rb +88 -0
  82. data/spec/shoulda/active_record/have_readonly_attributes_matcher_spec.rb +46 -0
  83. data/spec/shoulda/active_record/query_the_database_matcher_spec.rb +45 -0
  84. data/spec/shoulda/active_record/serialize_matcher_spec.rb +81 -0
  85. data/spec/spec_helper.rb +31 -0
  86. data/spec/support/model_builder.rb +149 -0
  87. metadata +211 -60
  88. data/CONTRIBUTION_GUIDELINES.rdoc +0 -10
  89. data/README.rdoc +0 -80
@@ -0,0 +1,185 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActiveRecord::HaveDbColumnMatcher do
4
+ context "have_db_column" do
5
+ before do
6
+ @matcher = have_db_column(:nickname)
7
+ end
8
+
9
+ it "should accept an existing database column" do
10
+ create_table 'superheros' do |table|
11
+ table.string :nickname
12
+ end
13
+ define_model_class 'Superhero'
14
+ Superhero.new.should @matcher
15
+ end
16
+
17
+ it "should reject a nonexistent database column" do
18
+ define_model :superhero
19
+ Superhero.new.should_not @matcher
20
+ end
21
+ end
22
+
23
+ context "have_db_column of type string" do
24
+ before do
25
+ @matcher = have_db_column(:nickname).of_type(:string)
26
+ end
27
+
28
+ it "should accept a column of correct type" do
29
+ create_table 'superheros' do |table|
30
+ table.string :nickname
31
+ end
32
+ define_model_class 'Superhero'
33
+ Superhero.new.should @matcher
34
+ end
35
+
36
+ it "should reject a nonexistent database column" do
37
+ define_model :superhero
38
+ Superhero.new.should_not @matcher
39
+ end
40
+
41
+ it "should reject a column of wrong type" do
42
+ create_table 'superheros' do |table|
43
+ table.integer :nickname
44
+ end
45
+ define_model_class 'Superhero'
46
+ Superhero.new.should_not @matcher
47
+ end
48
+ end
49
+
50
+ context "have_db_column with precision option" do
51
+ before do
52
+ @matcher = have_db_column(:salary).with_options(:precision => 5)
53
+ end
54
+
55
+ it "should accept a column of correct precision" do
56
+ create_table 'superheros' do |table|
57
+ table.decimal :salary, :precision => 5
58
+ end
59
+ define_model_class 'Superhero'
60
+ Superhero.new.should @matcher
61
+ end
62
+
63
+ it "should reject a column of wrong precision" do
64
+ create_table 'superheros' do |table|
65
+ table.decimal :salary, :precision => 15
66
+ end
67
+ define_model_class 'Superhero'
68
+ Superhero.new.should_not @matcher
69
+ end
70
+ end
71
+
72
+ context "have_db_column with limit option" do
73
+ before do
74
+ @matcher = have_db_column(:email).
75
+ of_type(:string).
76
+ with_options(:limit => 255)
77
+ end
78
+
79
+ it "should accept a column of correct limit" do
80
+ create_table 'superheros' do |table|
81
+ table.string :email, :limit => 255
82
+ end
83
+ define_model_class 'Superhero'
84
+ Superhero.new.should @matcher
85
+ end
86
+
87
+ it "should reject a column of wrong limit" do
88
+ create_table 'superheros' do |table|
89
+ table.string :email, :limit => 500
90
+ end
91
+ define_model_class 'Superhero'
92
+ Superhero.new.should_not @matcher
93
+ end
94
+ end
95
+
96
+ context "have_db_column with default option" do
97
+ before do
98
+ @matcher = have_db_column(:admin).
99
+ of_type(:boolean).
100
+ with_options(:default => false)
101
+ end
102
+
103
+ it "should accept a column of correct default" do
104
+ create_table 'superheros' do |table|
105
+ table.boolean :admin, :default => false
106
+ end
107
+ define_model_class 'Superhero'
108
+ Superhero.new.should @matcher
109
+ end
110
+
111
+ it "should reject a column of wrong default" do
112
+ create_table 'superheros' do |table|
113
+ table.boolean :admin, :default => true
114
+ end
115
+ define_model_class 'Superhero'
116
+ Superhero.new.should_not @matcher
117
+ end
118
+ end
119
+
120
+ context "have_db_column with null option" do
121
+ before do
122
+ @matcher = have_db_column(:admin).
123
+ of_type(:boolean).
124
+ with_options(:null => false)
125
+ end
126
+
127
+ it "should accept a column of correct null" do
128
+ create_table 'superheros' do |table|
129
+ table.boolean :admin, :null => false
130
+ end
131
+ define_model_class 'Superhero'
132
+ Superhero.new.should @matcher
133
+ end
134
+
135
+ it "should reject a column of wrong null" do
136
+ create_table 'superheros' do |table|
137
+ table.boolean :admin, :null => true
138
+ end
139
+ define_model_class 'Superhero'
140
+ Superhero.new.should_not @matcher
141
+ end
142
+ end
143
+
144
+ context "have_db_column with scale option" do
145
+ before do
146
+ @matcher = have_db_column(:salary).
147
+ of_type(:decimal).
148
+ with_options(:scale => 2)
149
+ end
150
+
151
+ it "should accept a column of correct scale" do
152
+ create_table 'superheros' do |table|
153
+ table.decimal :salary, :precision => 10, :scale => 2
154
+ end
155
+ define_model_class 'Superhero'
156
+ Superhero.new.should @matcher
157
+ end
158
+
159
+ it "should reject a column of wrong scale" do
160
+ create_table 'superheros' do |table|
161
+ table.decimal :salary, :precision => 10, :scale => 4
162
+ end
163
+ define_model_class 'Superhero'
164
+ Superhero.new.should_not @matcher
165
+ end
166
+ end
167
+
168
+ context "have_db_column with primary option" do
169
+ it "should accept a column that is primary" do
170
+ create_table 'superheros' do |table|
171
+ table.integer :id, :primary => true
172
+ end
173
+ define_model_class 'Superhero'
174
+ Superhero.new.should have_db_column(:id).with_options(:primary => true)
175
+ end
176
+
177
+ it "should reject a column that is not primary" do
178
+ create_table 'superheros' do |table|
179
+ table.integer :primary
180
+ end
181
+ define_model_class 'Superhero'
182
+ Superhero.new.should_not have_db_column(:primary).with_options(:primary => true)
183
+ end
184
+ end
185
+ end
@@ -0,0 +1,88 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActiveRecord::HaveDbIndexMatcher do
4
+ context "have_db_index" do
5
+ before do
6
+ @matcher = have_db_index(:age)
7
+ end
8
+
9
+ it "should accept an existing index" do
10
+ db_connection = create_table 'superheros' do |table|
11
+ table.integer :age
12
+ end
13
+ db_connection.add_index :superheros, :age
14
+ define_model_class 'Superhero'
15
+ Superhero.new.should @matcher
16
+ end
17
+
18
+ it "should reject a nonexistent index" do
19
+ define_model :superhero
20
+ Superhero.new.should_not @matcher
21
+ end
22
+ end
23
+
24
+ context "have_db_index with unique option" do
25
+ before do
26
+ @matcher = have_db_index(:ssn).unique(true)
27
+ end
28
+
29
+ it "should accept an index of correct unique" do
30
+ db_connection = create_table 'superheros' do |table|
31
+ table.integer :ssn
32
+ end
33
+ db_connection.add_index :superheros, :ssn, :unique => true
34
+ define_model_class 'Superhero'
35
+ Superhero.new.should @matcher
36
+ end
37
+
38
+ it "should reject an index of wrong unique" do
39
+ db_connection = create_table 'superheros' do |table|
40
+ table.integer :ssn
41
+ end
42
+ db_connection.add_index :superheros, :ssn, :unique => false
43
+ define_model_class 'Superhero'
44
+ Superhero.new.should_not @matcher
45
+ end
46
+ end
47
+
48
+ context "have_db_index on multiple columns" do
49
+ before do
50
+ @matcher = have_db_index([:geocodable_type, :geocodable_id])
51
+ end
52
+
53
+ it "should accept an existing index" do
54
+ db_connection = create_table 'geocodings' do |table|
55
+ table.integer :geocodable_id
56
+ table.string :geocodable_type
57
+ end
58
+ db_connection.add_index :geocodings, [:geocodable_type, :geocodable_id]
59
+ define_model_class 'Geocoding'
60
+ Geocoding.new.should @matcher
61
+ end
62
+
63
+ it "should reject a nonexistant index" do
64
+ db_connection = create_table 'geocodings' do |table|
65
+ table.integer :geocodable_id
66
+ table.string :geocodable_type
67
+ end
68
+ define_model_class 'Geocoding'
69
+ Geocoding.new.should_not @matcher
70
+ end
71
+ end
72
+
73
+ it "should join columns with and describing multiple columns" do
74
+ have_db_index([:user_id, :post_id]).description.should =~ /on columns user_id and post_id/
75
+ end
76
+
77
+ it "should context a unique index as unique" do
78
+ have_db_index(:user_id).unique(true).description.should =~ /a unique index/
79
+ end
80
+
81
+ it "should context a non-unique index as non-unique" do
82
+ have_db_index(:user_id).unique(false).description.should =~ /a non-unique index/
83
+ end
84
+
85
+ it "should not context an index's uniqueness when it isn't important" do
86
+ have_db_index(:user_id).description.should_not =~ /unique/
87
+ end
88
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActiveRecord::HaveReadonlyAttributeMatcher do
4
+ context "an attribute that cannot be set after being saved" do
5
+ before do
6
+ define_model :example, :attr => :string do
7
+ attr_readonly :attr
8
+ end
9
+ @model = Example.new
10
+ end
11
+
12
+ it "should accept being read-only" do
13
+ @model.should have_readonly_attribute(:attr)
14
+ end
15
+ end
16
+
17
+ context "an attribute not included in the readonly set" do
18
+ before do
19
+ define_model :example, :attr => :string, :other => :string do
20
+ attr_readonly :other
21
+ end
22
+ @model = Example.new
23
+ end
24
+
25
+ it "should not accept being read-only" do
26
+ @model.should_not have_readonly_attribute(:attr)
27
+ end
28
+ end
29
+
30
+ context "an attribute on a class with no readonly attributes" do
31
+ before do
32
+ define_model :example, :attr => :string
33
+ @model = Example.new
34
+ end
35
+
36
+ it "should not accept being read-only" do
37
+ @model.should_not have_readonly_attribute(:attr)
38
+ end
39
+
40
+ it "should assign a failure message" do
41
+ matcher = have_readonly_attribute(:attr)
42
+ matcher.matches?(@model).should == false
43
+ matcher.failure_message.should_not be_nil
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActiveRecord::QueryTheDatabaseMatcher do
4
+ before do
5
+ @parent = define_model :litter do
6
+ has_many :kittens
7
+ end
8
+ @child = define_model :kitten, :litter_id => :integer do
9
+ belongs_to :litter
10
+ end
11
+ end
12
+
13
+ it "accepts the correct number of queries when there is a single query" do
14
+ @parent.should query_the_database(1.times).when_calling(:count)
15
+ end
16
+
17
+ it "accepts any number of queries when no number is specified" do
18
+ @parent.should query_the_database.when_calling(:count)
19
+ end
20
+
21
+ it "rejects any number of queries when no number is specified" do
22
+ @parent.should_not query_the_database.when_calling(:to_s)
23
+ end
24
+
25
+ it "accepts the correct number of queries when there are two queries" do
26
+ nonsense = lambda do
27
+ @parent.create.kittens.create
28
+ end
29
+ nonsense.should query_the_database(2.times).when_calling(:call)
30
+ end
31
+
32
+ it "rejects the wrong number of queries" do
33
+ @parent.should_not query_the_database(10.times).when_calling(:count)
34
+ end
35
+
36
+ it "accepts fewer than the specified maximum" do
37
+ @parent.should query_the_database(5.times).or_less.when_calling(:count)
38
+ end
39
+
40
+ it "passes arguments to the method to examine" do
41
+ model = stub("Model", :count => nil)
42
+ model.should_not query_the_database.when_calling(:count).with("arguments")
43
+ model.should have_received(:count).with("arguments")
44
+ end
45
+ end
@@ -0,0 +1,81 @@
1
+ require 'spec_helper'
2
+
3
+ describe Shoulda::Matchers::ActiveRecord::SerializeMatcher do
4
+ context "an attribute that should be serialized" do
5
+ let(:model) do
6
+ define_model(:example, :attr => :string) do
7
+ serialize :attr
8
+ end.new
9
+ end
10
+
11
+ it "should be serialized" do
12
+ model.should serialize(:attr)
13
+ end
14
+ end
15
+
16
+ context "an attribute that should be serialized with a type of Hash" do
17
+ let(:model) do
18
+ define_model(:example, :attr => :string) do
19
+ serialize :attr, Hash
20
+ end.new
21
+ end
22
+
23
+ it "should be serialized" do
24
+ model.should serialize(:attr).as(Hash)
25
+ end
26
+
27
+ it "should not match when using as_instance_of" do
28
+ model.should_not serialize(:attr).as_instance_of(Hash)
29
+ end
30
+ end
31
+
32
+ context "an attribute that should be serialized with a type of Array" do
33
+ let(:model) do
34
+ define_model(:example, :attr => :string, :attr2 => :string) do
35
+ serialize :attr, Array
36
+ serialize :attr2, Array
37
+ end.new
38
+ end
39
+
40
+ it "should be serialized" do
41
+ model.should serialize(:attr).as(Array)
42
+ end
43
+ end
44
+
45
+ context "an attribute that should be serialized but isn't" do
46
+ let(:model) { define_model(:example, :attr => :string).new }
47
+
48
+ it "should assign a failure message" do
49
+ matcher = serialize(:attr)
50
+ matcher.matches?(model).should == false
51
+ matcher.failure_message.should_not be_nil
52
+ end
53
+
54
+ it "should assign a failure message with 'as'" do
55
+ matcher = serialize(:attr).as(Hash)
56
+ matcher.matches?(model).should == false
57
+ matcher.failure_message.should_not be_nil
58
+ end
59
+
60
+ context "a serializer that is an instance of a class" do
61
+ before do
62
+ define_constant(:ExampleSerializer, Object) do
63
+ def load(*); end
64
+ def dump(*); end
65
+ end
66
+ define_model :example, :attr => :string do
67
+ serialize :attr, ExampleSerializer.new
68
+ end
69
+ @model = Example.new
70
+ end
71
+
72
+ it "should match when using as_instance_of" do
73
+ @model.should serialize(:attr).as_instance_of(ExampleSerializer)
74
+ end
75
+
76
+ it "should not match when using as" do
77
+ @model.should_not serialize(:attr).as(ExampleSerializer)
78
+ end
79
+ end
80
+ end
81
+ end