OSDN Git Service

9c4301f6bb83809ea953364fdfa871004f1a8dc6
[twpd/master.git] / rspec.md
1 ---
2 title: RSpec
3 category: Ruby
4 ---
5
6 ### Invoking tests
7
8 ```sh
9 rake -T spec      # List spec tasks
10
11 rake spec         # Run all
12
13 rake spec/models/mymodel_spec.rb
14 rake spec/models/mymodel_spec.rb:27
15 ```
16
17 ## Writing tests
18
19 ```rb
20 describe "A User (in general)" do
21   include UserSpecHelper
22
23   subject { Person.new }
24
25   let(:admin) { Person.new(role: :admin) }
26
27   context "setter methods" do
28     it "should do this" do
29       pending "some other thing"
30
31       expect(subject.name).to eq 'x'
32     end
33   end
34 end
35 ```
36
37 ### Before/after
38
39 ```rb
40 before :each do
41   # before all tests
42 end
43
44 before do
45   # before this suite
46 end
47
48 after do
49   # after this suite
50 end
51 ```
52
53 ### Subjects
54
55 ```rb
56 subject { CheckingAccount.new }
57 it { is_expected.to be_empty }
58
59 # also names: subject(:account) { ... }
60 ```
61
62 ## Expectations
63
64 ```rb
65 target.should eq 1
66 target.should_not eq 1
67
68 expect(target).to eq 1
69 expect(target).not_to eq 1
70 ```
71
72 ### Numeric
73
74 ```rb
75 expect(5).to be < 6
76 expect(5).to == 5
77 expect(5).to equal value
78 expect(5).to be_between(1, 10)
79 expect(5).to be_within(0.05).of value
80 ```
81
82 ### Comparison
83
84 ```rb
85 expect(x).to be value
86 expect(x).to satisfy { |arg| ... }
87 expect(x).to match /regexp/
88 ```
89
90 ### Predicate
91
92 ```rb
93 expect(x).to be_zero    # FixNum#zero?
94 expect(x).to be_empty   # Array#empty?
95 expect(x).to have_key   # Hash#has_key?
96 ```
97
98 ### Objects
99
100 ```rb
101 expect(obj).to be_an_instance_of MyClass
102 expect(obj).to be_a_kind_of MyClass
103 expect(obj).to respond_to :save!
104 ```
105
106 ### Control flow
107
108 ```rb
109 expect { user.save! }.to raise_error
110 expect { user.save! }.to raise_error(ExceptionName, /msg/)
111 expect { user.save! }.to throw :symbol
112 ```
113
114 ### Enumerables/arrays
115
116 ```rb
117 expect(list).to include(<object>)
118
119 expect(list).to have(1).things
120 expect(list).to have_at_least(2).things
121 expect(list).to have_at_most(3).things
122
123 expect(list).to have(2).errors_on(:field)
124 ```
125
126 ### Change
127
128 ```rb
129 expect { thing.approve! }.to \
130   change(thing, :status)
131   .from(Status::AWAITING_APPROVAL)
132   .to(Status::APPROVED)
133
134 expect { thing.destroy }.to \
135   change(Thing, :count)
136   .by(-1)
137 ```
138
139 ## Doubles
140
141 ```rb
142 book = double('book')
143 book = instance_double('Book', pages: 250)
144 ```
145
146 ### Method stubs
147
148 ```rb
149 allow(die).to receive(:roll)
150 allow(die).to receive(:roll) { 3 }
151 allow_any_instance_of(Die).to receive(:roll)
152
153 expect(die).to receive(:roll)
154   .with(1)
155   .with(1, true)
156   .with(boolean)
157   .with(anything)
158   .with(any_args)
159   .with(1, any_args)
160   .with(no_args)
161   .with(hash_including(a: 1))
162   .with(hash_excluding(a: 1))
163   .with(array_including(:a, :b))
164   .with(array_excluding(:a, :b))
165   .with(instance_of(Fixnum))
166   .with(kind_of(Numeric))
167   .with(<matcher>)
168
169   .once
170   .twice
171   .exactly(n).times
172   .at_least(:once)
173   .at_least(:twice)
174   .at_least(n).times
175   .at_most(:once)
176   .at_most(:twice)
177   .at_most(n).times
178 ```
179
180 https://relishapp.com/rspec/rspec-mocks/docs
181
182 ## Spec helpers
183
184 ```rb
185 module UserSpecHelper
186   def valid_user_attributes
187     { :email => "joe@bloggs.com",
188       :username => "joebloggs",
189       :password => "abcdefg"}
190   end
191 end
192 ```
193
194 ```rb
195 describe User do
196   include UserSpecHelper
197
198   ...
199 end
200 ```