forked from betterspecs/betterspecs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjp.html
1372 lines (1063 loc) · 52.3 KB
/
jp.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: 'Better Specs(日本語)'
---
<% content_for :menu do %>
<li><a href="/jp/">rspec ガイドラインの紹介</a></li>
<li class="little">» <a href="#describe">メソッドの説明をする</a></li>
<li class="little">» <a href="#contexts">Contextsを使う</a></li>
<li class="little">» <a href="#short">説明を短く</a></li>
<li class="little">» <a href="#single">単一条件テスト</a></li>
<li class="little">» <a href="#all">可能な限り全部をテスト</a></li>
<li class="little">» <a href="#expect">Expect対Should</a></li>
<li class="little">» <a href="#subject">Subjectを使う</a></li>
<li class="little">» <a href="#let">letとlet!を使う</a></li>
<li class="little">» <a href="#mock">Mockを使うか使わないか</a></li>
<li class="little">» <a href="#data">必要なデータだけ作る</a></li>
<li class="little">» <a href="#factories">fixtureの代わりにfactoryを使う</a></li>
<li class="little">» <a href="#matchers">読みやすいmatcherを使う</a></li>
<li class="little">» <a href="#sharedexamples">Shared examples</a></li>
<li class="little">» <a href="#integration">見えるものをテスト</a></li>
<li class="little">» <a href="#should">shouldを使わない</a></li>
<li class="little">» <a href="#guard">guardを使ったテスト自動化</a></li>
<li class="little">» <a href="#spork">sporkを使ったテスト高速化</a></li>
<li class="little">» <a href="#http">HTTP requestをmockする</a></li>
<li class="little">» <a href="#formatter">有用なformatter</a></li>
<li><a href="#books">Books</a></li>
<li><a href="#presentations">Presentations</a></li>
<li><a href="#resources">Resources on the web</a></li>
<li><a href="#libraries">Libraries</a></li>
<li><a href="#styleguide">Styleguide</a></li>
<li><a href="#improving">Improving Better Specs</a></li>
<li><a href="#credits">Credits</a></li>
<li><a href="#help">Help us</a></li>
<% end %>
<div class="content">
<article>
<aside class="menu">
<h1>Affiliate books</h1>
<%= render "partials/books" %>
</aside>
</article>
<article>
<iframe src="http://ghbtns.com/github-btn.html?user=andreareginato&repo=betterspecs&type=watch&count=true&size=large"
allowtransparency="true" frameborder="0" scrolling="0" width="170px" height="30px"></iframe>
<iframe src="http://ghbtns.com/github-btn.html?user=andreareginato&repo=betterspecs&type=fork&count=true&size=large"
allowtransparency="true" frameborder="0" scrolling="0" width="170px" height="30px"></iframe>
<iframe src="http://ghbtns.com/github-btn.html?user=andreareginato&repo=betterspecs&type=follow&count=true&size=large"
allowtransparency="true" frameborder="0" scrolling="0" width="300px" height="30px"></iframe>
</article>
</br>
<article>
<!-- <p>
<a href="https://www.relishapp.com/rspec/">RSpec</a> is a great tool in the behavior-driven development (BDD) process of writing human readable specifications that direct and validate
the development of your application.</p> -->
<p>
<a href="https://www.relishapp.com/rspec/">RSpec</a>とはBDDで使う素晴らしいツールです。(BDDとはbehavior-driven developmentの略で方向性を人の読める仕様に沿って開発を行う開発方法論です。)
</p>
<!-- <p>
On the web there are many resources that give complete overview of _what_ you can do with RSpec.
But there are fewer resources devoted to how to create a great RSpec test suite.<p> -->
<p>
ウェブで既に沢山の使い方や _何_ ができるかを説明したRSpecの書き込みを探せますが、RSpecで良いテストの作り方を説明した書き込みはなかなか探せません。
<p>
<!-- <p>
Better Specs tries to fill this gap by collecting most of the "best practices" that other
developers learn through years of experience.</p> -->
<p>
Better Specsは大抵のガイドラインの書いてない部分を集めようとしました。- これは開発者達の経験を通して学んだ方法です。
</p>
</article>
<article>
<!-- <h1><a name="describe">How to describe your methods</a></h1> -->
<h1><a name="describe">メソッドの説明をする</a></h1>
<!-- <p>
Be clear about what method you are describing. For instance, use the Ruby documentation convention of
<code>.</code> (or <code>::</code>) when referring to a class method's name and <code>#</code> when
referring to an instance method's name.
</p> -->
<p>
作成中のメソッドを明らかにしましょう。例えば、Ruby文書の規約ではクラスメソッドの名前には<code>.</code>(もしくは<code>::</code>)をインスタンスメソッドの名前には<code>#</code>を使っています。
</p>
<p class="wrong">bad</p>
<div>
<pre><code class="ruby">describe 'the authenticate method for User' do
describe 'if the user is an admin' do
</code></pre>
</div>
<p class="correct">good</p>
<div>
<pre><code class="ruby">describe '.authenticate' do
describe '#admin?' do
</code></pre>
</div>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/2">このガイドに関して議論する →</a>
</p>
<%= render "partials/share", text: "betterspecs.org | メソッドの説明する", url: "http://betterspecs.org/#describe" %>
</article>
<article>
<!-- <h1><a name="contexts">Use contexts</a></h1> -->
<h1><a name="contexts">Contextsを使う</a></h1>
<!-- <p>
Contexts are a powerful method to make your tests clear and well organized.
In the long term this practice will keep tests easy to read.
</p> -->
<p>
Contextsはテストを明らかにし、まとめる素晴らしい方法です。
長い目で見ると、この方法はテストを読みやすくします。
</p>
<p class="wrong">bad</p>
<div>
<pre><code class="ruby">it 'has 200 status code if logged in' do
expect(response).to respond_with 200
end
it 'has 401 status code if not logged in' do
expect(response).to respond_with 401
end
</code></pre>
</div>
<p class="correct">good</p>
<div>
<pre><code class="ruby">context 'when logged in' do
it { is_expected.to respond_with 200 }
end
context 'when logged out' do
it { is_expected.to respond_with 401 }
end
</code></pre>
</div>
<!-- <p>
When describing a context, start its description with "when" or "with".
</p> -->
<p>
Contextsでは"when"もしくは"with"で説明し始めましょう。
</p>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/3">このガイドに関して議論する →</a>
</p>
<%= render "partials/share", text: "betterspecs.org | Contextsを使う", url: "http://betterspecs.org/#contexts" %>
</article>
<article>
<!-- <h1><a name="short">Keep your description short</a></h1> -->
<h1><a name="short">説明を短く</a></h1>
<!-- <p>
A spec description should never be longer than 40 characters. If this happens
you should split it using a context.
</p> -->
<p>
specの説明は40文字を超えないようにしましょう。超えた場合はcontextを分けてください。
</p>
<p class="wrong">bad</p>
<div>
<pre><code class="ruby">it 'has 422 status code if an unexpected params will be added' do
</code></pre>
</div>
<p class="correct">good</p>
<div>
<pre><code class="ruby">context 'when not valid' do
it { should respond_with 422 }
end
</code></pre>
</div>
<!-- <p>
In the example we removed the description related to the status code,
which has been replaced by the expectation <code>it { should respond_with 422 }</code>.
If you run this test typing <code>rspec filename</code> you will obtain a readable output.
</p> -->
<p>
この例では<code>it { should respond_with 422 }</code>と書いてステイタスの説明を削除しました。
<code>rspec filename</code>でテストを実行すると相変わらず読める出力を取得できます。
</p>
<p class="base">Formatted Output</p>
<div>
<pre><code>when not valid
it should respond with 422
</code></pre>
</div>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/4">このガイドに関して議論する →</a>
</p>
<%= render "partials/share", text: "betterspecs.org | 説明を短く", url: "http://betterspecs.org/#short" %>
</article>
<article>
<!-- <h1><a name="single">Single expectation test</a></h1> -->
<h1><a name="single">単一条件テスト</a></h1>
<!-- <p>
The 'one expectation' tip is more broadly expressed as 'each test should make only one assertion'.
This helps you on finding possible errors, going directly to the failing test, and to make your
code readable.
</p> -->
<p>
'単一条件'は'各テストは一つだけ確認すべき'という表現でより広く知られています。
これはエラーを探しやすくし、失敗するテストをすぐ見つけるようにし、コードを読みやすくします。
</p>
<!-- <p>
In isolated unit specs, you want each example to specify one (and only one) behavior. Multiple
expectations in the same example are a signal that you may be specifying multiple behaviors.
</p> -->
<p>
独立したユニットでは、各例はただ一つの振る舞いだけテストするのが望ましいです。例の中で多数のテストが有るのは幾つかの振る舞いに分離する必要があることを示しています。
</p>
<!-- <p>
Anyway, in tests that are not isolated (e.g. ones that integrate with a DB, an external webservice,
or end-to-end-tests), you take a massive performance hit to do the same setup over and over
again, just to set a different expectation in each test. In these sorts of slower tests, I
think it's fine to specify more than one isolated behavior.
</p> -->
<p>
しかし、分離できないテストで(例えばDBや外部システムとの連動、前後があるテストの場合)分離するだけでは同じセットアップを何回も行い、テストが重くなる現象が現れます。こういった重いテストは分けなくても良いでしょう。
</p>
<p class="correct">good (isolated)</p>
<div>
<pre><code class="ruby">it { should respond_with_content_type(:json) }
it { should assign_to(:resource) }
</code></pre>
</div>
<p class="correct">Good (not isolated)</p>
<div>
<pre><code class="ruby">it 'creates a resource' do
expect(response).to respond_with_content_type(:json)
expect(response).to assign_to(:resource)
end
</code></pre>
</div>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/5">このガイドに関して議論する →</a>
</p>
<%= render "partials/share", text: "betterspecs.org | 単一条件テスト", url: "http://betterspecs.org/#single" %>
</article>
<article>
<!-- <h1><a name="all">Test all possible cases</a></h1> -->
<h1><a name="all">可能な限り全部をテスト</a></h1>
<!-- <p>
Testing is a good practice, but if you do not test the edge cases, it will not
be useful. Test valid, edge and invalid case. For example, consider the following action.
</p> -->
<p>
テストはやった方がいいですが、全ケースをテストしないと、有用とはいえません。有効な場合と無効な場合を全部テストしましょう。例えばこんなアクションが有るとしましょう。
</p>
<p class="base">Destroy action</p>
<div>
<pre><code class="ruby">before_filter :find_owned_resources
before_filter :find_resource
def destroy
render 'show'
@consumption.destroy
end</code></pre>
</div>
<!-- <p>
The error I usually see lies in testing only whether the resource has been removed.
But there are at least two edge cases: when the resource is not found and when it's not
owned. As a rule of thumb think of all the possible inputs and test them.
</p> -->
<p>
普段よく見るエラーで、モデルがうまく削除できたかだけテストしています。
が、少なくとも二つのエッジケースが存在します:モデルを探せなかった時と権限がない時です。
すべての一般的に可能な入力を考えてテストしましょう。
</p>
<p class="wrong">bad</p>
<div>
<pre><code class="ruby">it 'shows the resource'
</code></pre>
</div>
<p class="correct">good</p>
<div class="correct">
<pre><code class="ruby">describe '#destroy' do
context 'when resource is found' do
it 'responds with 200'
it 'shows the resource'
end
context 'when resource is not found' do
it 'responds with 404'
end
context 'when resource is not owned' do
it 'responds with 404'
end
end
</code></pre>
</div>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/6">このガイドに関して議論する →</a>
</p>
<%= render "partials/share", text: "betterspecs.org | 可能な限り全部テスト", url: "http://betterspecs.org/#all" %>
</article>
<article>
<!-- <h1><a name="expect">Expect vs Should syntax</a></h1> -->
<h1><a name="expect">Expect対Should</a></h1>
<p>
<!-- On new projects always use the <code>expect</code> syntax. -->
新しいプロジェクトでは<code>expect</code>だけ使いましょう。
</p>
<p class="wrong">bad</p>
<div>
<pre><code class="ruby">it 'creates a resource' do
response.should respond_with_content_type(:json)
end</code></pre>
</div>
<p class="correct">good</p>
<div>
<pre><code class="ruby">it 'creates a resource' do
expect(response).to respond_with_content_type(:json)
end</code></pre>
</div>
<!-- <p>Configure the RSpec to only accept the new syntax on new projects, to avoid have the 2 syntax all over the place.</p> -->
<p>全体で新しい文法だけ許容し、RSpec2の文法を使えなくなる様に設定できます。</p>
<p class="correct">good</p>
<div>
<pre><code class="ruby"># spec_helper.rb
RSpec.configure do |config|
# ...
config.expect_with :rspec do |c|
c.syntax = :expect
end
end</code></pre>
</div>
<!-- <p>More about the new RSpec expectation syntax can be found <a href="http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax">here</a> and <a href="http://myronmars.to/n/dev-blog/2013/07/the-plan-for-rspec-3#what_about_the_old_expectationmock_syntax">here</a>.</p> -->
<p>RSpec expectation文法に対するもっと詳しい情報は<a href="http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax">ここ</a>と<a href="http://myronmars.to/n/dev-blog/2013/07/the-plan-for-rspec-3#what_about_the_old_expectationmock_syntax">ここ</a>をご覧ください。</p>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/83">このガイドに関して議論する →</a>
</p>
<%= render "partials/share", text: "betterspecs.org | Expect対Should", url: "http://betterspecs.org/#expect" %>
</article>
<article>
<!-- <h1><a name="subject">Use subject</a></h1> -->
<h1><a name="subject">Subjectを使う</a></h1>
<!-- <p>
If you have several tests related to the same subject use
<code>subject{}</code> to DRY them up.
</p> -->
<p>
もしも、同じsubjectに対して複数のテストをしていたら、<code>subject{}</code>を使ってDRYしましょう。
</p>
<p class="wrong">bad</p>
<div>
<pre><code class="ruby">it { expect(assigns('message')).to match /it was born in Belville/ }
</code></pre>
</div>
<p class="correct">good</p>
<div>
<pre><code class="ruby">subject { assigns('message') }
it { should match /it was born in Billville/ }
</code></pre>
</div>
<!-- <p>
RSpec has also the ability to use a named subject.
</p> -->
<p>
RSpecでは名前付きのsubjectも使えます。
</p>
<p class="correct">Good</p>
<div>
<pre><code class="ruby">subject(:hero) { Hero.first }
it "carries a sword" do
expect(hero.equipment).to include "sword"
end
</code></pre>
</div>
<!-- <p>
Learn more about <a href="https://www.relishapp.com/rspec/rspec-core/v/2-11/docs/subject">rspec subject</a>.
</p> -->
<p>
<a href="https://www.relishapp.com/rspec/rspec-core/v/2-11/docs/subject">rspec subject</a>に関してもっと学ぶ。
</p>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/7">このガイドに関して議論する →</a>
</p>
<%= render "partials/share", text: "betterspecs.org | Subjectを使う", url: "http://betterspecs.org/#subject" %>
</article>
<article>
<!-- <h1><a name="let">Use let and let!</a></h1> -->
<h1><a name="let">letとlet!を使う</a></h1>
<!-- <p>
When you have to assign a variable instead of using a <code>before</code> block to create
an instance variable, use <code>let</code>. Using <code>let</code> the variable lazy loads
only when it is used the first time in the test and get cached until that specific test is
finished. A really good and deep description of what <code>let</code> can be found in this
<a href="http://stackoverflow.com/questions/5359558/when-to-use-rspec-let/5359979#5359979">stackoverflow answer</a>.</p> -->
<p>
変数に値を入れる必要がある時は<code>before</code>ブロックの代わりに<code>let</code>を使いましょう。 <code>let</code>を使えば変数が初めて使用された時だけlazy loadしてspecテストが終わるまでキャッシュとして使えます。
この<a href="http://stackoverflow.com/questions/5359558/when-to-use-rspec-let/5359979#5359979">stackoverflowの答</a>に<code>let</code>に関しての詳しい説明が有ります。
</p>
<p class="wrong">bad</p>
<div>
<pre><code class="ruby">describe '#type_id' do
before { @resource = FactoryGirl.create :device }
before { @type = Type.find @resource.type_id }
it 'sets the type_id field' do
expect(@resource.type_id).to equal(@type.id)
end
end
</code></pre>
</div>
<p class="correct">good</p>
<div>
<pre><code class="ruby">describe '#type_id' do
let(:resource) { FactoryGirl.create :device }
let(:type) { Type.find resource.type_id }
it 'sets the type_id field' do
expect(resource.type_id).to equal(type.id)
end
end
</code></pre>
</div>
<!-- <p>
Use <code>let</code> to initialize actions that are lazy loaded to test your specs.
</p> -->
<p>
<code>let</code>を初期化に使うとspecをテストする時にlazy loadされます。
</p>
<p class="correct">good</p>
<div>
<pre><code class="ruby">context 'when updates a not existing property value' do
let(:properties) { { id: Settings.resource_id, value: 'on'} }
def update
resource.properties = properties
end
it 'raises a not found error' do
expect { update }.to raise_error Mongoid::Errors::DocumentNotFound
end
end
</code></pre>
</div>
<!-- <p>
Use <code>let!</code> if you want to define the variable when the block is defined.
This can be useful to populate your database to test queries or scopes.
</p> -->
<p>
ブロックが定義した時に変数を定義したい時は<code>let!</code>を使いましょう。
これはDBのクエリーやスコープのテストで有用です。
</p>
<!-- <p>
Here an example of what let actually is.
</p> -->
<p>
これはletの実体の説明です。
</p>
<p class="correct">good</p>
<div>
<pre><code class="ruby"># this:
let(:foo) { Foo.new }
# is very nearly equivalent to this:
def foo
@foo ||= Foo.new
end
</code></pre>
</div>
<p>
<!-- <p>
Learn more about <a href="https://www.relishapp.com/rspec/rspec-core/v/2-11/docs/helper-methods/let-and-let">rspec let</a>.
</p> -->
<p>
<a href="https://www.relishapp.com/rspec/rspec-core/v/2-11/docs/helper-methods/let-and-let">rspec let</a>に関してもっと学ぶ
</p>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/8">このガイドに関して議論する →</a>
</p>
<%= render "partials/share", text: "betterspecs.org | letとlet!を使う", url: "http://betterspecs.org/#let" %>
</article>
<article>
<!-- <h1><a name="mock">Mock or not to mock</a></h1> -->
<h1><a name="mock">Mockを使うか使わないか</a></h1>
<!-- <p>
There's a debate going on. Do not (over)use mocks and test real behavior when possible.
Testing real cases are useful when updating your application flow.
</p> -->
<p>
まだ議論中のものです。出来るだけ(過度に)mockを使わずに実際の振る舞いをテストしましょう。
実際のケースをテストするのはロジックを変更する際に有用です。
</p>
<p class="correct">good</p>
<div>
<pre><code class="ruby"># simulate a not found resource
context "when not found" do
before { allow(Resource).to receive(:where).with(created_from: params[:id]).and_return(false) }
it { should respond_with 404 }
end
</code></pre>
</div>
<!-- <p>
Mocking makes your specs faster but they are difficult to use. You need to understand them
well to use them well. Read more <a="http://myronmars.to/n/dev-blog/2012/06/thoughts-on-mocking">about</a>.
</p> -->
<p>
mockはspecを早くしますが正しく使うのが難しいです。mockをうまく使うにはもっとmockを理解する必要があります。<a href="http://myronmars.to/n/dev-blog/2012/06/thoughts-on-mocking">この書き込み</a>をご覧ください。
</p>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/9">このガイドに関して議論する →</a>
</p>
<%= render "partials/share", text: "betterspecs.org | Mockを使うか使わないか", url: "http://betterspecs.org/#mock" %>
</article>
<article>
<!-- <h1><a name="data">Create only the data you need</a></h1> -->
<h1><a name="data">必要なデータだけ作る</a></h1>
<!-- <p>
If you have ever worked in a medium size project (but also in small ones), test suites
can be heavy to run. To solve this problem, it's important not to load more data than needed.
Also, if you think you need dozens of records, you are probably wrong.
</p> -->
<p>
中小規模のプロジェクトの経験しかないと、テストスイートは重くなりがちです。必要以上のデータをロードしないようにしましょう。多数のデータが必要だと考えるならば、何かが間違っている可能性があります。
</p>
<p class="correct">good</p>
<div>
<pre><code class="ruby">describe "User"
describe ".top" do
before { FactoryGirl.create_list(:user, 3) }
it { expect(User.top(2)).to have(2).item }
end
end
</code></pre>
</div>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/10">このガイドに関して議論する →</a>
</p>
<%= render "partials/share", text: "betterspecs.org | 必要なテータだけ作る", url: "http://betterspecs.org/#data" %>
</article>
<article>
<!-- <h1><a name="factories">Use factories and not fixtures</a></h1> -->
<h1><a name="factories">fixtureの代わりにfactoryを使う</a></h1>
<!-- <p>
This is an old topic, but it's still good to remember it. Do not use fixtures because they are
difficult to control, use factories instead. Use them to reduce the verbosity
on creating new data.
</p> -->
<p>
これは古い話ですが、話しておく価値があります。fixtureは使わないでください。なぜならfixtureは操作が難しいからです。代わりにfactoryを使いましょう。factoryを使うと新しいデータを生成する際のコードの量を減らせます。
</p>
<p class="wrong">bad</p>
<div>
<pre><code class="ruby">user = User.create(
name: 'Genoveffa',
surname: 'Piccolina',
city: 'Billyville',
birth: '17 Agoust 1982',
active: true
)
</code></pre>
</div>
<p class="correct">good</p>
<div>
<pre><code class="ruby">user = FactoryGirl.create :user
</code></pre>
</div>
<!--<p>
One important note. When talking about unit tests the best practice would be to use neither
fixtures or factories. Put as much of your domain logic in libraries that can be tested without
needing complex, time consuming setup with either factories or fixtures. Read more in
<a href="http://blog.steveklabnik.com/posts/2012-07-14-why-i-don-t-like-factory_girl">this article</a>
</p>-->
<p>
注意!この文書でのユニットテストはfixtureもfactoryも使っておりません。
複雑なfixtureやfactoryを作る時間を節約すると、ライブラリにロジックを追加する時間を稼げます。
<a href="http://blog.steveklabnik.com/posts/2012-07-14-why-i-don-t-like-factory_girl">この書き込み</a>を読んでみましょう。
</p>
<!-- <p>
Learn more about <a href="https://github.com/thoughtbot/factory_girl">Factory Girl</a>.
</p> -->
<p>
<a href="https://github.com/thoughtbot/factory_girl">Factory Girl</a>をもっと学ぶ。
</p>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/11">このガイドに関して議論する →</a>
</p>
<%= render "partials/share", text: "betterspecs.org | fixtureの代わりにfactoryを使う", url: "http://betterspecs.org/#factories" %>
</article>
<article>
<!-- <h1><a name="matchers">Easy to read matcher</a></h1> -->
<h1><a name="matchers">読みやすいmatcherを使う</a></h1>
<!-- <p>
Use readable matchers and double check available <a href="/">rspec matcher</a>.
</p> -->
<p>
読みやすいmatcherを使いましょう。 <a href="https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers">rspec matcher</a>を二回以上確認しましょう。
</p>
<p class="wrong">bad</p>
<div>
<pre><code class="ruby">lambda { model.save! }.to raise_error Mongoid::Errors::DocumentNotFound
</code></pre>
</div>
<p class="correct">good</p>
<div>
<pre><code class="ruby">expect { model.save! }.to raise_error Mongoid::Errors::DocumentNotFound
</code></pre>
</div>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/12">このガイドに関して議論する →</a>
</p>
<%= render "partials/share", text: "betterspecs.org | 読みやすいmatcherを使う", url: "http://betterspecs.org/#matchers" %>
</article>
<article>
<h1><a name="sharedexamples">Shared Examples</a></h1>
<!-- <p>
Making tests is great and you get more confident day after day. But in the end you will start to see code duplication coming up everywhere.
Use shared examples to DRY your test suite up.
</p> -->
<p>
テストを作るのは素晴らしいです。毎日少しづつ自信がつきます。が、結局色んな所にコードの重複が発生します。shared exampleを使ってテストをDRYしましょう。
</p>
<p class="wrong">bad</p>
<div>
<pre><code class="ruby">describe 'GET /devices' do
let!(:resource) { FactoryGirl.create :device, created_from: user.id }
let(:uri) { '/devices' }
context 'when shows all resources' do
let!(:not_owned) { FactoryGirl.create factory }
it 'shows all owned resources' do
page.driver.get uri
expect(page.status_code).to be(200)
contains_owned_resource resource
does_not_contain_resource not_owned
end
end
describe '?start=:uri' do
it 'shows the next page' do
page.driver.get uri, start: resource.uri
expect(page.status_code).to be(200)
contains_resource resources.first
expect(page).to_not have_content resource.id.to_s
end
end
end
</code></pre>
</div>
<p class="correct">good</p>
<div>
<pre><code class="ruby">describe 'GET /devices' do
let!(:resource) { FactoryGirl.create :device, created_from: user.id }
let(:uri) { '/devices' }
it_behaves_like 'a listable resource'
it_behaves_like 'a paginable resource'
it_behaves_like 'a searchable resource'
it_behaves_like 'a filterable list'
end
</code></pre>
</div>
<!-- <p>
In our experience, shared examples are used mainly for controllers. Since models are pretty
different from each other, they (usually) do not share much logic.
</p> -->
<p>
経験によると、shared exampleは大体コントローラで使われます。モデルはお互いかなり異なるため、(普通は)多くのロジックを共有しません。
</p>
<!-- <p>
Learn more about <a href="https://www.relishapp.com/rspec/rspec-core/v/2-11/docs/example-groups/shared-examples">rspec shared examples</a>.
</p> -->
<p>
<a href="https://www.relishapp.com/rspec/rspec-core/v/2-11/docs/example-groups/shared-examples">rspec shared examples</a>に関してもっと学ぶ。
</p>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/13">このガイドに関して議論する →</a>
</p>
<%= render "partials/share", text: "betterspecs.org | shared examples", url: "http://betterspecs.org/#sharedexamples" %>
</article>
<article>
<!-- <h1><a name="integration">Test what you see</a></h1> -->
<h1><a name="integration">見えるものをテスト</a></h1>
<!-- <p>
<strong>Deeply test your models and your application behaviour (integration tests).
Do not add useless complexity testing controllers.</strong>
</p> -->
<p>
<strong>モデルとアプリの振る舞いを深くテストしましょう(結合テスト)。
コントローラをテストする為にいらない複雑さを入れないようにしましょう。</strong>
</p>
<!-- <p>
When I first started testing my apps I was testing controllers, now I don't.
Now I only create integration tests using RSpec and Capybara. Why? Because I truly
believe that you should test what you see and because testing controllers is an
extra step you don't need. You'll find out that most of your tests go into the
models and that integration tests can be easily grouped into shared examples,
building a clear and readable test suite.
</p> -->
<p>
私は自分のアプリケーションをテストする時、最初はコントローラをテストしていましたが、今は行いません。
今はRSpecとCapybaraの統合テストしか作りません。なぜかと言うとほんとに目に見えるものをテストするべきだと信じているし、コントローラをテストするのは不要な段階だと思っているからです。そのうちテスト達はモデルと統合テストにされ、shared examplesに纏まって、きれいで読みやすいテストを作るようになります。
</p>
<!-- <p>
This is an open debate in the Ruby community and both sides have good arguments
supporting their idea. People supporting the need of testing controllers will tell
you that your integration tests don't cover all use cases and that they are slow.
</p> -->
<p>
これは未だ Ruby コミュニティーで議論中の話で、両方の側その考えを支える良い根拠が有ります。コントローラのテストを支持する側は統合テストは遅くてすべての機能をカバー出来ないと主張します。
</p>
<!-- <p>
Both are wrong. You can easily cover all use cases (why shouldn't you?) and you
can run single file specs using automated tools like Guard. In this way you will run
only the specs you need to test blazing fast without stopping your flow.
</p> -->
<p>
両方とも違います。大体簡単にすべての機能をカバー出るし(でしょ?)Guardみたいな自動化ツールを利用して一ファイルだけテストするのも可能です。こうすれば大きな流れを逆らわない範囲内で必要なspecだけテストできます。
</p>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/14">このガイドに関して議論する →</a>
</p>
<%= render "partials/share", text: "betterspecs.org | 見えるものをテスト", url: "http://betterspecs.org/#integration" %>
</article>
<article>
<!-- <h1><a name="should">Don't use should</a></h1> -->
<h1><a name="should">shouldを使わない</a></h1>
<!-- <p>
Do not use should when describing your tests. Use the third person in the present tense.
Even better start using the new
<a href="http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax">expectation</a>
syntax.
</p> -->
<p>
テストの説明を書くときにshould使わないようにしましょう。現在時制に第三者観点から書きましょう。
新しい<a href="http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax">expectation</a>を使うともっと良いです。
</p>
<p class="wrong">bad</p>
<div>
<pre><code class="ruby">it 'should not change timings' do
consumption.occur_at.should == valid.occur_at
end
</code></pre>
</div>
<p class="correct">good</p>
<div>
<pre><code class="ruby">it 'does not change timings' do
expect(consumption.occur_at).to equal(valid.occur_at)
end
</code></pre>
</div>
<!-- <p>
See <a href="https://github.com/should-not/should_not">the should_not gem</a> for a way to enforce this in RSpec
and <a href="https://github.com/siyelo/should_clean">the should_clean</a> gem for a way to clean up existing RSpec examples that begin with "should."
</p> -->
<p>
shouldを使わないように制限できる<a href="https://github.com/should-not/should_not">should_not</a>をご覧ください。
そして既に作成されたrspecから"should."で始まる例文を消してくれる<a href="https://github.com/siyelo/should_clean">the should_clean</a>もご覧ください。
</p>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/15">このガイドに関して議論する →</a>
</p>
<%= render "partials/share", text: "betterspecs.org | shouldを使わない", url: "http://betterspecs.org/#should" %>
</article>
<article>
<!-- <h1><a name="guard">Automatic tests with guard</a></h1> -->
<h1><a name="guard">guardを使ったテスト自動化</a></h1>
<!-- <p>
Running all the test suite every time you change your app can be cumbersome. It takes a lot of
time and it can break your flow. With Guard you can automate your test suite running only the
tests related to the updated spec, model, controller or file you are working at.
</p> -->
<p>
変更がある度に全テストをまわすのは厄介です。それは時間もかかりますし、流れをわずわらせます。Guardを使うと更新された使用、モデル、コントローラ、ファイルだけに対してテストをまわせるように自動化できます。
</p>
<p class="correct">good</p>
<div>
<pre><code class="ruby">bundle exec guard
</code></pre>
</div>
<!-- Here you can see a sample Guardfile with some basic reloading rules. -->
この例は基本的なリロードルールがあるGuardfileです。
<p class="correct">good</p>
<!-- <div>
<pre><code class="ruby">guard 'rspec', cli: '--drb --format Fuubar --color', version: 2 do
# run every updated spec file
watch(%r{^spec/.+_spec\.rb$})
# run the lib specs when a file in lib/ changes
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
# run the model specs related to the changed model
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
# run the view specs related to the changed view
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
# run the integration specs related to the changed controller
watch(%r{^app/controllers/(.+)\.rb}) { |m| "spec/requests/#{m[1]}_spec.rb" }
# run all integration tests when application controller change
watch('app/controllers/application_controller.rb') { "spec/requests" }
end
</code></pre>
</div> -->
<div>
<pre><code class="ruby">guard 'rspec', cli: '--drb --format Fuubar --color', version: 2 do
# すべての更新されたspecを実行
watch(%r{^spec/.+_spec\.rb$})
# lib/フォルダーの中のファイルが変更された時libのspecを実行