forked from betterspecs/betterspecs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
976 lines (695 loc) · 28.6 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
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
---
layout: default
---
<button class="action action--open" aria-label="Open Menu"><span class="icon icon--menu"></span></button>
<nav id="ml-menu" class="menu">
<button class="action action--close" aria-label="Close Menu"><span class="icon icon--cross"></span></button>
<div class="menu__wrap">
<ul data-menu="main" class="menu__level">
<li class="menu__item"><a class="menu__link" href="#describe">Describe methods</a></li>
<li class="menu__item"><a class="menu__link" href="#contexts">Use contexts</a></li>
<li class="menu__item"><a class="menu__link" href="#short">Short description</a></li>
<li class="menu__item"><a class="menu__link" href="#single">Single Expectation</a></li>
<li class="menu__item"><a class="menu__link" href="#all">All possible cases</a></li>
<li class="menu__item"><a class="menu__link" href="#expect">Expect vs should</a></li>
<li class="menu__item"><a class="menu__link" href="#subject">Use subject</a></li>
<li class="menu__item"><a class="menu__link" href="#let">Use let and let!</a></li>
<li class="menu__item"><a class="menu__link" href="#mock">Mock or not to mock</a></li>
<li class="menu__item"><a class="menu__link" href="#data">Create the data you need</a></li>
<li class="menu__item"><a class="menu__link" href="#factories">Use factories</a></li>
<li class="menu__item"><a class="menu__link" href="#matchers">Easy to read matchers</a></li>
<li class="menu__item"><a class="menu__link" href="#shared">Shared Examples</a></li>
<li class="menu__item"><a class="menu__link" href="#integration">Test what you see</a></li>
<li class="menu__item"><a class="menu__link" href="#should">Do not use should</a></li>
<li class="menu__item"><a class="menu__link" href="#continuous">Continuous testing</a></li>
<li class="menu__item"><a class="menu__link" href="#faster">Faster tests</a></li>
<li class="menu__item"><a class="menu__link" href="#stubbing">Stubbing HTTP requests</a></li>
<li class="menu__item"><a class="menu__link" href="#formatter">Formatter</a></li>
<li class="menu__item"><a class="menu__link" href="#contributing">Contributing</a></li>
</ul>
</div>
</nav>
<div class="content">
<article>
<h1><a name="what">What is Better Specs</a></h1>
<p>
{{ site.description }}
Better Specs came to life at <a target="_blank" href="https://lelylan.com">Lelylan</a> (open source IoT cloud platform) and <a target="_blank" href="https://github.com/lelylan/lelylan">checking out its test suite</a> may be of inspiration.
</p>
<p class="info">
Better Specs focus on Rails testing, but our goal is to create testing guidelines covering most languages and frameworks out there (e.g. Scala, Elixir, React). If you want to add your favourite language testing guidelines <a target="_blank" href="https://github.com/lelylan/betterspecs/issues">open an issue</a>.
</p>
<p>
</p>
</article>
<article>
<h1><a name="describe">Describe Your Methods</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>
<div class="code-block">
<p class="spec-title spec-wrong">bad</p>
{% highlight ruby %}
describe 'the authenticate method for User' do
describe 'if the user is an admin' do
{% endhighlight %}
</div>
<div class="code-block">
<p class="spec-title spec-correct">good</p>
{% highlight ruby %}
describe '.authenticate' do
describe '#admin?' do
{% endhighlight %}
</div>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/2">Discuss this guideline →</a>
</p>
</article>
<article>
<h1><a name="contexts">Use contexts</a></h1>
<p>
Contexts are a powerful method to make your tests clear and well organized (they keep tests easy to read). When describing a context, start its description with "when", "with" or "without".
</p>
<div class="code-block">
<p class="spec-title spec-wrong">bad</p>
{% highlight 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
{% endhighlight %}
</div>
<div class="code-block">
<p class="spec-title spec-correct">good</p>
{% highlight 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
{% endhighlight %}
</div>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/3">Discuss this guideline →</a>
</p>
</article>
<article>
<h1><a name="short">Keep your description 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>
<div class="code-block">
<p class="spec-title spec-wrong">bad</p>
{% highlight ruby %}
it 'has 422 status code if an unexpected params will be added' do
{% endhighlight %}
</div>
<div class="code-block">
<p class="spec-title spec-correct">Good</p>
{% highlight ruby %}
context 'when not valid' do
it { is_expected.to respond_with 422 }
end
{% endhighlight %}
</div>
<p>
In the example we removed the description related to the status code, which has been replaced by the expectation <code>is_expected</code>. If you run this test typing <code>rspec filename</code> you will obtain a readable output.
</p>
<div class="code-block">
<p class="spec-title">Formatted output</p>
{% highlight ruby %}
when not valid
it should respond with 422
{% endhighlight %}
</div>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/4">Discuss this guideline →</a>
</p>
</article>
<article>
<h1><a name="single">Single expectation test</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. 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>
<div class="code-block">
<p class="spec-title spec-correct">good (isolated)</p>
{% highlight ruby %}
it { is_expected.to respond_with_content_type(:json) }
it { is_expected.to assign_to(:resource) }
{% endhighlight %}
</div>
<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>
<div class="code-block">
<p class="spec-title spec-correct">good (not isolated)</p>
{% highlight ruby %}
it 'creates a resource' do
expect(response).to respond_with_content_type(:json)
expect(response).to assign_to(:resource)
end
{% endhighlight %}
</div>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/5">Discuss this guideline →</a>
</p>
</article>
<article>
<h1><a name="all">Test all possible cases</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>
<div class="code-block">
<p class="spec-title">Destroy Action</p>
{% highlight ruby %}
before_action :find_owned_resources
before_action :find_resource
def destroy
render 'show'
@consumption.destroy
end
{% endhighlight %}
<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>
<div class="code-block">
<p class="spec-title spec-wrong">bad</p>
{% highlight ruby %}
it 'shows the resource'
{% endhighlight %}
</div>
<div class="code-block">
<p class="spec-title spec-correct">good</p>
{% highlight 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
{% endhighlight %}
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/6">Discuss this guideline →</a>
</p>
</article>
<article>
<h1><a name="expect">Expect vs Should syntax</a></h1>
<p>
On new projects always use the <code>expect</code> syntax.
</p>
<div class="code-block">
<p class="spec-title spec-wrong">bad</p>
{% highlight ruby %}
it 'creates a resource' do
response.should respond_with_content_type(:json)
end
{% endhighlight %}
</div>
<div class="code-block">
<p class="spec-title spec-correct">good</p>
{% highlight ruby %}
it 'creates a resource' do
expect(response).to respond_with_content_type(:json)
end
{% endhighlight %}
</div>
<p>Configure the RSpec to only accept the new syntax on new projects, to avoid having the 2 syntax all over the place.</p>
<div class="code-block">
<p class="spec-title spec-correct">good</p>
{% highlight ruby %}
# spec_helper.rb
RSpec.configure do |config|
# ...
config.expect_with :rspec do |c|
c.syntax = :expect
end
end
{% endhighlight %}
</div>
<p>On one line expectations or with implicit subject we should use <code>is_expected.to</code>.</p>
<div class="code-block">
<p class="spec-title spec-wrong">bad</p>
{% highlight ruby %}
context 'when not valid' do
it { should respond_with 422 }
end
{% endhighlight %}
</div>
<div class="code-block">
<p class="spec-title spec-correct">good</p>
{% highlight ruby %}
context 'when not valid' do
it { is_expected.to respond_with 422 }
end
{% endhighlight %}
</div>
<p>On old projects you can use the <a href="https://github.com/yujinakayama/transpec">transpec</a> to convert them to the new syntax. 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>
<a href="https://github.com/andreareginato/betterspecs/issues/83">Discuss this guideline →</a>
</p>
</article>
<article>
<h1><a name="subject">Use subject</a></h1>
<p>
If you have several tests related to the same subject use
<code>subject{}</code> to DRY them up.
</p>
<div class="code-block">
<p class="spec-title spec-wrong">bad</p>
{% highlight ruby %}
it { expect(assigns('message')).to match /it was born in Belville/ }
{% endhighlight %}
</div>
<div class="code-block">
<p class="spec-title spec-correct">good</p>
{% highlight ruby %}
subject { assigns('message') }
it { is_expected.to match /it was born in Billville/ }
{% endhighlight %}
</div>
<p>
RSpec has also the ability to use a named subject (learn more about <a href="https://www.relishapp.com/rspec/rspec-core/v/2-11/docs/subject">rspec subject</a>).
</p>
<div class="code-block">
<p class="spec-title spec-correct">good</p>
{% highlight ruby %}
subject(:hero) { Hero.first }
it "carries a sword" do
expect(hero.equipment).to include "sword"
end
{% endhighlight %}
</div>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/7">Discuss this guideline →</a>
</p>
</article>
<article>
<h1><a name="let">Use let and 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> does can be found in this
<a href="http://stackoverflow.com/questions/5359558/when-to-use-rspec-let/5359979#5359979">stackoverflow answer</a>.
</p>
<div class="code-block">
<p class="spec-title spec-wrong">bad</p>
{% highlight 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
{% endhighlight %}
</div>
<div class="code-block">
<p class="spec-title spec-correct">good</p>
{% highlight 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
{% endhighlight %}
</div>
<p>
Use <code>let</code> to initialize actions that are lazy loaded to test your specs.
</p>
<div class="code-block">
<p class="spec-title spec-correct">good</p>
{% highlight 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
{% endhighlight %}
</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. Here an example of what let actually is (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>
<div class="code-block">
<p class="spec-title">Explenation</p>
{% highlight ruby %}
# this use of let
let(:foo) { Foo.new }
# is very nearly equivalent to this:
def foo
@foo ||= Foo.new
end
{% endhighlight %}
</div>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/8">Discuss this guideline →</a>
</p>
</article>
<article>
<h1><a name="mock">Mock or not to mock</a></h1>
<p>
As general rule do not (over)use mocks and test real behavior when possible, as testing real cases is useful when validating your application flow.
</p>
<div class="code-block">
<p class="spec-title spec-correct">good</p>
{% highlight ruby %}
# simulate a not found resource
context "when not found" do
before do
allow(Resource).to receive(:where).with(created_from: params[:id])
.and_return(false)
end
it { is_expected.to respond_with 404 }
end
{% endhighlight %}
</div>
<p>
Mocking makes your specs faster but they are difficult to use. You need to understand them well to use them well. Read <a href="http://myronmars.to/n/dev-blog/2012/06/thoughts-on-mocking">this article</a> to learn more abou mocks.
</p>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/9">Discuss this guideline →</a>
</p>
</article>
<article>
<h1><a name="data">Create only the data you need</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>
<div class="code-block">
<p class="spec-title spec-correct">good</p>
{% highlight ruby %}
RSpec.describe User do
describe ".top" do
before { FactoryGirl.create_list(:user, 3) }
it { expect(described_class.top(2).size).to eq 2 }
end
end
{% endhighlight %}
</div>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/10">Discuss this guideline →</a>
</p>
</article>
<article>
<h1><a name="factories">Use factories and not fixtures</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 (learn about <a href="https://github.com/thoughtbot/factory_girl">Factory Girl</a>).
</p>
</p>
<div class="code-block">
<p class="spec-title spec-wrong">bad</p>
{% highlight ruby %}
user = User.create(
name: 'Genoveffa',
surname: 'Piccolina',
city: 'Billyville',
birth: '17 Agoust 1982',
active: true
)
{% endhighlight %}
</div>
<div class="code-block">
<p class="spec-title spec-correct">good</p>
{% highlight ruby %}
user = FactoryGirl.create :user
{% endhighlight %}
</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>
<a href="https://github.com/andreareginato/betterspecs/issues/11">Discuss this guideline →</a>
</p>
</article>
<article>
<h1><a name="matchers">Easy to read matcher</a></h1>
<p>
Use readable matchers and double check the available <a href="https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers">rspec matchers</a>.
</p>
<div class="code-block">
<p class="spec-title spec-wrong">bad</p>
{% highlight ruby %}
lambda { model.save! }.to raise_error Mongoid::Errors::DocumentNotFound
{% endhighlight %}
</div>
<div class="code-block">
<p class="spec-title spec-correct">good</p>
{% highlight ruby %}
expect { model.save! }.to raise_error Mongoid::Errors::DocumentNotFound
{% endhighlight %}
</div>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/12">Discuss this guideline →</a>
</p>
</article>
<article>
<h1><a name="shared">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>
<div class="code-block">
<p class="spec-title spec-wrong">bad</p>
{% highlight 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
{% endhighlight %}
</div>
<div class="code-block">
<p class="spec-title spec-correct">good</p>
{% highlight 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
{% endhighlight %}
</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. Learn more about <a href="https://www.relishapp.com/rspec/rspec-core/v/3-2/docs/example-groups/shared-examples">rspec shared examples</a>.
</p>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/13">Discuss this guideline →</a>
</p>
</article>
<article>
<h1><a name="integration">Test what you see</a></h1>
<p>
Deeply test your models and your application behaviour (integration tests). Do not add useless complexity testing controllers.
</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
believe that you should test what you see and because testing controllers is an extra step you wont usually 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>
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. 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>
<a href="https://github.com/andreareginato/betterspecs/issues/14">Discuss this guideline →</a>
</p>
</article>
<article>
<h1><a name="should">Don't use 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>
<div class="code-block">
<p class="spec-title spec-wrong">bad</p>
{% highlight ruby %}
it 'should not change timings' do
consumption.occur_at.should == valid.occur_at
end
{% endhighlight %}
</div>
<div class="code-block">
<p class="spec-title spec-correct">good</p>
{% highlight ruby %}
it 'does not change timings' do
expect(consumption.occur_at).to equal(valid.occur_at)
end
{% endhighlight %}
</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>
<a href="https://github.com/andreareginato/betterspecs/issues/15">Discuss this guideline →</a>
</p>
</article>
<article>
<h1><a name="continuous">Automatic tests with 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>
<div class="code-block">
<p class="spec-title spec-correct">good</p>
{% highlight ruby %}
bundle exec guard
{% endhighlight %}
</div>
Here you can see a sample Guardfile with some basic reloading rules.
<div class="code-block">
<p class="spec-title spec-correct">good</p>
{% highlight 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
{% endhighlight %}
</div>
<p>
Guard is a fine tool but as usual it doesn't fit all of your needs. Sometimes your TDD workflow works best with a keybinding that makes it easy to run just the examples you want when you want to. Then, you can use a rake task to run the entire suite before pushing code. Find an example vim keybinding <a href="https://github.com/myronmarston/vim_files/blob/5bd4faad7c020ebcbf62dcbc59985262b4eacb53/vimrc.after#L61-103">here</a> and learn more about <a href="https://github.com/guard/guard-rspec">guard-rspec</a>.
</p>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/16">Discuss this guideline →</a>
</p>
</article>
<article>
<h1><a name="faster">Faster tests (preloading Rails)</a></h1>
<p>
When running a test on Rails the whole Rails app is loaded. This can take time and it can break your development flow. To solve this problem use solutions like <a href="https://github.com/burke/zeus">Zeus</a>,
<a href="https://github.com/jstorimer/spin">Spin</a> or
<a href="https://github.com/sporkrb/spork">Spork</a>.
Those solutions will preload all libraries you (usually) do not change and reload controllers, models, view, factories and all the files you change most often.
</p>
<p>
Here you can find a <a href="https://gist.github.com/3821012">spec helper</a> and a <a href="https://gist.github.com/3821031">Guardfile</a> configuration based on Spork.
With this configuration you will reload the whole app if a preloaded file (like initializers) change and you will run the single tests really, really fast.
</p>
<p>
The drawback of using Spork is that it aggressively monkey-patches your code and you could lose some hours trying to understand why a file is not reloaded. If you have some
code examples using Spin or any other solution <a href="https://github.com/lelylan/betterspecs/issues/17">let us know</a>.
</p>
<p>
Here you can find a <a href="https://gist.github.com/HuffMoody/5912373">Guardfile</a> configuration for using Zeus. The spec_helper does not need to be modified, however, you will have to run `zeus start` in a console to start the zeus server before running your tests. Although Zeus takes a less aggressive approach than Spork, one major drawback is the fairly strict usage requirements; Ruby 1.9.3+ (recommended using backported GC from Ruby 2.0)
as well as an operating system that supports FSEvents or inotify is required.
</p>
<p>
Many criticisms are moved to those solutions. Those libraries are a band aid on a problem that is better solved through better design, and being intentional about only loading the dependencies that you need. Learn more reading the <a href="https://github.com/lelylan/betterspecs/issues/17">related discussion</a>.
</p>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/17">Discuss this guideline →</a>
</p>
</article>
<article>
<h1><a name="stubbing">Stubbing HTTP requests</a></h1>
<p>
Sometimes you need to access external services. In these cases you can't rely on the real service but you should stub it with solutions like webmock.
</p>
<div class="code-block">
<p class="spec-title spec-correct">good</p>
{% highlight ruby %}
context "with unauthorized access" do
let(:uri) { 'http://api.lelylan.com/types' }
before { stub_request(:get, uri).to_return(status: 401, body: fixture('401.json')) }
it "gets a not authorized notification" do
page.driver.get uri
expect(page).to have_content 'Access denied'
end
end
{% endhighlight %}
</div>
<p>
Learn more about <a href="https://github.com/bblimke/webmock">webmock</a> and
<a href="https://github.com/vcr/vcr">VCR</a>. Here is a
<a href="http://marnen.github.com/webmock-presentation/webmock.html">nice presentation</a>
explaining how to mix them together.
</p>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/18">Discuss this guideline →</a>
</p>
</article>
<article>
<h1><a name="formatter">Useful formatter</a></h1>
<p>
Use a formatter that can give you useful information about the test suite. I personally find fuubar really nice. To make it work add the gem and set fuubar as default formatter in your Guardfile.
</p>
<div class="code-block">
<p class="spec-title spec-correct">good</p>
{% highlight ruby %}
# Gemfile
group :development, :test do
gem 'fuubar'
# .rspec configuration file
--drb
--format Fuubar
--color
{% endhighlight %}
<p>
Learn more about <a href="http://jeffkreeftmeijer.com/2010/fuubar-the-instafailing-rspec-progress-bar-formatter/">fuubar</a>.
</p>
<p>
<a href="https://github.com/andreareginato/betterspecs/issues/19">Discuss this guideline →</a>
</p>
</article>
<article>
<h1><a name="contributing">Contributing</a></h1>
<p>
“Feel free to submit a PR” are words often found in GitHub, but met with confusion and fear by many. Getting started with contributing open source is not always straightforward and can be tricky. If you are new to contribution, watch these <a href="https://egghead.io/series/how-to-contribute-to-an-open-source-project-on-github">videos</a> and you’ll be equipped with the the tools, knowledge, and understanding you need to start on contributing to the world of open source projects. In particular, Better Specs needs your help on the following tasks.
</p>
<p class="info">
→ Add testing guide lines for new languages (<a target="_blank" href="https://github.com/lelylan/betterspecs/issues">open an issue</a>).<br>
→ Fix outdated best practices which has changed during time (<a target="_blank" href="https://github.com/lelylan/betterspecs/issues">open an issue</a>).<br>
→ Add or update existing translations (<a target="_blank" href="https://github.com/lelylan/betterspecs/issues">open an issue</a>).<br>
</span>
</p>
<p>
Thanks for your time, enjoy the coding and start contributing to the projects you use and love today.
</p>
</article>