-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1422 lines (1256 loc) · 40.9 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
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<!-- Slide meta data, remove/edit as you see fit -->
<title>BDD for .Net</title>
<meta name="author" content="satya chundur" />
<meta name="email" content="[email protected]" />
<meta name="date" content="2012-05-07" />
<meta name="venue" content="Continuum Testing Group" />
<!-- Slippy core file and dependencies -->
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="jquery.history.js"></script>
<script type="text/javascript" src="fulltoo.js"></script>
<script type="text/javascript" src="analytics.js"></script>
<script type="text/javascript" src="slippy-0.9.0.js"></script>
<!-- Slippy structural styles -->
<link type="text/css" rel="stylesheet" href="slippy-0.9.0.css"/>
<!-- Slippy theme -->
<link type="text/css" rel="stylesheet" href="slippy-specflow.css"/>
<!-- Syntax highlighting -->
<script type="text/javascript" src="highlighter/shCore.js"></script>
<script type="text/javascript" src="highlighter/shBrushBash.js"></script>
<script type="text/javascript" src="highlighter/shBrushRuby.js"></script>
<link type="text/css" rel="stylesheet" href="highlighter/shCore.css"/>
<link type="text/css" rel="stylesheet" href="highlighter/shThemeEclipse.css"/>
<!-- Slippy init code -->
<script type="text/javascript">
$(function() {
$(".slide").slippy({
});
SyntaxHighlighter.all();
});
</script>
</head>
<body>
<div class="slide">
<h1>What is BDD</h1>
<h3 class="center">A Cynical definition is: <br />"Development community building tools for non-developers and ending up being the only ones using them"</h3>
<div class="footer">
<span class="right">Use space bar or arrow keys to navigate, and escape for index.</span>
</div>
</div>
<div class="slide">
<h1>BDD for .NET</h1>
<div lass ="left">
<h4>Below are the BDD Frameworks developed for .NET and based on Gherkin <h4>
<P>Specflow </p>
<p>Cuke4Nuke </p>
<p>NSpec </p>
<p>Spec </p>
<p>Nbehave </p>
<P>blah...blah ... </p>
</div>
</div>
<div class="slide">
<h1>Why Specflow?</h1>
<div class ="center">
<h3>Clear, concise and readable and Write Executable Specifications to drive the development process <h3>
</div>
<div class="comparison">
<div class="left">
<pre class="brush: ruby">
Example:
Feature Addition of two Number
As a Math Idiot i want to
add two numbers
Scenario: Addition
Given I have calculator
When I add two numbers
Then addition will be displayed
</pre>
</div>
<div class="middle">
<p>vs</p>
</div>
<div class="right">
<pre class="brush: ruby">
public class addition(int a, int b)
{
calculator = new Systemcalculator();
calculator.add(a,b);
}
</pre>
</div>
</div>
</div>
<div class="slide">
<h1>Specflow</h1>
<span style="color:black">
<h3>For: Stakeholders/BA</h3>
<h3>Feature request means BA can create a file with feature like this. <br></span></h3>
<div class="center" style="color : orange">
<pre class = "brush: ruby" >
Feature : Total Score calculations
In order to know my performance
As a player
I want the system to calculate my total score
</span>
</pre>
</div>
</div>
<div class="slide">
<div class ="center">
<h3>For: BA/Developers/Test Team </h3>
</div>
<div class="vcenter">
<pre class ="brush : ruby">
Scenario: Gutter Game
Given a new bowling game
When all of my balls are landing in the gutter
Then my total scroe should be 0
</pre>
<br>
<pre class = "brush:ruby">
Scenario: All Strikes
Given a new bowling game
When all of my rolls are strikes
Then my total score should be 300
</pre>
</div>
</div>
<div class="slide">
<div class="vcenter">
<h3>For: Dev/Test team Lets make this Spec executable</h3>
<pre class="brush: ruby">
[Binding]
public class BowlingSteps
{
private Game _game;
[Given(@"a new bowling game")]
public void GivenANewBowlingGame()
{
_game = new Game();
}
[When(@"all of my balls are landing in the gutter")]
public void WhenAllOfMyBallsAreLandingInTheGutter()
{
for (int i = 0; i < 20; i++)
{
_game.Roll(0);
}
}
[When(@"all of my rolls are strikes")]
public void WhenAllOfMyRollsAreStrikes()
{
for (int i = 0; i < 12; i++)
{
_game.Roll(10);
}
}
[Then(@"my total score should be (\d+)")]
public void ThenMyTotalScoreShouldBe(int score)
{
Assert.AreEqual(score, _game.Score);
}
</pre>
</div>
</div>
<div class="slide">
<h2>Gherkin Language Elements</h2>
<div class="vcenter">
<pre class="brush: ruby">
General Gherkin Language Elements which every one knows
</pre>
<ul>
<li>Features</li>
<li>Scenarios</li>
<li>Scenario Steps</li>
<li>Examples</li>
<li>Tags</li>
<li>Background</li>
<li>Scenario Outline</li>
<li>Bindings</li>
</ul>
</div>
</div>
<div class="slide">
<h2>Specflow Unit Test Providers</h2>
<pre class="brush: ruby">
Specflow Support below unit test providers
</pre>
<ul>
<li>NUnit</li>
<li>MsTest</li>
<li>MsTest2010</li>
<li>MsTest.Sliverlight</li>
<li>Xunit</li>
<li>MbUnit</li>
</ul>
</div>
<div class="slide">
<h3>Enough of introduction/awesomeness of specflow.. Lets get into installation</h3>
<div class="vcenter">
<pre>
</pre>
</div>
</div>
<!-- <div class="slide">
<h1>Formatters</h1>
<h2>progress (default)</h2>
<div class="vcenter">
<pre>
$ rspec --colour spec/
<span style="color:green">..</span>
Finished in 0.00089 seconds
<span style="color:green">2 examples, 0 failures</span>
</pre>
</div>
</div>
<div class="slide">
<h1>A digression</h1>
<h2>Configuration file</h2>
<div class="vcenter">
<p>We don’t want to type <code>--colour</code> every time!</p>
<p>Put common options in <code>.rspec</code> in the project root.</p>
</div>
</div>
<div class="slide">
<h1>Formatters</h1>
<h2>documentation</h2>
<div class="vcenter">
<pre>
$ rspec -f doc spec/
Stack
when new
<span style="color:green">should be empty</span>
<span style="color:green">should have 0 items</span>
Finished in 0.00089 seconds
<span style="color:green">2 examples, 0 failures</span>
</pre>
</div>
</div>
<div class="slide">
<h1>Formatters</h1>
<h2>html</h2>
<img src="images/html_output.png" alt="HTML output" />
</div>
<div class="slide">
<h1>Formatters</h1>
<h2>Fuubar</h2>
<div class="vcenter">
<p>Fuubar is an external formatter (<code>gem in fuubar</code>).</p>
<pre>
$ rspec -f Fuubar spec/
1) Stack after pushing 1 item
<span style="color:red">Failure/Error: it { should have(1).item }</span>
<span style="color:red">Expected 1 item, got 0</span>
<span style="color:grey"># ./spec/stack_spec.rb:12</span>
<span style="color:red">8/10: 80% |================================ | ETA: 00:00:02</span>
</pre>
</div>
</div>
<div class="slide">
<h1>Formatters</h1>
<h2>Multiple formatters</h2>
<div class="vcenter">
<pre>
$ rspec -f Fuubar -f html -o specs.html spec/
</pre>
</div>
</div>
<div class="slide">
<h1>Profiling</h1>
<pre>
$ rspec --profile spec/
<span style="color:green">55/55: 100% |====================================| Time: 00:00:00</span>
Top 10 slowest examples:
RedRock server allows the port to be overridden
<span style="color:red">0.10258</span> seconds ./spec/redrock_spec.rb:107
RedRock supports specification of a response body as an IO object
<span style="color:red">0.08752</span> seconds ./spec/redrock_spec.rb:336
RedRock returning a custom response returns the specified headers
<span style="color:red">0.07319</span> seconds ./spec/redrock_spec.rb:331
...
</pre>
</div>
<div class="slide">
<h1>Pending specs</h1>
<pre class="brush: ruby">
describe "Something" do
it "does something" do
...
end
it "does something I haven't implemented yet"
end
</pre>
<hr />
<pre>
Something
...
<span style="color:#aa0">does something I haven't implemented yet (PENDING: Not Yet Implemented)</span>
Pending:
<span style="color:#aa0">Something does something I haven't implemented yet</span>
<span style="color:grey"># Not Yet Implemented</span>
<span style="color:grey"># ./spec/something_spec.rb:5</span>
Finished in 0.00052 seconds
<span style="color:#aa0">2 examples, 0 failures, 1 pending</span>
</pre>
</div>
<div class="slide">
<h1>Pending specs</h1>
<pre class="brush: ruby">
describe "Something" do
...
it "does something I haven't implemented yet" do
pending "waiting for inspiration"
...
end
end
</pre>
<hr />
<pre>
Something
...
<span style="color:#aa0">does something I haven't implemented yet (PENDING: waiting for inspiration)</span>
Pending:
<span style="color:#aa0">Something does something I haven't implemented yet</span>
<span style="color:grey"># waiting for inspiration</span>
<span style="color:grey"># ./spec/something_spec.rb:5</span>
Finished in 0.00078 seconds
<span style="color:#aa0">2 examples, 0 failures, 1 pending</span>
</pre>
</div>
<div class="slide">
<h1>Pending specs</h1>
<pre class="brush: ruby">
describe "Something" do
before { pending "Coming soon!" }
it "does something" do
end
it "does something else" do
end
end
</pre>
<hr />
<pre>
Something
<span style="color:#aa0">does something (PENDING: Coming soon!)</span>
<span style="color:#aa0">does something else (PENDING: Coming soon!)</span>
Pending:
<span style="color:#aa0">Something does something</span>
<span style="color:grey"># Coming soon!</span>
<span style="color:grey"># ./spec/something_spec.rb:4</span>
<span style="color:#aa0">Something does something else</span>
<span style="color:grey"># Coming soon!</span>
<span style="color:grey"># ./spec/something_spec.rb:7</span>
Finished in 0.0114 seconds
<span style="color:#aa0">2 examples, 0 failures, 2 pending</span>
</pre>
</div>
<div class="slide">
<h1>Running one spec</h1>
<div class="vcenter">
<pre class="brush: ruby">
describe "Something" do
it "does stuff" do ...
it "does other stuff" do ...
it "does one more thing" do ...
end
</pre>
<hr />
<pre>
$ rspec -f doc spec/something_spec.rb:5
Run filtered using {:line_number=>5}
Something
<span style="color:green">does other stuff</span>
Finished in 0.00036 seconds
<span style="color:green">1 example, 0 failures</span>
</pre>
</div>
</div>
<div class="slide">
<h1>Filtering by tag</h1>
<div class="vcenter">
<pre class="brush: ruby">
describe "Something" do
it "does stuff" do
...
end
it "does the thing I'm working on", :current => true do
...
end
end
</pre>
<hr />
<p>Run only specs tagged ‘current’</p>
<pre>
rspec --tag current ...
</pre>
<p>Run specs <em>not</em> tagged ‘current’</p>
<pre>
rspec --tag ~current ...
</pre>
</div>
</div>
<div class="slide">
<h1>Filtering by tag value</h1>
<div class="vcenter">
<pre class="brush: ruby">
describe "Something" do
it "behaves one way in Ruby 1.8", :ruby => "1.8" do
...
end
it "behaves another way in Ruby 1.9", :ruby => "1.9" do
...
end
end
</pre>
<hr />
<p>Run 1.8 specs:</p>
<pre>
rspec --tag ruby:1.8
</pre>
</div>
</div>
<div class="slide">
<h1>Implicit filtering</h1>
<div class="vcenter">
<pre class="brush: ruby">
RUBY_1_9 = (RUBY_VERSION =~ /^1\.9/)
describe "Something" do
it "does this when using 1.9", :if => RUBY_1_9 do
...
end
it "Does that when not using 1.9", :unless => RUBY_1_9 do
...
end
end
</pre>
</div>
</div>
<div class="slide">
<h1>Filtering by name</h1>
<pre class="brush: ruby">
describe "Something" do
it "does stuff" do ...
it "does other stuff" do ...
it "does one more thing" do ...
end
</pre>
<hr />
<p>Select specs using a regular expression:</p>
<pre>
$ rspec -e stuff spec/something_spec.rb
Run filtered using {:full_description=>/(?-mix:stuff)/}
Something
<span style="color:green">does stuff</span>
<span style="color:green">does other stuff</span>
Finished in 0.00101 seconds
<span style="color:green">2 examples, 0 failures</span>
</pre>
</div>
<div class="slide">
<h1>Setup and teardown</h1>
<div class="vcenter">
<pre class="brush: ruby">
describe "Something" do
before :all do
# per-context setup
end
before do
# per-example setup
end
after do
# per-example teardown
end
after :all do
# per-context teardown
end
end
</pre>
</div>
</div>
<div class="slide">
<h1>Expectations</h1>
<div class="vcenter">
<p>RSpec adds <code>should</code> and <code>should_not</code> to all objects.</p>
<pre class="brush: ruby">
(2 + 2).should == 4
"foo".should_not == "bar"
value.should be_true
</pre>
<p>The <code>==</code> and <code>be_true</code> are <em>matchers</em>.</p>
</div>
</div>
<div class="slide">
<h1>Built-in matchers</h1>
<h2>Equality</h2>
<div class="vcenter">
<pre class="brush: ruby">
a.should equal(b) # both pass if a.equal? b
a.should be(b) #
a.should eql(b) # passes if a.eql? b
a.should == b # both pass if a == b
a.should eq(b) #
</pre>
</div>
</div>
<div class="slide">
<h1>Built-in matchers</h1>
<h2>Floating point</h2>
<div class="vcenter">
<pre class="brush: ruby">
Math::PI.should be_within(0.01).of(22.0/7)
</pre>
</div>
</div>
<div class="slide">
<h1>Built-in matchers</h1>
<h2>True or false</h2>
<div class="vcenter">
<pre class="brush: ruby">
obj.should be_true # passes if obj is truthy (not nil or false)
obj.should be_false # passes if obj is falsy (nil or false)
obj.should be_nil # passes if obj is nil
obj.should be # passes if obj is not nil
</pre>
<p><code>should be</code> is preferred to <code>should_not be_nil</code>.</p>
</div>
</div>
<div class="slide">
<h1>Built-in matchers</h1>
<h2>Operators</h2>
<div class="vcenter">
<pre class="brush: ruby">
7.should == 7
[1, 2, 3].should == [1, 2, 3]
"this is a string".should =~ /^this/
"this is a string".should_not =~ /^that/
String.should === "this is a string"
</pre>
<p>Comparison operators work with the "be" matcher:</p>
<pre class="brush: ruby">
37.should be < 100
37.should be <= 38
37.should be >= 2
37.should be > 7
</pre>
</div>
</div>
<div class="slide">
<h1>Built-in matchers</h1>
<h2>Collections</h2>
<pre class="brush: ruby">
# These two are identical:
[1, 2, 3].should have(3).items
[1, 2, 3].should have_exactly(3).items
[1, 2, 3].should have_at_least(2).items
[1, 2, 3].should have_at_most(4).items
# "items" is just syntactic sugar:
[1, 2, 3].should have(3).numbers
[1, 2, 3].should include(2)
[1, 2, 3].should include(1, 2)
{:a => 1, :b => 2}.should include(:a => 1)
[1, 2, 3].should =~ [2, 3, 1]
[:a, :c, :b].should_not =~ [:a, :c]
</pre>
</div>
<div class="slide">
<h1>Built-in matchers</h1>
<h2>Strings</h2>
<div class="vcenter">
<pre class="brush: ruby">
"This is a string".should include "str"
# These two are identical:
"This is a string".should =~ /^This/
"This is a string".should match(/^This/)
</pre>
</div>
</div>
<div class="slide">
<h1>Another digression</h1>
<h2>Failure messages</h2>
<div class="vcenter">
<p>The default messages are usually pretty good:</p>
<pre class="brush: ruby">
(2 + 2).should == 5
[1, 2, 3].should =~ [4, 3, 2]
</pre>
<hr />
<pre>
<span style="color:red">Failure/Error: (2 + 2).should == 5
expected: 5
got: 4 (using ==)
Failure/Error: [1, 2, 3].should =~ [4, 3, 2]
expected collection contained: [2, 3, 4]
actual collection contained: [1, 2, 3]
the missing elements were: [4]
the extra elements were: [1]</span>
</pre>
</div>
</div>
<div class="slide">
<h1>Another digression</h1>
<h2>Failure messages</h2>
<div class="vcenter">
<p>…but you can use a custom message if you like<sup>*</sup>:</p>
<pre class="brush: ruby">
it "includes 3" do
[1, 2, 3].should include(3), "Oh noes! No three!"
end
</pre>
<hr />
<pre>
1) Something includes 3
<span style="color:red">Failure/Error: [1, 2, 4].should include(3), "Oh noes! No three!"
Oh noes! No three!</span>
</pre>
<p><sup>*</sup>except with operator matchers (<code>==</code> etc).</p>
</div>
</div>
<div class="slide">
<h1>Back to built-in matchers</h1>
<h2>Predicates</h2>
<div class="comparison">
<div class="left">
<pre class="brush: ruby">
[].empty?.should be_true
42.even?.should be_true
123.multiple_of?(5).
should be_false
{:a => 1, :b => 2}.has_key?(:a).
should be_true
</pre>
</div>
<div class="middle">
<p>➧</p>
</div>
<div class="right">
<pre class="brush: ruby">
[].should be_empty
42.should be_even
123.should_not be_multiple_of(5)
{:a => 1, :b => 2}.
should have_key(:a)
</pre>
</div>
</div>
</div>
<div class="slide">
<h1>Built-in matchers</h1>
<h2>Classes</h2>
<div class="vcenter">
<pre class="brush: ruby">
"foo".should be_an_instance_of(String) # Exact class
"foo".should_not be_an_instance_of(Object) #
"foo".should be_a_kind_of(Object) # Match subclasses
"foo".should be_an(Object) #
"foo".should respond_to(:upcase)
"foo".should respond_to(:upcase, :downcase)
"foo".should respond_to(:upcase).with(0).arguments
</pre>
</div>
</div>
<div class="slide">
<h1>Built-in matchers</h1>
<h2>Changing values</h2>
<div class="vcenter">
<pre class="brush: ruby">
expect{ array << 42 }.to change{ array.size }.from(0).to(1)
expect{ array << 42 }.to change{ array.size }.by(1)
</pre>
</div>
</div>
<div class="slide">
<h1>Built-in matchers</h1>
<h2>Raising and throwing</h2>
<div class="vcenter">
<pre class="brush: ruby">
expect { 4/2 }.to_not raise_error
expect { 4/0 }.to raise_error
expect { 4/0 }.to raise_error(ZeroDivisionError)
expect { 4/0 }.to raise_error(ZeroDivisionError, "divided by 0")
expect { throw :foo }.to throw_symbol
expect { throw :foo }.to throw_symbol(:foo)
expect { throw :foo, 7 }.to throw_symbol(:foo, 7)
</pre>
</div>
</div>
<div class="slide">
<h1>Built-in matchers</h1>
<h2>Exist</h2>
<div class="vcenter">
<pre class="brush: ruby">
obj.should exist # passes if obj.exist? returns true
</pre>
<p>This is the only use case I could think of:</p>
<pre class="brush: ruby">
Pathname("/etc/passwd").should exist
</pre>
</div>
</div>
<div class="slide">
<h1>Built-in matchers</h1>
<h2>Arbitrary blocks</h2>
<div class="vcenter">
<pre class="brush: ruby">
10.should satisfy { |v| v % 5 == 0 }
</pre>
<p>Not really recommended, because of the unfriendly failure message:</p>
<pre>
<span style="color:red">Failure/Error: 11.should satisfy { |v| v % 5 == 0 }</span>
<span style="color:red">expected 11 to satisfy block</span>
</pre>
<p>Which brings us to…</p>
</div>
</div>
<div class="slide">
<h1>Custom matchers</h1>
<pre class="brush: ruby">
RSpec::Matchers.define :be_a_multiple_of do |expected|
match do |actual|
actual % expected == 0
end
end
describe 10 do
it { should be_a_multiple_of(5) }
end
</pre>
<hr />
<pre>
10
<span style="color:green">should be a multiple of 5</span>
Finished in 0.04768 seconds
<span style="color:green">1 example, 0 failures</span>
</pre>
</div>
<div class="slide">
<h1>Hang on a minute…</h1>
<pre class="brush: ruby">
RSpec::Matchers.define :be_a_multiple_of do |expected|
match do |actual|
actual % expected == 0
end
end
describe 10 do
it { should be_a_multiple_of(5) }
end
</pre>
<hr />
<pre>
10
<span style="color:green">should be a multiple of 5</span>
Finished in 0.04768 seconds
<span style="color:green">1 example, 0 failures</span>
</pre>
<p>Firstly, note how a description has been automatically generated for us.</p>
<p>But what are we calling <code>should</code> on?</p>
</div>
<div class="slide">
<h1>Implicit subject</h1>
<p>The argument to <code>describe</code> becomes the subject:</p>
<pre class="brush: ruby">
describe 42 do
it { should be_even }
it { should_not be_zero }
end
</pre>
<p>If it’s a class, the subject is a new instance:</p>
<pre class="brush: ruby">
describe Array do
it { should be_empty }
it { should have(0).items }
end
</pre>
</div>
<div class="slide">
<h1>Implicit subject</h1>
<p>Access attributes of the subject with <code>its</code>:</p>
<pre class="brush: ruby">
describe [1, 2, 3, 3] do
its(:size) { should == 4 }
its("uniq.size") { should == 3 }
end
</pre>
<hr />
<pre>
1233
size
<span style="color:green">should == 4</span>
uniq.size
<span style="color:green">should == 3</span>
Finished in 0.00279 seconds
<span style="color:green">2 examples, 0 failures</span>
</pre>
<p>That description (from <code>to_s</code>) could be better...</p>
</div>
<div class="slide">
<h1>Explicit subject</h1>
<p>You can set the subject explicitly using a block:</p>
<pre class="brush: ruby">
describe "An array containing 1, 2, 3, 3" do
subject { [1, 2, 3, 3] }
its(:size) { should == 4 }
end
</pre>
<p>And access it in examples as <code>subject</code>:</p>
<pre class="brush: ruby">
describe Array do
it "is empty" do
subject.should be_empty
end
end
</pre>
</div>
<div class="slide">
<h1>Custom matchers</h1>
<p>You can override the description and failure messages:</p>
<pre class="brush: ruby">
RSpec::Matchers.define :be_a_multiple_of do |expected|
match do |actual|
actual % expected == 0
end
failure_message_for_should do |actual|
"expected that #{actual} would be a multiple of #{expected}"
end
failure_message_for_should_not do |actual|
"expected that #{actual} would not be a multiple of #{expected}"
end
description do
"be multiple of #{expected}"
end
end
</pre>
</div>
<div class="slide">
<h1>Custom matchers</h1>
<p>Not all matchers take an argument:</p>
<pre class="brush: ruby">
RSpec::Matchers.define :be_a_multiple_of_3 do
match do |number|
number % 3 == 0
end
end
</pre>
</div>
<div class="slide">
<h1>Shoulda matchers</h1>
<p><a href="https://github.com/thoughtbot/shoulda">Shoulda</a> provides a whole bunch of Rails matchers, including:</p>
<pre class="brush: ruby">
it { should render_template(:show) }
it { should assign_to(:user).with(@user) }
it { should redirect_to(users_path) }
it { should render_with_layout(:special) }
it { should respond_with(:success) }
it { should route(:get, "/posts/new").to(:action => :new) }
it { should set_the_flash.to(/created/i) }
it { should validate_presence_of(:name) }
it { should_not allow_mass_assignment_of(:password) }
it { should have_one(:profile) }
it { should have_db_column(:salary).of_type(:decimal) }
</pre>
</div>
<div class="slide">
<h1>Speaking of shoulda…</h1>
<blockquote>Shoulda is focusing on the RSpec/Shoulda combination and will
primarily support that combination of tools, moving away from
Test::Unit.</blockquote>
<p><em><a href="http://robots.thoughtbot.com/post/701863189/shoulda-rails3-and-beyond">Thoughtbot blog</a>, June 2010</em></p>
</div>
<div class="slide">
<h1>Let there be let</h1>
<p>These examples have some duplication…</p>
<pre class="brush: ruby">
describe BowlingGame do
it "scores all gutters with 0" do
game = BowlingGame.new
20.times { game.roll(0) }
game.score.should == 0
end
it "scores all 1s with 20" do
game = BowlingGame.new
20.times { game.roll(1) }
game.score.should == 20
end
end
</pre>
</div>
<div class="slide">