-
-
Notifications
You must be signed in to change notification settings - Fork 910
/
Copy pathindex.html
320 lines (319 loc) · 13.3 KB
/
index.html
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta content='Shoulda Matchers Gem' name='description'>
<meta content='Ruby,Gem' name='keywords'>
<meta content='Shoulda Matchers' name='author'>
<meta content='IE=edge,chrome=1' http-equiv='X-UA-Compatible'>
<title>Shoulda Matchers</title>
<link href="/assets/stylesheets/bundle-7326c06b.css" rel="stylesheet" />
<script src='//use.typekit.net/wch8lhz.js'></script>
<script>
try {
Typekit.load();
} catch(e) {
// don't worry, be happy
}
</script>
</head>
<body class='index'>
<nav>
<div class='wrapper'>
<div class='logo'></div>
<a class='btn-github' href='https://github.com/thoughtbot/shoulda-matchers' target='_blank'>GitHub</a>
<a class='btn-docs' href='docs'>Docs</a>
</div>
</nav>
<section class='main-header'>
<div class='wrapper'>
<div class='main-header-copy'>
<h1 class='subheader'>Simple One-Liner Tests for Rails</h1>
</div>
<div class='browser-wrapper'>
<div class='browser-container animation'>
<h4 class='subheader'>Without Shoulda Matchers</h4>
<div class='browser-bar'>
<div class='browser-button'></div>
<div class='browser-button'></div>
<div class='browser-button'></div>
<div class='emoji-thumb-down'></div>
</div>
<div class='browser-background'>
<pre><code class='language-ruby'>describe User do
 it 'is not valid if its username is the same as another user within the same account' do
 _existing_user = FactoryBot.create(:user,
 username: 'johnsmith',
 account_id: 1
 )
 user = FactoryBot.build(:user,
 username: 'johnsmith',
 account_id: 1
 )
 expect(user).not_to be_valid
 end

 it 'is valid if its username is the same as another user within the same account, but for different case' do
 _existing_user = FactoryBot.create(:user,
 username: 'johnsmith',
 account_id: 1
 )
 user = FactoryBot.build(:user,
 username: 'JohnSmith',
 account_id: 1
 )
 expect(user).to be_valid
 end
end</code></pre>
</div>
</div>
</div>
<div class='browser-wrapper'>
<div class='browser-container animation'>
<h4 class='subheader'>With Shoulda Matchers</h4>
<div class='browser-bar'>
<div class='browser-button'></div>
<div class='browser-button'></div>
<div class='browser-button'></div>
<div class='emoji-thumb'></div>
</div>
<div class='browser-background'>
<pre><code class='language-ruby'>describe User do
 context 'validations' do
 before { FactoryBot.build(:user) }

 it do
 should validate_uniqueness_of(:username).
 scoped_to(:account_id).
 case_insensitive
 end
 end
end
</code></pre>
</div>
</div>
</div>
</div>
</section>
<section class='benefits'>
<div class='wrapper'>
<div class='benefit'>
<img src="/assets/images/clock-icon-08020d9f.svg" class="icon" data-id="animate-when-visible" alt="Clock icon" />
<h4>Save Time</h4>
<p>Spend less time writing long, complex, and error-prone tests</p>
</div>
<div class='benefit'>
<img src="/assets/images/write-icon-a8fbe02a.svg" class="icon" data-id="animate-when-visible" alt="Write icon" />
<h4>Write More Tests</h4>
<p>Test thoroughly by using over 30 pre-existing matchers, developed over time</p>
</div>
<div class='benefit'>
<img src="/assets/images/check-icon-70a7811f.svg" class="icon" data-id="animate-when-visible" alt="Check icon" />
<h4>More Readable Results</h4>
<p>Get clear, readable, and actionable messages from the tests you run</p>
</div>
</div>
</section>
<section class='examples-wrapper'>
<div class='wrapper'>
<h2 class='section-header'>
Extensive matchers for ActiveModel, ActiveRecord, and ActionController
</h2>
<div class='vertical-tabs-container' data-id='tabs-container'>
<div class='vertical-tabs'>
<a class='vertical-tab is-active' data-id='sidebar-tab' href='#tab1'>ActiveModel validation matchers</a>
<a class='vertical-tab' data-id='sidebar-tab' href='#tab2'>Other ActiveModel matchers</a>
<a class='vertical-tab' data-id='sidebar-tab' href='#tab3'>ActiveRecord validation matchers</a>
<a class='vertical-tab' data-id='sidebar-tab' href='#tab4'>ActiveRecord association matchers</a>
<a class='vertical-tab' data-id='sidebar-tab' href='#tab5'>ActionController matchers</a>
<a class='vertical-tab' data-id='sidebar-tab' href='#tab6'>Independent matchers</a>
</div>
<div class='vertical-tab-content-container'>
<div class='vertical-tab-accordion' id='tab1'>
<a class='vertical-tab-accordion-heading is-active' data-id='accordion-tab' href='#tab1'>
ActiveModel validation matchers
</a>
<div class='vertical-tab-content is-active' data-id='tab-content'>
<p>
Example:
<code>validate_presence_of</code>
matcher
</p>
<div class='code-sample'>
<div class='filename'>Post model</div>
<pre><code class='language-ruby'>class Robot
 include ActiveModel::Model
 attr_accessor :arms

 validate :arms, presence: true
