rspec-expectations 2.13.0 → 2.14.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog.md +34 -0
- data/README.md +43 -87
- data/features/README.md +8 -9
- data/features/built_in_matchers/README.md +41 -41
- data/features/built_in_matchers/be_within.feature +3 -3
- data/features/built_in_matchers/expect_change.feature +6 -6
- data/features/built_in_matchers/expect_error.feature +2 -2
- data/features/built_in_matchers/start_with.feature +1 -1
- data/features/built_in_matchers/throw_symbol.feature +11 -11
- data/features/built_in_matchers/yield.feature +18 -3
- data/features/custom_matchers/define_diffable_matcher.feature +1 -1
- data/features/custom_matchers/define_matcher_outside_rspec.feature +6 -6
- data/features/custom_matchers/define_matcher_with_fluent_interface.feature +1 -1
- data/features/customized_message.feature +1 -1
- data/features/support/env.rb +10 -1
- data/features/syntax_configuration.feature +3 -0
- data/features/test_frameworks/test_unit.feature +15 -17
- data/lib/rspec/expectations.rb +1 -1
- data/lib/rspec/expectations/deprecation.rb +12 -33
- data/lib/rspec/expectations/differ.rb +25 -7
- data/lib/rspec/expectations/expectation_target.rb +7 -8
- data/lib/rspec/expectations/extensions/object.rb +2 -12
- data/lib/rspec/expectations/fail_with.rb +11 -1
- data/lib/rspec/expectations/handler.rb +11 -6
- data/lib/rspec/expectations/syntax.rb +2 -2
- data/lib/rspec/expectations/version.rb +1 -1
- data/lib/rspec/matchers.rb +134 -144
- data/lib/rspec/matchers/be_close.rb +1 -1
- data/lib/rspec/matchers/built_in/be_within.rb +1 -1
- data/lib/rspec/matchers/built_in/have.rb +20 -4
- data/lib/rspec/matchers/built_in/raise_error.rb +23 -7
- data/lib/rspec/matchers/built_in/yield.rb +78 -3
- data/lib/rspec/matchers/operator_matcher.rb +1 -1
- data/lib/rspec/matchers/test_unit_integration.rb +11 -0
- data/spec/rspec/expectations/differ_spec.rb +27 -5
- data/spec/rspec/expectations/expectation_target_spec.rb +10 -3
- data/spec/rspec/expectations/extensions/kernel_spec.rb +5 -5
- data/spec/rspec/expectations/fail_with_spec.rb +19 -0
- data/spec/rspec/expectations/handler_spec.rb +42 -21
- data/spec/rspec/expectations/syntax_spec.rb +45 -3
- data/spec/rspec/matchers/be_close_spec.rb +6 -6
- data/spec/rspec/matchers/be_spec.rb +36 -36
- data/spec/rspec/matchers/be_within_spec.rb +4 -0
- data/spec/rspec/matchers/change_spec.rb +6 -6
- data/spec/rspec/matchers/configuration_spec.rb +57 -89
- data/spec/rspec/matchers/description_generation_spec.rb +1 -1
- data/spec/rspec/matchers/exist_spec.rb +9 -9
- data/spec/rspec/matchers/has_spec.rb +1 -1
- data/spec/rspec/matchers/have_spec.rb +12 -2
- data/spec/rspec/matchers/include_matcher_integration_spec.rb +2 -2
- data/spec/rspec/matchers/include_spec.rb +4 -4
- data/spec/rspec/matchers/match_array_spec.rb +1 -1
- data/spec/rspec/matchers/match_spec.rb +1 -1
- data/spec/rspec/matchers/raise_error_spec.rb +189 -99
- data/spec/rspec/matchers/respond_to_spec.rb +4 -4
- data/spec/rspec/matchers/satisfy_spec.rb +1 -1
- data/spec/rspec/matchers/start_with_end_with_spec.rb +2 -2
- data/spec/rspec/matchers/yield_spec.rb +81 -4
- data/spec/spec_helper.rb +1 -1
- metadata +10 -12
data/Changelog.md
CHANGED
@@ -1,3 +1,37 @@
|
|
1
|
+
### 2.14.0.rc1 / 2013-05-27
|
2
|
+
[full changelog](http://github.com/rspec/rspec-expectations/compare/v2.13.0...v2.14.0.rc1)
|
3
|
+
|
4
|
+
Enhancements
|
5
|
+
|
6
|
+
* Enhance `yield_control` so that you can specify an exact or relative
|
7
|
+
number of times: `expect { }.to yield_control.exactly(3).times`,
|
8
|
+
`expect { }.to yield_control.at_least(2).times`, etc (Bartek
|
9
|
+
Borkowski).
|
10
|
+
* Make the differ that is used when an expectation fails better handle arrays
|
11
|
+
by splitting each element of the array onto its own line. (Sam Phippen)
|
12
|
+
* Accept duck-typed strings that respond to `:to_str` as expectation messages.
|
13
|
+
(Toby Ovod-Everett)
|
14
|
+
|
15
|
+
Bug fixes
|
16
|
+
|
17
|
+
* Fix differ to not raise errors when dealing with differently-encoded
|
18
|
+
strings (Jon Rowe).
|
19
|
+
* Fix `expect(something).to be_within(x).percent_of(y)` where x and y are both
|
20
|
+
integers (Sam Phippen).
|
21
|
+
* Fix `have` matcher to handle the fact that on ruby 2.0,
|
22
|
+
`Enumerator#size` may return nil (Kenta Murata).
|
23
|
+
* Fix `expect { raise s }.to raise_error(s)` where s is an error instance
|
24
|
+
on ruby 2.0 (Sam Phippen).
|
25
|
+
* Fix `expect(object).to raise_error` passing. This now warns the user and
|
26
|
+
fails the spec (tomykaira).
|
27
|
+
|
28
|
+
Deprecations
|
29
|
+
|
30
|
+
* Deprecate `expect { }.not_to raise_error(SpecificErrorClass)` or
|
31
|
+
`expect { }.not_to raise_error("some specific message")`. Using
|
32
|
+
these was prone to hiding failures as they would allow _any other
|
33
|
+
error_ to pass. (Sam Phippen and David Chelimsky)
|
34
|
+
|
1
35
|
### 2.13.0 / 2013-02-23
|
2
36
|
[full changelog](http://github.com/rspec/rspec-expectations/compare/v2.12.1...v2.13.0)
|
3
37
|
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
RSpec::Expectations lets you express expected outcomes on an object in an
|
4
4
|
example.
|
5
5
|
|
6
|
-
account.balance.
|
6
|
+
expect(account.balance).to eq(Money.new(37.42, :USD))
|
7
7
|
|
8
8
|
## Install
|
9
9
|
|
@@ -38,8 +38,7 @@ describe Order do
|
|
38
38
|
end
|
39
39
|
```
|
40
40
|
|
41
|
-
The `describe` and `it` methods come from rspec-core. The `Order`, `LineItem`,
|
42
|
-
and `Item` classes would be from _your_ code. The last line of the example
|
41
|
+
The `describe` and `it` methods come from rspec-core. The `Order`, `LineItem`, `Item` and `Money` classes would be from _your_ code. The last line of the example
|
43
42
|
expresses an expected outcome. If `order.total == Money.new(5.55, :USD)`, then
|
44
43
|
the example passes. If not, it fails with a message like:
|
45
44
|
|
@@ -51,52 +50,50 @@ the example passes. If not, it fails with a message like:
|
|
51
50
|
### Equivalence
|
52
51
|
|
53
52
|
```ruby
|
54
|
-
actual.
|
55
|
-
actual.
|
56
|
-
actual.should eql(expected) # passes if actual.eql?(expected)
|
53
|
+
expect(actual).to eq(expected) # passes if actual == expected
|
54
|
+
expect(actual).to eql(expected) # passes if actual.eql?(expected)
|
57
55
|
```
|
58
56
|
|
59
|
-
Note:
|
60
|
-
useless context" warning when the `==` matcher is used anywhere but the
|
61
|
-
last statement of an example.
|
57
|
+
Note: The new `expect` syntax no longer supports `==` matcher.
|
62
58
|
|
63
59
|
### Identity
|
64
60
|
|
65
61
|
```ruby
|
66
|
-
actual.
|
67
|
-
actual.
|
62
|
+
expect(actual).to be(expected) # passes if actual.equal?(expected)
|
63
|
+
expect(actual).to equal(expected) # passes if actual.equal?(expected)
|
68
64
|
```
|
69
65
|
|
70
66
|
### Comparisons
|
71
67
|
|
72
68
|
```ruby
|
73
|
-
actual.
|
74
|
-
actual.
|
75
|
-
actual.
|
76
|
-
actual.
|
77
|
-
actual.
|
69
|
+
expect(actual).to be > expected
|
70
|
+
expect(actual).to be >= expected
|
71
|
+
expect(actual).to be <= expected
|
72
|
+
expect(actual).to be < expected
|
73
|
+
expect(actual).to be_within(delta).of(expected)
|
78
74
|
```
|
79
75
|
|
80
76
|
### Regular expressions
|
81
77
|
|
82
78
|
```ruby
|
83
|
-
actual.
|
84
|
-
actual.should =~ /expression/
|
79
|
+
expect(actual).to match(/expression/)
|
85
80
|
```
|
86
81
|
|
82
|
+
Note: The new `expect` syntax no longer supports `=~` matcher.
|
83
|
+
|
87
84
|
### Types/classes
|
88
85
|
|
89
86
|
```ruby
|
90
|
-
actual.
|
91
|
-
actual.
|
87
|
+
expect(actual).to be_an_instance_of(expected)
|
88
|
+
expect(actual).to be_a_kind_of(expected)
|
92
89
|
```
|
93
90
|
|
94
91
|
### Truthiness
|
95
92
|
|
96
93
|
```ruby
|
97
|
-
actual.
|
98
|
-
actual.
|
99
|
-
actual.
|
94
|
+
expect(actual).to be_true # passes if actual is truthy (not nil or false)
|
95
|
+
expect(actual).to be_false # passes if actual is falsy (nil or false)
|
96
|
+
expect(actual).to be_nil # passes if actual is nil
|
100
97
|
```
|
101
98
|
|
102
99
|
### Expecting errors
|
@@ -134,92 +131,51 @@ expect { |b| { :a => 1, :b => 2 }.each(&b) }.to yield_successive_args([:a, 1], [
|
|
134
131
|
### Predicate matchers
|
135
132
|
|
136
133
|
```ruby
|
137
|
-
actual.
|
138
|
-
actual.
|
134
|
+
expect(actual).to be_xxx # passes if actual.xxx?
|
135
|
+
expect(actual).to have_xxx(:arg) # passes if actual.has_xxx?(:arg)
|
139
136
|
```
|
140
137
|
|
141
138
|
### Ranges (Ruby >= 1.9 only)
|
142
139
|
|
143
140
|
```ruby
|
144
|
-
(1..10).
|
141
|
+
expect(1..10).to cover(3)
|
145
142
|
```
|
146
143
|
|
147
144
|
### Collection membership
|
148
145
|
|
149
146
|
```ruby
|
150
|
-
actual.
|
151
|
-
actual.
|
152
|
-
actual.
|
147
|
+
expect(actual).to include(expected)
|
148
|
+
expect(actual).to start_with(expected)
|
149
|
+
expect(actual).to end_with(expected)
|
153
150
|
```
|
154
151
|
|
155
152
|
#### Examples
|
156
153
|
|
157
154
|
```ruby
|
158
|
-
[1,2,3].
|
159
|
-
[1,2,3].
|
160
|
-
[1,2,3].
|
161
|
-
[1,2,3].
|
162
|
-
[1,2,3].
|
163
|
-
[1,2,3].
|
164
|
-
{:a => 'b'}.
|
165
|
-
"this string".
|
166
|
-
"this string".
|
167
|
-
"this string".
|
155
|
+
expect([1,2,3]).to include(1)
|
156
|
+
expect([1,2,3]).to include(1, 2)
|
157
|
+
expect([1,2,3]).to start_with(1)
|
158
|
+
expect([1,2,3]).to start_with(1,2)
|
159
|
+
expect([1,2,3]).to end_with(3)
|
160
|
+
expect([1,2,3]).to end_with(2,3)
|
161
|
+
expect({:a => 'b'}).to include(:a => 'b')
|
162
|
+
expect("this string").to include("is str")
|
163
|
+
expect("this string").to start_with("this")
|
164
|
+
expect("this string").to end_with("ring")
|
168
165
|
```
|
169
166
|
|
170
|
-
## `
|
171
|
-
|
172
|
-
In addition to the `should` syntax, rspec-expectations supports
|
173
|
-
a new `expect` syntax as of version 2.11.0:
|
174
|
-
|
175
|
-
```ruby
|
176
|
-
expect(actual).to eq expected
|
177
|
-
expect(actual).to be > 3
|
178
|
-
expect([1, 2, 3]).to_not include 4
|
179
|
-
```
|
167
|
+
## `should` syntax
|
180
168
|
|
181
|
-
|
182
|
-
|
169
|
+
In addition to the `expect` syntax, rspec-expectations continues to support the
|
170
|
+
`should` syntax:
|
183
171
|
|
184
172
|
```ruby
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
# or
|
189
|
-
c.syntax = :should
|
190
|
-
# or
|
191
|
-
c.syntax = [:should, :expect]
|
192
|
-
end
|
193
|
-
end
|
173
|
+
actual.should eq expected
|
174
|
+
actual.should be > 3
|
175
|
+
[1, 2, 3].should_not include 4
|
194
176
|
```
|
195
177
|
|
196
|
-
See
|
197
|
-
[RSpec::Expectations::Syntax#expect](http://rubydoc.info/gems/rspec-expectations/RSpec/Expectations/Syntax:expect)
|
198
|
-
for more information.
|
199
|
-
|
200
|
-
### Motivation for `expect`
|
201
|
-
|
202
|
-
We added the `expect` syntax to resolve some edge case issues, most notably
|
203
|
-
that objects whose definitions wipe out all but a few methods were throwing
|
204
|
-
`should` and `should_not` away. `expect` solves that by not monkey patching
|
205
|
-
those methods onto `Kernel` (or any global object).
|
206
|
-
|
207
|
-
See
|
208
|
-
[http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax](http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax)
|
209
|
-
for a detailed explanation.
|
210
|
-
|
211
|
-
### One-liners
|
212
|
-
|
213
|
-
The one-liner syntax supported by
|
214
|
-
[rspec-core](http://rubydoc.info/gems/rspec-core) uses `should` even when
|
215
|
-
`config.syntax = :expect`. It reads better than the alternative, and does not
|
216
|
-
require a global monkey patch:
|
217
|
-
|
218
|
-
```ruby
|
219
|
-
describe User do
|
220
|
-
it { should validate_presence_of :email }
|
221
|
-
end
|
222
|
-
```
|
178
|
+
See [detailed information on the `should` syntax and its usage.](https://github.com/rspec/rspec-expectations/blob/master/Should.md)
|
223
179
|
|
224
180
|
## Also see
|
225
181
|
|
data/features/README.md
CHANGED
@@ -2,7 +2,7 @@ rspec-expectations is used to define expected outcomes.
|
|
2
2
|
|
3
3
|
describe Account do
|
4
4
|
it "has a balance of zero when first created" do
|
5
|
-
Account.new.balance.
|
5
|
+
expect(Account.new.balance).to eq(Money.new(0))
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
@@ -10,17 +10,16 @@ rspec-expectations is used to define expected outcomes.
|
|
10
10
|
|
11
11
|
The basic structure of an rspec expectation is:
|
12
12
|
|
13
|
-
actual.
|
14
|
-
actual.
|
13
|
+
expect(actual).to matcher(expected)
|
14
|
+
expect(actual).not_to matcher(expected)
|
15
15
|
|
16
|
-
|
16
|
+
Note: You can also use `expect(..).to_not` instead of `expect(..).not_to`.
|
17
|
+
One is an alias to the other, so you can use whichever reads better to you.
|
17
18
|
|
18
|
-
|
19
|
-
the system. These methods each accept a matcher as an argument. This allows
|
20
|
-
each matcher to work in a positive or negative mode:
|
19
|
+
#### Examples
|
21
20
|
|
22
|
-
5.
|
23
|
-
5.
|
21
|
+
expect(5).to eq(5)
|
22
|
+
expect(5).not_to eq(4)
|
24
23
|
|
25
24
|
## What is a matcher?
|
26
25
|
|
@@ -1,55 +1,55 @@
|
|
1
1
|
# Built-in Matchers
|
2
2
|
|
3
|
-
|
4
|
-
can be used with `
|
3
|
+
rspec-expectations ships with a number of built-in matchers.
|
4
|
+
Each matcher can be used with `expect(..).to` or `expect(..).not_to` to define
|
5
|
+
positive and negative expectations respectively on an object. Most matchers can
|
6
|
+
also be accessed using the `(...).should` and `(...).should_not` syntax, see
|
7
|
+
[using should syntax](https://github.com/rspec/rspec-expectations/blob/master/Should.md)
|
8
|
+
for why we recommend using `expect`.
|
5
9
|
|
6
|
-
|
7
|
-
|
10
|
+
e.g.
|
11
|
+
|
12
|
+
expect(result).to eq(3)
|
13
|
+
expect(list).not_to be_empty
|
14
|
+
pi.should be > 3
|
8
15
|
|
9
16
|
## Object identity
|
10
17
|
|
11
|
-
actual.
|
12
|
-
|
18
|
+
expect(actual).to be(expected) # passes if actual.equal?(expected)
|
19
|
+
|
13
20
|
## Object equivalence
|
14
21
|
|
15
|
-
actual.
|
22
|
+
expect(actual).to eq(expected) # passes if actual == expected
|
16
23
|
|
17
24
|
## Optional APIs for identity/equivalence
|
18
25
|
|
19
|
-
actual.
|
20
|
-
actual.
|
21
|
-
actual.should equal(expected) # passes if actual.equal?(expected)
|
26
|
+
expect(actual).to eql(expected) # passes if actual.eql?(expected)
|
27
|
+
expect(actual).to equal(expected) # passes if actual.equal?(expected)
|
22
28
|
|
23
|
-
# NOTE:
|
24
|
-
# actual.should != expected
|
25
|
-
# The reason is that Ruby 1.8 parses it as:
|
26
|
-
# !(actual.should.==(expected)),
|
27
|
-
# so by the time RSpec sees it it has no way to know that it's
|
28
|
-
# been negated. Use either of these instead:
|
29
|
-
# actual.should_not eq(expected)
|
30
|
-
# actual.should_not == expected
|
29
|
+
# NOTE: `expect` does not support `==` matcher.
|
31
30
|
|
32
31
|
## Comparisons
|
33
32
|
|
34
|
-
actual.
|
35
|
-
actual.
|
36
|
-
actual.
|
37
|
-
actual.
|
38
|
-
actual.
|
39
|
-
actual.
|
40
|
-
|
33
|
+
expect(actual).to be > expected
|
34
|
+
expect(actual).to be >= expected
|
35
|
+
expect(actual).to be <= expected
|
36
|
+
expect(actual).to be < expected
|
37
|
+
expect(actual).to match(/expression/)
|
38
|
+
expect(actual).to be_within(delta).of(expected)
|
39
|
+
|
40
|
+
# NOTE: `expect` does not support `=~` matcher.
|
41
41
|
|
42
42
|
## Types/classes
|
43
43
|
|
44
|
-
actual.
|
45
|
-
actual.
|
44
|
+
expect(actual).to be_instance_of(expected)
|
45
|
+
expect(actual).to be_kind_of(expected)
|
46
46
|
|
47
47
|
## Truthiness and existentialism
|
48
48
|
|
49
|
-
actual.
|
50
|
-
actual.
|
51
|
-
actual.
|
52
|
-
actual.
|
49
|
+
expect(actual).to be_true # passes if actual is truthy (not nil or false)
|
50
|
+
expect(actual).to be_false # passes if actual is falsy (nil or false)
|
51
|
+
expect(actual).to be_nil # passes if actual is nil
|
52
|
+
expect(actual).to be # passes if actual is truthy (not nil or false)
|
53
53
|
|
54
54
|
## Expecting errors
|
55
55
|
|
@@ -66,25 +66,25 @@ can be used with `should` or `should_not` e.g.
|
|
66
66
|
|
67
67
|
## Predicate matchers
|
68
68
|
|
69
|
-
actual.
|
70
|
-
actual.
|
69
|
+
expect(actual).to be_xxx # passes if actual.xxx?
|
70
|
+
expect(actual).to have_xxx(:arg) # passes if actual.has_xxx?(:arg)
|
71
71
|
|
72
72
|
### Examples
|
73
73
|
|
74
|
-
[].
|
75
|
-
|
74
|
+
expect([]).to be_empty
|
75
|
+
expect(:a => 1).to have_key(:a)
|
76
76
|
|
77
77
|
## Collection membership
|
78
78
|
|
79
|
-
actual.
|
79
|
+
expect(actual).to include(expected)
|
80
80
|
|
81
81
|
### Examples
|
82
82
|
|
83
|
-
[1,2,3].
|
84
|
-
[1,2,3].
|
85
|
-
|
86
|
-
"this string".
|
83
|
+
expect([1,2,3]).to include(1)
|
84
|
+
expect([1,2,3]).to include(1, 2)
|
85
|
+
expect(:a => 'b').to include(:a => 'b')
|
86
|
+
expect("this string").to include("is str")
|
87
87
|
|
88
88
|
## Ranges (1.9 only)
|
89
89
|
|
90
|
-
(1..10).
|
90
|
+
expect(1..10).to cover(3)
|
@@ -4,11 +4,11 @@ Feature: be_within matcher
|
|
4
4
|
Consider this irb session:
|
5
5
|
|
6
6
|
> radius = 3
|
7
|
-
=> 3
|
7
|
+
=> 3
|
8
8
|
> area_of_circle = radius * radius * Math::PI
|
9
|
-
=> 28.2743338823081
|
9
|
+
=> 28.2743338823081
|
10
10
|
> area_of_circle == 28.2743338823081
|
11
|
-
=> false
|
11
|
+
=> false
|
12
12
|
|
13
13
|
Instead, you should use the be_within matcher to check that the value
|
14
14
|
is within a delta of your expected value:
|
@@ -11,14 +11,14 @@ Feature: expect change
|
|
11
11
|
@count ||= 0
|
12
12
|
@count += 1
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
def count
|
16
16
|
@count ||= 0
|
17
17
|
end
|
18
18
|
end
|
19
19
|
end
|
20
20
|
"""
|
21
|
-
|
21
|
+
|
22
22
|
Scenario: expect change
|
23
23
|
Given a file named "spec/example_spec.rb" with:
|
24
24
|
"""ruby
|
@@ -45,13 +45,13 @@ Feature: expect change
|
|
45
45
|
require "counter"
|
46
46
|
|
47
47
|
describe Counter, "#increment" do
|
48
|
-
it "should not increment the count by 1 (using to_not)" do
|
49
|
-
expect { Counter.increment }.to_not change{Counter.count}
|
50
|
-
end
|
51
|
-
|
52
48
|
it "should not increment the count by 1 (using not_to)" do
|
53
49
|
expect { Counter.increment }.not_to change{Counter.count}
|
54
50
|
end
|
51
|
+
|
52
|
+
it "should not increment the count by 1 (using to_not)" do
|
53
|
+
expect { Counter.increment }.to_not change{Counter.count}
|
54
|
+
end
|
55
55
|
end
|
56
56
|
"""
|
57
57
|
When I run `rspec spec/example_spec.rb`
|