OSDN Git Service

Regular updates
[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 ### Compound expectations
83
84 ```rb
85 expect(1).to (be < 2).or be > 5
86 ```
87
88 Use `or`/`and` to string multiple matchers together. See: [Compound expectations](https://relishapp.com/rspec/rspec-expectations/docs/compound-expectations)
89
90 ### Comparison
91
92 ```rb
93 expect(x).to be value
94 expect(x).to satisfy { |arg| ... }
95 expect(x).to match /regexp/
96 ```
97
98 ### Predicate
99
100 ```rb
101 expect(x).to be_zero    # FixNum#zero?
102 expect(x).to be_empty   # Array#empty?
103 expect(x).to have_key   # Hash#has_key?
104 ```
105
106 ### Objects
107
108 ```rb
109 expect(obj).to be_an_instance_of MyClass
110 expect(obj).to be_a_kind_of MyClass
111 expect(obj).to respond_to :save!
112 ```
113
114 ### Control flow
115
116 ```rb
117 expect { user.save! }.to raise_error
118 expect { user.save! }.to raise_error(ExceptionName, /msg/)
119 expect { user.save! }.to throw :symbol
120 ```
121
122 ### Enumerables/arrays
123
124 ```rb
125 expect(list).to include(<object>)
126
127 expect(list).to have(1).things
128 expect(list).to have_at_least(2).things
129 expect(list).to have_at_most(3).things
130
131 expect(list).to have(2).errors_on(:field)
132 ```
133
134 ### Change
135
136 ```rb
137 expect { thing.approve! }.to \
138   change(thing, :status)
139   .from(Status::AWAITING_APPROVAL)
140   .to(Status::APPROVED)
141
142 expect { thing.destroy }.to \
143   change(Thing, :count)
144   .by(-1)
145 ```
146
147 ## Doubles
148
149 ```rb
150 book = double('book')
151 book = instance_double('Book', pages: 250)
152 ```
153
154 ### Method stubs
155
156 ```rb
157 allow(die).to receive(:roll)
158 allow(die).to receive(:roll) { 3 }
159 allow_any_instance_of(Die).to receive(:roll)
160
161 expect(die).to receive(:roll)
162   .with(1)
163   .with(1, true)
164   .with(boolean)
165   .with(anything)
166   .with(any_args)
167   .with(1, any_args)
168   .with(no_args)
169   .with(hash_including(a: 1))
170   .with(hash_excluding(a: 1))
171   .with(array_including(:a, :b))
172   .with(array_excluding(:a, :b))
173   .with(instance_of(Fixnum))
174   .with(kind_of(Numeric))
175   .with(<matcher>)
176
177   .once
178   .twice
179   .exactly(n).times
180   .at_least(:once)
181   .at_least(:twice)
182   .at_least(n).times
183   .at_most(:once)
184   .at_most(:twice)
185   .at_most(n).times
186 ```
187
188 https://relishapp.com/rspec/rspec-mocks/docs
189
190 ## Spec helpers
191
192 ```rb
193 module UserSpecHelper
194   def valid_user_attributes
195     { :email => "joe@bloggs.com",
196       :username => "joebloggs",
197       :password => "abcdefg"}
198   end
199 end
200 ```
201
202 ```rb
203 describe User do
204   include UserSpecHelper
205
206   ...
207 end
208 ```