end</code></pre>
</div>
<div class='code-sample'>
<div class='filename'>Test using RSpec</div>
<pre><code class='language-ruby'>describe Robot do
 it { should validate_presence_of(:arms) }
end</code></pre>
</div>
<div class='code-sample'>
<div class='filename'>Test using Shoulda Context</div>
<pre><code class='language-ruby'>class RobotTest < ActiveSupport::TestCase
 should validate_presence_of(:arms)
end</code></pre>
</div>
</div>
</div>
<div class='vertical-tab-accordion' id='tab2'>
<a class='vertical-tab-accordion-heading' data-id='accordion-tab' href='#tab2'>
Other ActiveModel matchers
</a>
<div class='vertical-tab-content' data-id='tab-content'>
<p>
Example:
<code>have_secure_password</code>
matcher
</p>
<div class='code-sample'>
<div class='filename'>Post model</div>
<pre><code class='language-ruby'>class User
 include ActiveModel::Model
 include ActiveModel::SecurePassword
 attr_accessor :password

 has_secure_password
end</code></pre>
</div>
<div class='code-sample'>
<div class='filename'>Test using RSpec</div>
<pre><code class='language-ruby'>describe User do
 it { should have_secure_password }
end</code></pre>
</div>
<div class='code-sample'>
<div class='filename'>Test using Shoulda Context</div>
<pre><code class='language-ruby'>class UserTest < ActiveSupport::TestCase
 should have_secure_password
end</code></pre>
</div>
</div>
</div>
<div class='vertical-tab-accordion' id='tab3'>
<a class='vertical-tab-accordion-heading is-active' data-id='accordion-tab' href='#tab3'>
ActiveRecord validation matchers
</a>
<div class='vertical-tab-content' data-id='tab-content'>
<p>
Example:
<code>validate_uniqueness_of</code>
matcher
</p>
<div class='code-sample'>
<div class='filename'>Post model</div>
<pre><code class='language-ruby'>class Post < ActiveRecord::Base
 validates_uniqueness_of :slug,
 scope: :user_id,
 message: 'duplicate slug within same user_id',
 case_insensitive: true
end</code></pre>
</div>
<div class='code-sample'>
<div class='filename'>Test using RSpec</div>
<pre><code class='language-ruby'>describe Post do
 context 'validations' do
 subject { FactoryBot.build(:post) }

 it do
 should validate_uniqueness_of(:slug).
 scoped_to(:user_id).
 with_message('duplicate slug within same user_id').
 case_insensitive
 end
 end
end</code></pre>
</div>
<div class='code-sample'>
<div class='filename'>Test using Shoulda Context</div>
<pre><code class='language-ruby'>class PostTest < ActiveSupport::TestCase
 context 'validations' do
 subject { FactoryBot.build(:post) }

 should validate_uniqueness_of(:slug).
 scoped_to(:user_id).
 with_message('duplicate slug within same user_id').
 case_insensitive
 end
end</code></pre>
</div>
</div>
</div>
<div class='vertical-tab-accordion' id='tab4'>
<a class='vertical-tab-accordion-heading' data-id='accordion-tab' href='#tab4'>
ActiveRecord association matchers
</a>
<div class='vertical-tab-content' data-id='tab-content'>
<p>
Example:
<code>have_many</code>
matcher
</p>
<div class='code-sample'>
<div class='filename'>Post model</div>
<pre><code class='language-ruby'>class Person < ActiveRecord::Base
 has_many :acquaintances,
 through: :friends,
 class_name: 'Person'
end</code></pre>
</div>
<div class='code-sample'>
<div class='filename'>Test using RSpec</div>
<pre><code class='language-ruby'>describe Person do
 it do
 should have_many(:acquaintances).
 through(:friends).
 class_name('Person')
 end
end</code></pre>
</div>
<div class='code-sample'>
<div class='filename'>Test using Shoulda Context</div>
<pre><code class='language-ruby'>require 'test_helper'

class PersonTest < ActiveSupport::TestCase
 should have_many(:acquaintances).
 through(:friends).
 class_name('Person')
