rspec-on-rails-matchers 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/.rspec +1 -0
- data/CHANGELOG +10 -0
- data/Gemfile +8 -0
- data/MIT-LICENSE +20 -0
- data/README +197 -0
- data/Rakefile +9 -0
- data/TODO +4 -0
- data/lib/rspec_on_rails_matchers.rb +8 -0
- data/lib/rspec_on_rails_matchers/associations.rb +51 -0
- data/lib/rspec_on_rails_matchers/observers.rb +6 -0
- data/lib/rspec_on_rails_matchers/validations.rb +96 -0
- data/lib/rspec_on_rails_matchers/version.rb +3 -0
- data/lib/rspec_on_rails_matchers/views.rb +96 -0
- data/rspec-on-rails-matchers.gemspec +21 -0
- data/spec/rspec-on-rails-matchers/associations_spec.rb +38 -0
- data/spec/rspec-on-rails-matchers/validations_spec.rb +56 -0
- data/spec/spec_helper.rb +58 -0
- metadata +104 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/CHANGELOG
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
Change Log
|
2
|
+
==========
|
3
|
+
|
4
|
+
Trunk
|
5
|
+
-----
|
6
|
+
|
7
|
+
* 2008/03/02 - Added have_form_putting_to(url_or_path) - Patch by unknown google code submitter
|
8
|
+
* 2008/03/02 - Added should observe (Luke Melia)
|
9
|
+
* 2008/03/02 - Patched validates_length_of to use within to be consistent with Rails (Matt Pelletier)
|
10
|
+
* 2007/01/03 - Initial Public Release
|
data/Gemfile
ADDED
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 The Plugin Development Team
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README
ADDED
@@ -0,0 +1,197 @@
|
|
1
|
+
rspec-on-rails-matchers
|
2
|
+
=======================
|
3
|
+
|
4
|
+
Setup
|
5
|
+
------
|
6
|
+
|
7
|
+
Add the following to your gemfile:
|
8
|
+
|
9
|
+
gem 'rspec-on-rails-matchers'
|
10
|
+
|
11
|
+
In your spec helper, after the require to 'rspec/rails':
|
12
|
+
|
13
|
+
require 'rspec_on_rails_matchers'
|
14
|
+
|
15
|
+
Dependencies:
|
16
|
+
-------------
|
17
|
+
|
18
|
+
* rspec >= 2
|
19
|
+
* rspec_on_rails
|
20
|
+
|
21
|
+
Overview
|
22
|
+
--------
|
23
|
+
|
24
|
+
Adds the following RSpec matchers using the new Matcher syntax (http://github.com/dchelimsky/rspec/wiki/custom-matchers):
|
25
|
+
|
26
|
+
* Associations:
|
27
|
+
Verify that the association has been defined. (doesn't verify that the association works!)
|
28
|
+
|
29
|
+
object.should have_many(:association)
|
30
|
+
example: @post.should have_many(:comments)
|
31
|
+
TM snippet: [mshm + tab] (Model Should Have Many)
|
32
|
+
|
33
|
+
object.should belong_to(:association)
|
34
|
+
example: @comment.should belong_to(:post)
|
35
|
+
TM snippet: [msbt + tab]
|
36
|
+
|
37
|
+
object.should have_one(:association)
|
38
|
+
user.should have_one(:social_security_number)
|
39
|
+
TM snippet: [msho + tab]
|
40
|
+
|
41
|
+
object.should have_and_belong_to_many(:association)
|
42
|
+
project.should have_and_belong_to_many(:categories)
|
43
|
+
TM snippet: [mshabtm + tab]
|
44
|
+
|
45
|
+
|
46
|
+
* Validations:
|
47
|
+
Verify that a validation has been defined. (doesn't test the validation itself)
|
48
|
+
|
49
|
+
object.should validate_presence_of(:attribute)
|
50
|
+
TM snippet: [msvp + tab]
|
51
|
+
|
52
|
+
object.should validate_confirmation_of(:attribute)
|
53
|
+
TM snippet: [msvc + tab]
|
54
|
+
|
55
|
+
object.should validate_uniqueness_of(:attribute)
|
56
|
+
TM snippet: [msvu + tab]
|
57
|
+
|
58
|
+
object.should validate_length_of(:attribute, :within => 5..10)
|
59
|
+
object.should validate_length_of(:attribute, :is => 5)
|
60
|
+
TM snippet: [msvl + tab]
|
61
|
+
|
62
|
+
* Observers:
|
63
|
+
Verify that the observer is observing a class. (doesn't verify that the observation works)
|
64
|
+
|
65
|
+
object.should observe(:model)
|
66
|
+
example: GroupObserver.should observe(Group)
|
67
|
+
|
68
|
+
* Views:
|
69
|
+
Verifies that the views contains some tags.
|
70
|
+
|
71
|
+
response.should have_form_posting_to(url_or_path)
|
72
|
+
TM snippet: [hfpt + tab]
|
73
|
+
|
74
|
+
response.should have_form_putting_to(url_or_path)
|
75
|
+
|
76
|
+
response.should have_text_field_for(:attribute)
|
77
|
+
TM snippet: [htff + tab]
|
78
|
+
|
79
|
+
response.should have_label_for(:attribute)
|
80
|
+
TM snippet: [hlf + tab]
|
81
|
+
|
82
|
+
response.should have_password_field_for(:attribute)
|
83
|
+
TM snippet: [hpff + tab]
|
84
|
+
|
85
|
+
response.should have_checkbox_for(:attribute)
|
86
|
+
TM snippet: [hcf + tab]
|
87
|
+
|
88
|
+
response.should have_submit_button
|
89
|
+
TM snippet: [hsb + tab]
|
90
|
+
|
91
|
+
response.should have_link_to(url_or_path, "optional_text")
|
92
|
+
TM snippet: [hlt + tab]
|
93
|
+
|
94
|
+
* nested view tests:
|
95
|
+
for instance:
|
96
|
+
|
97
|
+
response.should have_form_posting_to(url_or_path) do
|
98
|
+
with_text_field_for(:attribute)
|
99
|
+
end
|
100
|
+
|
101
|
+
with_text_field_for(:attribute)
|
102
|
+
TM snippet: [wtff + tab]
|
103
|
+
|
104
|
+
with_label_for(:attribute)
|
105
|
+
TM snippet: [wlf + tab]
|
106
|
+
|
107
|
+
with_password_field_for(:attribute)
|
108
|
+
TM snippet: [wpff + tab]
|
109
|
+
|
110
|
+
with_checkbox_for(:attribute)
|
111
|
+
TM snippet: [wcf + tab]
|
112
|
+
|
113
|
+
with_submit_button
|
114
|
+
TM snippet: [wsb + tab]
|
115
|
+
|
116
|
+
with_link_to(url_or_path, "optional_text")
|
117
|
+
TM snippet: [wlt + tab]
|
118
|
+
|
119
|
+
Usage:
|
120
|
+
------
|
121
|
+
|
122
|
+
In your view spec:
|
123
|
+
|
124
|
+
it "should render new form" do
|
125
|
+
render "/users/new.html.erb"
|
126
|
+
|
127
|
+
response.should have_form_posting_to(users_path) do
|
128
|
+
with_text_field_for(:user_name)
|
129
|
+
with_text_area_for(:user_address)
|
130
|
+
with_text_field_for(:user_login)
|
131
|
+
with_text_field_for(:user_email)
|
132
|
+
with_submit_button
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
In your model spec:
|
137
|
+
|
138
|
+
describe User do
|
139
|
+
before(:each) do
|
140
|
+
@user = User.new
|
141
|
+
end
|
142
|
+
|
143
|
+
it "should have many posts" do
|
144
|
+
@user.should have_many(:posts)
|
145
|
+
end
|
146
|
+
|
147
|
+
it "should belong to a group" do
|
148
|
+
@user.should belong_to(:group)
|
149
|
+
end
|
150
|
+
|
151
|
+
it do
|
152
|
+
@user.should validate_presence_of(:email)
|
153
|
+
end
|
154
|
+
|
155
|
+
it do
|
156
|
+
@user.should validate_uniqueness_of(:email)
|
157
|
+
end
|
158
|
+
|
159
|
+
it do
|
160
|
+
@user.should validate_uniqueness_of(:login)
|
161
|
+
end
|
162
|
+
|
163
|
+
it do
|
164
|
+
@user.should validate_presence_of(:login)
|
165
|
+
end
|
166
|
+
|
167
|
+
it do
|
168
|
+
@user.should validate_presence_of(:name)
|
169
|
+
end
|
170
|
+
|
171
|
+
it do
|
172
|
+
@user.should validate_length_of(:password, :between => 4..40)
|
173
|
+
end
|
174
|
+
|
175
|
+
it do
|
176
|
+
@user.should validate_confirmation_of(:password)
|
177
|
+
end
|
178
|
+
|
179
|
+
end
|
180
|
+
|
181
|
+
Core Contributors
|
182
|
+
-----------------
|
183
|
+
|
184
|
+
* Josh Knowles <joshknowles@gmail.com>
|
185
|
+
* Bryan Helmkamp <bryan@brynary.com>
|
186
|
+
* Matt Aimonetti <mattaimonetti@gmail.com>
|
187
|
+
* Scott Taylor <scott@railsnewbie.com>
|
188
|
+
* Stephen Schor <stephen@eastmedia.com>
|
189
|
+
|
190
|
+
Contributors
|
191
|
+
-------------
|
192
|
+
|
193
|
+
* ckknight
|
194
|
+
* Matt Pelletier
|
195
|
+
* Luke Melia
|
196
|
+
|
197
|
+
Copyright (c) 2008 The Plugin Development Team, released under the MIT license
|
data/Rakefile
ADDED
data/TODO
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'rspec_on_rails_matchers', 'version')
|
2
|
+
|
3
|
+
if !defined?(Rails) || Rails.env.test?
|
4
|
+
require File.join(File.dirname(__FILE__), 'rspec_on_rails_matchers', 'observers')
|
5
|
+
require File.join(File.dirname(__FILE__), 'rspec_on_rails_matchers', 'associations')
|
6
|
+
require File.join(File.dirname(__FILE__), 'rspec_on_rails_matchers', 'validations')
|
7
|
+
require File.join(File.dirname(__FILE__), 'rspec_on_rails_matchers', 'views')
|
8
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
RSpec::Matchers.define :belong_to do |association|
|
2
|
+
match do |object|
|
3
|
+
object = object.class if object.is_a? ActiveRecord::Base
|
4
|
+
object.reflect_on_all_associations(:belongs_to).find { |a| a.name == association }
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
RSpec::Matchers.define :have_many do |association|
|
9
|
+
match do |object|
|
10
|
+
object = object.class if object.is_a? ActiveRecord::Base
|
11
|
+
object.reflect_on_all_associations(:has_many).find { |a| a.name == association }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
RSpec::Matchers.define :have_one do |association|
|
16
|
+
match do |object|
|
17
|
+
object = object.class if object.is_a? ActiveRecord::Base
|
18
|
+
object.reflect_on_all_associations(:has_one).find { |a| a.name == association }
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
RSpec::Matchers.define :have_and_belong_to_many do |association|
|
23
|
+
match do |object|
|
24
|
+
object = object.class if object.is_a? ActiveRecord::Base
|
25
|
+
object.reflect_on_all_associations(:has_and_belongs_to_many).find { |a| a.name == association }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
RSpec::Matchers.define :have_valid_associations do |association, *_|
|
30
|
+
failed_association = nil
|
31
|
+
model_class = nil
|
32
|
+
|
33
|
+
match do |object|
|
34
|
+
failed_association = nil
|
35
|
+
model_class = object.class
|
36
|
+
|
37
|
+
model_class.reflect_on_all_associations.each do |assoc|
|
38
|
+
begin
|
39
|
+
object.send(assoc.name)
|
40
|
+
rescue
|
41
|
+
failed_association = assoc.name
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
!failed_association
|
46
|
+
end
|
47
|
+
|
48
|
+
failure_message_for_should do |actual|
|
49
|
+
"invalid or nonexistent association \"#{failed_association}\" on #{model_class}"
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
RSpec::Matchers.define :validate_presence_of do |attribute|
|
2
|
+
match do |object|
|
3
|
+
if !object.respond_to?("#{attribute}=")
|
4
|
+
false
|
5
|
+
else
|
6
|
+
object.send("#{attribute}=", nil)
|
7
|
+
!object.valid? && object.errors[attribute].any?
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
RSpec::Matchers.define :validate_length_of do |attribute, options|
|
13
|
+
if options.has_key? :within
|
14
|
+
min = options[:within].first
|
15
|
+
max = options[:within].last
|
16
|
+
elsif options.has_key? :is
|
17
|
+
min = options[:is]
|
18
|
+
max = min
|
19
|
+
elsif options.has_key? :minimum
|
20
|
+
min = options[:minimum]
|
21
|
+
elsif options.has_key? :maximum
|
22
|
+
max = options[:maximum]
|
23
|
+
end
|
24
|
+
|
25
|
+
invalid = false
|
26
|
+
if !min.nil? && min >= 1
|
27
|
+
object.send("#{attribute}=", 'a' * (min - 1))
|
28
|
+
invalid = !object.valid? && object.errors[attribute].any?
|
29
|
+
end
|
30
|
+
|
31
|
+
if !max.nil?
|
32
|
+
object.send("#{attribute}=", 'a' * (max + 1))
|
33
|
+
invalid ||= !object.valid? && object.errors[attribute].any?
|
34
|
+
end
|
35
|
+
|
36
|
+
invalid
|
37
|
+
end
|
38
|
+
|
39
|
+
RSpec::Matchers.define :validate_uniqueness_of do |attribute|
|
40
|
+
match do |object|
|
41
|
+
object.class.stub!(:with_exclusive_scope).and_return([object])
|
42
|
+
!object.valid? && object.errors[attribute].any?
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
RSpec::Matchers.define :validate_confirmation_of do |attribute|
|
47
|
+
match do |object|
|
48
|
+
object.send("#{attribute}_confirmation=", 'asdf')
|
49
|
+
!object.valid? && object.errors[attribute].any?
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
RSpec::Matchers.define :validate_inclusion_of do |attribute, options|
|
54
|
+
match do |object|
|
55
|
+
values = options[:in]
|
56
|
+
booleans = assign_and_validate_values(object, attribute, values)
|
57
|
+
booleans.all?
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
RSpec::Matchers.define :validate_boolean_of do |attribute, options|
|
62
|
+
match do |object|
|
63
|
+
values = [true, false]
|
64
|
+
|
65
|
+
booleans = values.map do |value|
|
66
|
+
object.send("#{attribute}=", value)
|
67
|
+
object.valid?
|
68
|
+
object.errors[attribute].empty?
|
69
|
+
end
|
70
|
+
|
71
|
+
object.send("#{attribute}=", nil)
|
72
|
+
object.valid?
|
73
|
+
booleans << object.errors[attribute].any?
|
74
|
+
|
75
|
+
booleans.all?
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
|
80
|
+
module RSpec
|
81
|
+
module Matchers
|
82
|
+
|
83
|
+
private
|
84
|
+
def assign_and_validate_value(model, attribute, value)
|
85
|
+
model.send("#{attribute}=", value)
|
86
|
+
model.valid?
|
87
|
+
model.errors[attribute].empty?
|
88
|
+
end
|
89
|
+
|
90
|
+
def assign_and_validate_values(model, attribute, values)
|
91
|
+
values.map do |value|
|
92
|
+
assign_and_validate_value(model, attribute, value)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
RSpec::Matchers.define :have_form_posting_to do |url_or_path|
|
2
|
+
match do |response|
|
3
|
+
have_tag("form[method=post][action=#{url_or_path}]").matches?(response)
|
4
|
+
end
|
5
|
+
end
|
6
|
+
|
7
|
+
RSpec::Matchers.define :have_form_puting_to do |url_or_path|
|
8
|
+
match do |response|
|
9
|
+
have_tag("form[method=post][action=#{url_or_path}/#{id}]").matches?(response)
|
10
|
+
have_tag("input[name=_method][type=hidden][value=put]").matches?(response)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
RSpec::Matchers.define :have_label_for do |attribute|
|
15
|
+
match do |response|
|
16
|
+
have_tag("label[for=#{attribute}]").matches?(response)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
RSpec::Matchers.define :with_label_for do |attribute|
|
21
|
+
match do |response|
|
22
|
+
with_tag("label[for=#{attribute}]").matches?(response)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
RSpec::Matchers.define :have_text_field_for do |attribute|
|
27
|
+
match do |response|
|
28
|
+
have_tag("input##{attribute}[type=text]").matches?(response)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
RSpec::Matchers.define :with_text_field_for do |attribute|
|
33
|
+
match do |response|
|
34
|
+
with_tag("input##{attribute}[type=text]").matches?(response)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
RSpec::Matchers.define :have_text_area_for do |attribute|
|
39
|
+
match do |response|
|
40
|
+
have_tag("textarea##{attribute}[type=text]").matches?(response)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
RSpec::Matchers.define :with_text_area_for do |attribute|
|
45
|
+
match do |response|
|
46
|
+
with_tag("textarea##{attribute}[type=text]").matches?(response)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
RSpec::Matchers.define :have_password_field_for do |attribute|
|
51
|
+
match do |response|
|
52
|
+
have_tag("input##{attribute}[type=password]").matches?(response)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
RSpec::Matchers.define :with_password_field_for do |attribute|
|
57
|
+
match do |response|
|
58
|
+
with_tag("input##{attribute}[type=password]").matches?(response)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
RSpec::Matchers.define :have_checkbox_for do |attribute|
|
63
|
+
match do |response|
|
64
|
+
have_tag("input##{attribute}[type=checkbox]").matches?(response)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
RSpec::Matchers.define :with_checkbox_for do |attribute|
|
69
|
+
match do |response|
|
70
|
+
with_tag("input##{attribute}[type=checkbox]").matches?(response)
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
RSpec::Matchers.define :have_submit_button do |attribute|
|
75
|
+
match do |response|
|
76
|
+
have_tag("input[type=submit]").matches?(response)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
RSpec::Matchers.define :with_submit_button do |attribute|
|
81
|
+
match do |response|
|
82
|
+
with_tag("input[type=submit]").matches?(response)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
RSpec::Matchers.define :have_link_to do |url_or_path|
|
87
|
+
match do |response|
|
88
|
+
have_tag("a[href=#{url_or_path}]", text).matches?(response)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
RSpec::Matchers.define :with_link_to do |url_or_path|
|
93
|
+
match do |response|
|
94
|
+
with_tag("a[href=#{url_or_path}]", text).matches?(response)
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "rspec_on_rails_matchers/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "rspec-on-rails-matchers"
|
7
|
+
s.version = RspecOnRailsMatchers::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Scott Taylor", "Stephen Schor", "Josh Knowles"]
|
10
|
+
s.email = ["scott@railsnewbie.com", "stephen@eastmedia.com"]
|
11
|
+
s.homepage = "http://github.com/smtlaissezfaire/rspec-on-rails-matchers"
|
12
|
+
s.summary = %q{Rspec on rails matchers}
|
13
|
+
s.description = %q{Josh Knowles rspec-on-rails-matchers, upgraded for Rails 3+.}
|
14
|
+
|
15
|
+
s.add_development_dependency "rspec"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
# s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe "RSpec::Matchers" do
|
4
|
+
describe "belongs_to" do
|
5
|
+
describe Comment do
|
6
|
+
it { should belong_to(:post) }
|
7
|
+
it { should_not belong_to(:foobar) }
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "valid associations" do
|
12
|
+
describe Comment do
|
13
|
+
it { should have_valid_associations }
|
14
|
+
end
|
15
|
+
|
16
|
+
describe Image do
|
17
|
+
it { should have_valid_associations }
|
18
|
+
end
|
19
|
+
|
20
|
+
describe Post do
|
21
|
+
it { should have_valid_associations }
|
22
|
+
end
|
23
|
+
|
24
|
+
describe Boolean do
|
25
|
+
it { should have_valid_associations }
|
26
|
+
end
|
27
|
+
|
28
|
+
describe InvalidAssociationClass do
|
29
|
+
it { should_not have_valid_associations }
|
30
|
+
|
31
|
+
it "should use the failed association name in the failure message" do
|
32
|
+
lambda {
|
33
|
+
subject.should have_valid_associations
|
34
|
+
}.should raise_error(RSpec::Expectations::ExpectationNotMetError, 'invalid or nonexistent association "foos" on InvalidAssociationClass')
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe RSpec::Matchers, "validations" do
|
4
|
+
before do
|
5
|
+
@example = self
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "validates_presence_of" do
|
9
|
+
before do
|
10
|
+
@comment = Comment.new
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should match if the object has a validates_presence_of" do
|
14
|
+
@example.validate_presence_of(:post).matches?(@comment).should be_true
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should not match if the object does not have the association" do
|
18
|
+
@example.validate_presence_of(:an_attr_accessor).matches?(@comment).should be_false
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should not match if the object does not respond to the method" do
|
22
|
+
@example.validate_presence_of(:foobar).matches?(@comment).should be_false
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "validate_inclusion_of" do
|
27
|
+
before do
|
28
|
+
@boolean = Boolean.new
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should be true if it is the same list" do
|
32
|
+
matcher = @example.validate_inclusion_of(:null_not_allowable, :in => [true, false])
|
33
|
+
matcher.matches?(@boolean).should be_true
|
34
|
+
end
|
35
|
+
|
36
|
+
it "should not be true if one of the values is not true" do
|
37
|
+
@example.validate_inclusion_of(:null_not_allowable, :in => [nil]).matches?(@boolean).should be_false
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe "validate_boolean_of" do
|
42
|
+
before do
|
43
|
+
@boolean = Boolean.new
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should be true if it is valid when [true, false]" do
|
47
|
+
matcher = @example.validate_boolean_of(:null_not_allowable)
|
48
|
+
matcher.matches?(@boolean).should be_true
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should not be true if it allows a nil" do
|
52
|
+
matcher = @example.validate_boolean_of(:null_allowable)
|
53
|
+
matcher.matches?(@boolean).should be_false
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + "/../lib/rspec_on_rails_matchers")
|
3
|
+
|
4
|
+
ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => ':memory:'
|
5
|
+
ActiveRecord::Migration.verbose = true
|
6
|
+
|
7
|
+
class Comment < ActiveRecord::Base
|
8
|
+
belongs_to :post
|
9
|
+
belongs_to :commentable, :polymorphic => true
|
10
|
+
|
11
|
+
validates_presence_of :post
|
12
|
+
|
13
|
+
attr_accessor :an_attr_accessor
|
14
|
+
end
|
15
|
+
|
16
|
+
class Image < ActiveRecord::Base
|
17
|
+
has_one :comment, :as => :commentable
|
18
|
+
end
|
19
|
+
|
20
|
+
class Post < ActiveRecord::Base
|
21
|
+
has_many :comments
|
22
|
+
end
|
23
|
+
|
24
|
+
class Boolean < ActiveRecord::Base
|
25
|
+
validates_inclusion_of :null_not_allowable, :in => [true, false]
|
26
|
+
end
|
27
|
+
|
28
|
+
class InvalidAssociationClass < ActiveRecord::Base
|
29
|
+
set_table_name "invalid_association"
|
30
|
+
|
31
|
+
has_many :foos
|
32
|
+
end
|
33
|
+
|
34
|
+
ActiveRecord::Schema.define do
|
35
|
+
create_table :booleans do |t|
|
36
|
+
t.boolean :null_allowable
|
37
|
+
t.boolean :null_not_allowable
|
38
|
+
end
|
39
|
+
|
40
|
+
create_table :posts do |t|
|
41
|
+
t.string :name
|
42
|
+
end
|
43
|
+
|
44
|
+
create_table :comments do |t|
|
45
|
+
t.string :text
|
46
|
+
t.integer :post_id
|
47
|
+
t.integer :commentable_id
|
48
|
+
t.string :commentable_type
|
49
|
+
end
|
50
|
+
|
51
|
+
create_table :images do |t|
|
52
|
+
t.string :image_id
|
53
|
+
end
|
54
|
+
|
55
|
+
create_table :invalid_association, :force => true do |t|
|
56
|
+
t.timestamps
|
57
|
+
end
|
58
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rspec-on-rails-matchers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Scott Taylor
|
14
|
+
- Stephen Schor
|
15
|
+
- Josh Knowles
|
16
|
+
autorequire:
|
17
|
+
bindir: bin
|
18
|
+
cert_chain: []
|
19
|
+
|
20
|
+
date: 2011-06-06 00:00:00 -04:00
|
21
|
+
default_executable:
|
22
|
+
dependencies:
|
23
|
+
- !ruby/object:Gem::Dependency
|
24
|
+
name: rspec
|
25
|
+
prerelease: false
|
26
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
27
|
+
none: false
|
28
|
+
requirements:
|
29
|
+
- - ">="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
hash: 3
|
32
|
+
segments:
|
33
|
+
- 0
|
34
|
+
version: "0"
|
35
|
+
type: :development
|
36
|
+
version_requirements: *id001
|
37
|
+
description: Josh Knowles rspec-on-rails-matchers, upgraded for Rails 3+.
|
38
|
+
email:
|
39
|
+
- scott@railsnewbie.com
|
40
|
+
- stephen@eastmedia.com
|
41
|
+
executables: []
|
42
|
+
|
43
|
+
extensions: []
|
44
|
+
|
45
|
+
extra_rdoc_files: []
|
46
|
+
|
47
|
+
files:
|
48
|
+
- .gitignore
|
49
|
+
- .rspec
|
50
|
+
- CHANGELOG
|
51
|
+
- Gemfile
|
52
|
+
- Gemfile.lock
|
53
|
+
- MIT-LICENSE
|
54
|
+
- README
|
55
|
+
- Rakefile
|
56
|
+
- TODO
|
57
|
+
- lib/rspec_on_rails_matchers.rb
|
58
|
+
- lib/rspec_on_rails_matchers/associations.rb
|
59
|
+
- lib/rspec_on_rails_matchers/observers.rb
|
60
|
+
- lib/rspec_on_rails_matchers/validations.rb
|
61
|
+
- lib/rspec_on_rails_matchers/version.rb
|
62
|
+
- lib/rspec_on_rails_matchers/views.rb
|
63
|
+
- rspec-on-rails-matchers.gemspec
|
64
|
+
- spec/rspec-on-rails-matchers/associations_spec.rb
|
65
|
+
- spec/rspec-on-rails-matchers/validations_spec.rb
|
66
|
+
- spec/spec_helper.rb
|
67
|
+
has_rdoc: true
|
68
|
+
homepage: http://github.com/smtlaissezfaire/rspec-on-rails-matchers
|
69
|
+
licenses: []
|
70
|
+
|
71
|
+
post_install_message:
|
72
|
+
rdoc_options: []
|
73
|
+
|
74
|
+
require_paths:
|
75
|
+
- lib
|
76
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
77
|
+
none: false
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
hash: 3
|
82
|
+
segments:
|
83
|
+
- 0
|
84
|
+
version: "0"
|
85
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
86
|
+
none: false
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
hash: 3
|
91
|
+
segments:
|
92
|
+
- 0
|
93
|
+
version: "0"
|
94
|
+
requirements: []
|
95
|
+
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 1.3.7
|
98
|
+
signing_key:
|
99
|
+
specification_version: 3
|
100
|
+
summary: Rspec on rails matchers
|
101
|
+
test_files:
|
102
|
+
- spec/rspec-on-rails-matchers/associations_spec.rb
|
103
|
+
- spec/rspec-on-rails-matchers/validations_spec.rb
|
104
|
+
- spec/spec_helper.rb
|