saturnflyer-rspec-rails-matchers 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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/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,187 @@
1
+ rspec-on-rails-matchers
2
+ =======================
3
+
4
+ Setup
5
+ ------
6
+
7
+ Dependencies:
8
+ -------------
9
+
10
+ * rspec
11
+ * rspec_on_rails
12
+
13
+ Overview
14
+ --------
15
+
16
+ Adds the following RSpec matchers:
17
+
18
+ * Associations:
19
+ Verify that the association has been defined. (doesn't verify that the association works!)
20
+
21
+ object.should have_many(:association)
22
+ example: @post.should have_many(:comments)
23
+ TM snippet: [mshm + tab] (Model Should Have Many)
24
+
25
+ object.should belong_to(:association)
26
+ example: @comment.should belong_to(:post)
27
+ TM snippet: [msbt + tab]
28
+
29
+ object.should have_one(:association)
30
+ user.should have_one(:social_security_number)
31
+ TM snippet: [msho + tab]
32
+
33
+ object.should have_and_belong_to_many(:association)
34
+ project.should have_and_belong_to_many(:categories)
35
+ TM snippet: [mshabtm + tab]
36
+
37
+
38
+ * Validations:
39
+ Verify that a validation has been defined. (doesn't test the validation itself)
40
+
41
+ object.should validate_presence_of(:attribute)
42
+ TM snippet: [msvp + tab]
43
+
44
+ object.should validate_confirmation_of(:attribute)
45
+ TM snippet: [msvc + tab]
46
+
47
+ object.should validate_uniqueness_of(:attribute)
48
+ TM snippet: [msvu + tab]
49
+
50
+ object.should validate_length_of(:attribute, :within => 5..10)
51
+ object.should validate_length_of(:attribute, :is => 5)
52
+ TM snippet: [msvl + tab]
53
+
54
+ * Observers:
55
+ Verify that the observer is observing a class. (doesn't verify that the observation works)
56
+
57
+ object.should observe(:model)
58
+ example: GroupObserver.should observe(Group)
59
+
60
+ * Views:
61
+ Verifies that the views contains some tags.
62
+
63
+ response.should have_form_posting_to(url_or_path)
64
+ TM snippet: [hfpt + tab]
65
+
66
+ response.should have_form_putting_to(url_or_path)
67
+
68
+ response.should have_text_field_for(:attribute)
69
+ TM snippet: [htff + tab]
70
+
71
+ response.should have_label_for(:attribute)
72
+ TM snippet: [hlf + tab]
73
+
74
+ response.should have_password_field_for(:attribute)
75
+ TM snippet: [hpff + tab]
76
+
77
+ response.should have_checkbox_for(:attribute)
78
+ TM snippet: [hcf + tab]
79
+
80
+ response.should have_submit_button
81
+ TM snippet: [hsb + tab]
82
+
83
+ response.should have_link_to(url_or_path, "optional_text")
84
+ TM snippet: [hlt + tab]
85
+
86
+ * nested view tests:
87
+ for instance:
88
+
89
+ response.should have_form_posting_to(url_or_path) do
90
+ with_text_field_for(:attribute)
91
+ end
92
+
93
+ with_text_field_for(:attribute)
94
+ TM snippet: [wtff + tab]
95
+
96
+ with_label_for(:attribute)
97
+ TM snippet: [wlf + tab]
98
+
99
+ with_password_field_for(:attribute)
100
+ TM snippet: [wpff + tab]
101
+
102
+ with_checkbox_for(:attribute)
103
+ TM snippet: [wcf + tab]
104
+
105
+ with_submit_button
106
+ TM snippet: [wsb + tab]
107
+
108
+ with_link_to(url_or_path, "optional_text")
109
+ TM snippet: [wlt + tab]
110
+
111
+ Usage:
112
+ ------
113
+
114
+ In your view spec:
115
+
116
+ it "should render new form" do
117
+ render "/users/new.html.erb"
118
+
119
+ response.should have_form_posting_to(users_path) do
120
+ with_text_field_for(:user_name)
121
+ with_text_area_for(:user_address)
122
+ with_text_field_for(:user_login)
123
+ with_text_field_for(:user_email)
124
+ with_submit_button
125
+ end
126
+ end
127
+
128
+ In your model spec:
129
+
130
+ describe User do
131
+ before(:each) do
132
+ @user = User.new
133
+ end
134
+
135
+ it "should have many posts" do
136
+ @user.should have_many(:posts)
137
+ end
138
+
139
+ it "should belong to a group" do
140
+ @user.should belong_to(:group)
141
+ end
142
+
143
+ it do
144
+ @user.should validate_presence_of(:email)
145
+ end
146
+
147
+ it do
148
+ @user.should validate_uniqueness_of(:email)
149
+ end
150
+
151
+ it do
152
+ @user.should validate_uniqueness_of(:login)
153
+ end
154
+
155
+ it do
156
+ @user.should validate_presence_of(:login)
157
+ end
158
+
159
+ it do
160
+ @user.should validate_presence_of(:name)
161
+ end
162
+
163
+ it do
164
+ @user.should validate_length_of(:password, :between => 4..40)
165
+ end
166
+
167
+ it do
168
+ @user.should validate_confirmation_of(:password)
169
+ end
170
+
171
+ end
172
+
173
+ Core Contributors
174
+ -----------------
175
+
176
+ * Josh Knowles <joshknowles@gmail.com>
177
+ * Bryan Helmkamp <bryan@brynary.com>
178
+ * Matt Aimonetti <mattaimonetti@gmail.com>
179
+
180
+ Contributors
181
+ -------------
182
+
183
+ * ckknight
184
+ * Matt Pelletier
185
+ * Luke Melia
186
+
187
+ Copyright (c) 2008 The Plugin Development Team, released under the MIT license
data/TODO ADDED
@@ -0,0 +1,4 @@
1
+ To Do
2
+ =====
3
+
4
+ * Update README to include instructions on installing from github
@@ -0,0 +1,33 @@
1
+ module Spec
2
+ module Rails
3
+ module Matchers
4
+ def belong_to(association)
5
+ return simple_matcher("model to belong to #{association}") do |model|
6
+ model = model.class if model.is_a? ActiveRecord::Base
7
+ model.reflect_on_all_associations(:belongs_to).find { |a| a.name == association }
8
+ end
9
+ end
10
+
11
+ def have_many(association)
12
+ return simple_matcher("model to have many #{association}") do |model|
13
+ model = model.class if model.is_a? ActiveRecord::Base
14
+ model.reflect_on_all_associations(:has_many).find { |a| a.name == association }
15
+ end
16
+ end
17
+
18
+ def have_one(association)
19
+ return simple_matcher("model to have one #{association}") do |model|
20
+ model = model.class if model.is_a? ActiveRecord::Base
21
+ model.reflect_on_all_associations(:has_one).find { |a| a.name == association }
22
+ end
23
+ end
24
+
25
+ def have_and_belong_to_many(association)
26
+ return simple_matcher("model to have and belong to many #{association}") do |model|
27
+ model = model.class if model.is_a? ActiveRecord::Base
28
+ model.reflect_on_all_associations(:has_and_belongs_to_many).find { |a| a.name == association }
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,34 @@
1
+ module Spec
2
+ module Rails
3
+ module Matchers
4
+
5
+ class Observe
6
+ def initialize(expected_model_class)
7
+ @expected_model_class = expected_model_class
8
+ end
9
+
10
+ def matches?(observer)
11
+ @observer = observer
12
+ if @observer.is_a?(ActiveRecord::Observer)
13
+ @observer = @observer.class
14
+ end
15
+ @observed_classes = observer.observed_classes.flatten
16
+ @observed_classes.include?(@expected_model_class)
17
+ end
18
+
19
+ def failure_message
20
+ return "expected #{@observer.name} to observe #{@expected_model_class.name}, but it was not included in [#{@observed_classes.map(&:name).join(', ')}]"
21
+ end
22
+
23
+ def description
24
+ "observer to be observing #{@expected_model_class.name}"
25
+ end
26
+ end
27
+
28
+ def observe(expected_model_class)
29
+ Observe.new(expected_model_class)
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,56 @@
1
+ module Spec
2
+ module Rails
3
+ module Matchers
4
+ def validate_presence_of(attribute)
5
+ return simple_matcher("model to validate the presence of #{attribute}") do |model|
6
+ model.send("#{attribute}=", nil)
7
+ !model.valid? && model.errors.invalid?(attribute)
8
+ end
9
+ end
10
+
11
+ def validate_length_of(attribute, options)
12
+ if options.has_key? :within
13
+ min = options[:within].first
14
+ max = options[:within].last
15
+ elsif options.has_key? :is
16
+ min = options[:is]
17
+ max = min
18
+ elsif options.has_key? :minimum
19
+ min = options[:minimum]
20
+ elsif options.has_key? :maximum
21
+ max = options[:maximum]
22
+ end
23
+
24
+ return simple_matcher("model to validate the length of #{attribute} within #{min || 0} and #{max || 'Infinity'}") do |model|
25
+ invalid = false
26
+ if !min.nil? && min >= 1
27
+ model.send("#{attribute}=", 'a' * (min - 1))
28
+
29
+ invalid = !model.valid? && model.errors.invalid?(attribute)
30
+ end
31
+
32
+ if !max.nil?
33
+ model.send("#{attribute}=", 'a' * (max + 1))
34
+
35
+ invalid ||= !model.valid? && model.errors.invalid?(attribute)
36
+ end
37
+ invalid
38
+ end
39
+ end
40
+
41
+ def validate_uniqueness_of(attribute)
42
+ return simple_matcher("model to validate the uniqueness of #{attribute}") do |model|
43
+ model.class.stub!(:find).and_return(true)
44
+ !model.valid? && model.errors.invalid?(attribute)
45
+ end
46
+ end
47
+
48
+ def validate_confirmation_of(attribute)
49
+ return simple_matcher("model to validate the confirmation of #{attribute}") do |model|
50
+ model.send("#{attribute}_confirmation=", 'asdf')
51
+ !model.valid? && model.errors.invalid?(attribute)
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,102 @@
1
+ module Spec
2
+ module Rails
3
+ module Matchers
4
+ def have_form_posting_to(url_or_path)
5
+ return simple_matcher("have a form submitting via POST to '#{url_or_path}'") do |response|
6
+ have_tag("form[method=post][action=#{url_or_path}]").matches?(response)
7
+ end
8
+ end
9
+
10
+ def have_form_puting_to(url_or_path, id)
11
+ return simple_matcher("have a form submitting via PUT to '#{url_or_path}/#{id}'") do |response|
12
+ have_tag("form[method=post][action=#{url_or_path}/#{id}]").matches?(response)
13
+ have_tag("input[name=_method][type=hidden][value=put]").matches?(response)
14
+ end
15
+ end
16
+
17
+ def have_label_for(attribute, text)
18
+ return simple_matcher("have a label for '#{attribute}' with value of '#{text}'") do |response|
19
+ have_tag("label[for=#{attribute}]").matches?(response)
20
+ end
21
+ end
22
+
23
+ def with_label_for(attribute, text)
24
+ return simple_matcher("have a label for '#{attribute}' with value of '#{text}'") do |response|
25
+ with_tag("label[for=#{attribute}]").matches?(response)
26
+ end
27
+ end
28
+
29
+ def have_text_field_for(attribute)
30
+ return simple_matcher("have a text field for '#{attribute}'") do |response|
31
+ have_tag("input##{attribute}[type=text]").matches?(response)
32
+ end
33
+ end
34
+
35
+ def with_text_field_for(attribute)
36
+ return simple_matcher("with a text field for '#{attribute}'") do |response|
37
+ with_tag("input##{attribute}[type=text]").matches?(response)
38
+ end
39
+ end
40
+
41
+ def have_text_area_for(attribute)
42
+ return simple_matcher("have a text field for '#{attribute}'") do |response|
43
+ have_tag("textarea##{attribute}[type=text]").matches?(response)
44
+ end
45
+ end
46
+
47
+ def with_text_area_for(attribute)
48
+ return simple_matcher("have a text field for '#{attribute}'") do |response|
49
+ with_tag("textarea##{attribute}[type=text]").matches?(response)
50
+ end
51
+ end
52
+
53
+ def have_password_field_for(attribute)
54
+ return simple_matcher("have a password field for '#{attribute}'") do |response|
55
+ have_tag("input##{attribute}[type=password]").matches?(response)
56
+ end
57
+ end
58
+
59
+ def with_password_field_for(attribute)
60
+ return simple_matcher("have a password field for '#{attribute}'") do |response|
61
+ with_tag("input##{attribute}[type=password]").matches?(response)
62
+ end
63
+ end
64
+
65
+ def have_checkbox_for(attribute)
66
+ return simple_matcher("have a checkbox for '#{attribute}'") do |response|
67
+ have_tag("input##{attribute}[type=checkbox]").matches?(response)
68
+ end
69
+ end
70
+
71
+ def with_checkbox_for(attribute)
72
+ return simple_matcher("have a checkbox for '#{attribute}'") do |response|
73
+ with_tag("input##{attribute}[type=checkbox]").matches?(response)
74
+ end
75
+ end
76
+
77
+ def have_submit_button
78
+ return simple_matcher("have a submit button") do |response|
79
+ have_tag("input[type=submit]").matches?(response)
80
+ end
81
+ end
82
+
83
+ def with_submit_button
84
+ return simple_matcher("have a submit button") do |response|
85
+ with_tag("input[type=submit]").matches?(response)
86
+ end
87
+ end
88
+
89
+ def have_link_to(url_or_path, text = nil)
90
+ return simple_matcher("have a link to '#{url_or_path}'") do |response|
91
+ have_tag("a[href=#{url_or_path}]", text).matches?(response)
92
+ end
93
+ end
94
+
95
+ def with_link_to(url_or_path, text = nil)
96
+ return simple_matcher("have a link to '#{url_or_path}'") do |response|
97
+ with_tag("a[href=#{url_or_path}]", text).matches?(response)
98
+ end
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,24 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "rspec-rails-matchers"
3
+ s.version = "1.0.0"
4
+ s.date = "2009-01-14"
5
+ s.summary = "Rails-specific matchers for RSpec"
6
+ s.email = "jim@saturnflyer.com"
7
+ s.homepage = "http://github.com/saturnflyer/rspec-rails-matchers"
8
+ s.description = "Rspec-Rails-Matchers is a library of matchers for RSpec to make writing specs short and sweet."
9
+ s.has_rdoc = true
10
+ s.authors = ["Josh Knowles", "Bryan Helmkamp", "Matt Aimonetti"]
11
+ s.files = ["CHANGELOG",
12
+ "README",
13
+ "TODO",
14
+ "MIT-LICENSE",
15
+ "rspec-rails-matchers.gemspec",
16
+ "lib/spec/rails/matchers/associations.rb",
17
+ "lib/spec/rails/matchers/observers.rb",
18
+ "lib/spec/rails/matchers/validations.rb",
19
+ "lib/spec/rails/matchers/views.rb" ]
20
+ s.test_files = []
21
+ s.rdoc_options = []
22
+ s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
23
+ s.add_dependency("rspec-rails", ["> 1.1.12"])
24
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: saturnflyer-rspec-rails-matchers
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Josh Knowles
8
+ - Bryan Helmkamp
9
+ - Matt Aimonetti
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2009-01-14 00:00:00 -08:00
15
+ default_executable:
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: rspec-rails
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">"
23
+ - !ruby/object:Gem::Version
24
+ version: 1.1.12
25
+ version:
26
+ description: Rspec-Rails-Matchers is a library of matchers for RSpec to make writing specs short and sweet.
27
+ email: jim@saturnflyer.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ - README.txt
36
+ files:
37
+ - CHANGELOG
38
+ - README
39
+ - TODO
40
+ - MIT-LICENSE
41
+ - rspec-rails-matchers.gemspec
42
+ - lib/spec/rails/matchers/associations.rb
43
+ - lib/spec/rails/matchers/observers.rb
44
+ - lib/spec/rails/matchers/validations.rb
45
+ - lib/spec/rails/matchers/views.rb
46
+ - History.txt
47
+ - Manifest.txt
48
+ - README.txt
49
+ has_rdoc: true
50
+ homepage: http://github.com/saturnflyer/rspec-rails-matchers
51
+ post_install_message:
52
+ rdoc_options: []
53
+
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ version:
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ version:
68
+ requirements: []
69
+
70
+ rubyforge_project:
71
+ rubygems_version: 1.2.0
72
+ signing_key:
73
+ specification_version: 2
74
+ summary: Rails-specific matchers for RSpec
75
+ test_files: []
76
+