end</code></pre>
</div>
</div>
</div>
<div class='vertical-tab-accordion' id='tab5'>
<a class='vertical-tab-accordion-heading' data-id='accordion-tab' href='#tab5'>
ActionController matchers
</a>
<div class='vertical-tab-content' data-id='tab-content'>
<p>
Example:
<code>rescue_from</code>
matcher
</p>
<div class='code-sample'>
<div class='filename'>Routes</div>
<pre><code class='language-ruby'>class ApplicationController < ActionController::Base
 rescue_from ActiveRecord::RecordNotFound, with: :render_not_found

 private

 def render_not_found
 # ...
 end
end</code></pre>
</div>
<div class='code-sample'>
<div class='filename'>Test using RSpec</div>
<pre><code class='language-ruby'>describe ApplicationController do
 it do
 should rescue_from(ActiveRecord::RecordNotFound).
 with(:render_not_found)
 end
end</code></pre>
</div>
<div class='code-sample'>
<div class='filename'>Test using Shoulda Context</div>
<pre><code class='language-ruby'>class ApplicationControllerTest < ActionController::TestCase
 should rescue_from(ActiveRecord::RecordNotFound).
 with(:render_not_found)
end</code></pre>
</div>
</div>
</div>
<div class='vertical-tab-accordion' id='tab6'>
<a class='vertical-tab-accordion-heading' data-id='accordion-tab' href='#tab6'>
Independent matchers
</a>
<div class='vertical-tab-content' data-id='tab-content'>
<p>
Example:
<code>delegate_method</code>
matcher
</p>
<div class='code-sample'>
<div class='filename'>app/models/courier.rb</div>
<pre><code class='language-ruby'>require 'forwardable'

class Courier
 extend Forwardable

 attr_reader :post_office

 def_delegators :post_office, :deliver

 def initialize
 @post_office = PostOffice.new
 end
end</code></pre>
</div>
<div class='code-sample'>
<div class='filename'>Test using RSpec</div>
<pre><code class='language-ruby'>describe Courier do
 it { should delegate_method(:deliver).to(:post_office) }
end</code></pre>
</div>
<div class='code-sample'>
<div class='filename'>Test using Shoulda Context</div>
<pre><code class='language-ruby'>class CourierTest < Minitest::Test
 should delegate_method(:deliver).to(:post_office)
end
</code></pre>
</div>
</div>
</div>
</div>
</div>
</div>
</section>
<section class='wrapper'>
<h2 class='section-header'>List of Matchers</h2>
<div class='list-wrapper'>
<h4>ActiveModel</h4>
<li>allow_value</li>
<li>have_secure_password</li>
<li>validate_absence_of</li>
<li>validate_acceptance_of</li>
<li>validate_confirmation_of</li>
<li>validate_exclusion_of</li>
<li>validate_inclusion_of</li>
<li>validate_length_of</li>
<li>validate_numericality_of</li>
<li>validate_presence_of</li>
</div>
<div class='list-wrapper'>
<h4>ActiveRecord</h4>
<li>accept_nested_attributes_for</li>
<li>belong_to</li>
<li>define_enum_for</li>
<li>have_and_belong_to_many</li>
<li>have_db_column</li>
<li>have_db_index</li>
<li>have_implicit_order_column</li>
<li>have_many</li>
<li>have_many_attached</li>
<li>have_one</li>
<li>have_one_attached</li>
<li>have_readonly_attribute</li>
<li>have_rich_text</li>
<li>serialize</li>
<li>validate_uniqueness_of</li>
</div>
<div class='list-wrapper'>
<h4>ActionController</h4>
<li>filter_param</li>
<li>permit</li>
<li>redirect_to</li>
<li>render_template</li>
<li>render_with_layout</li>
<li>rescue_from</li>
<li>respond_with</li>
<li>route</li>
<li>set_session</li>
<li>set_flash</li>
<li>use_after_action</li>
<li>use_around_action</li>
<li>use_before_action</li>
</div>
<div class='list-wrapper'>
<h4>Independent Matchers</h4>
<li>delegate_method</li>
</div>
</section>
<footer>
<div class='wrapper'>
<div class='footer-buttons'>
<a class='btn-github' href='https://github.com/thoughtbot/shoulda-matchers#installation' target='_blank'>
Get full installation instructions on GitHub
</a>
</div>
<p>
Shoulda Matchers is maintained and funded by thoughtbot, inc. Tweet your
questions or suggestions to <a href='https://twitter.com/mcmire'
target='_blank'>@mcmire</a> or <a href='https://twitter.com/thoughtbot'
target='_blank'>@thoughtbot</a>
</p>
<p>
Copyright © 2014-2023 thoughtbot, inc.
</p>
</div>
</footer>
<script src="/assets/javascripts/bundle-4fb9d0d0.js"></script>
</body>
</html>