rspec-expectations 2.12.1 → 2.13.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Changelog.md +31 -0
- data/README.md +1 -1
- data/features/built_in_matchers/be.feature +6 -4
- data/features/built_in_matchers/be_within.feature +3 -1
- data/features/built_in_matchers/cover.feature +2 -0
- data/features/built_in_matchers/end_with.feature +2 -0
- data/features/built_in_matchers/equality.feature +9 -15
- data/features/built_in_matchers/exist.feature +2 -0
- data/features/built_in_matchers/expect_error.feature +14 -8
- data/features/built_in_matchers/have.feature +11 -5
- data/features/built_in_matchers/include.feature +53 -0
- data/features/built_in_matchers/match.feature +2 -0
- data/features/built_in_matchers/operators.feature +17 -11
- data/features/built_in_matchers/predicates.feature +21 -13
- data/features/built_in_matchers/respond_to.feature +7 -1
- data/features/built_in_matchers/satisfy.feature +2 -0
- data/features/built_in_matchers/start_with.feature +2 -0
- data/features/built_in_matchers/throw_symbol.feature +6 -0
- data/features/built_in_matchers/types.feature +8 -6
- data/lib/rspec/expectations/deprecation.rb +1 -1
- data/lib/rspec/expectations/differ.rb +8 -8
- data/lib/rspec/expectations/fail_with.rb +17 -3
- data/lib/rspec/expectations/syntax.rb +46 -0
- data/lib/rspec/expectations/version.rb +1 -1
- data/lib/rspec/matchers/built_in/be.rb +7 -3
- data/lib/rspec/matchers/built_in/be_within.rb +13 -4
- data/lib/rspec/matchers/built_in/change.rb +2 -2
- data/lib/rspec/matchers/built_in/equal.rb +5 -1
- data/lib/rspec/matchers/built_in/exist.rb +1 -1
- data/lib/rspec/matchers/built_in/have.rb +8 -8
- data/lib/rspec/matchers/built_in/include.rb +19 -3
- data/lib/rspec/matchers/built_in/respond_to.rb +1 -1
- data/lib/rspec/matchers/extensions/instance_eval_with_args.rb +1 -1
- data/lib/rspec/matchers/matcher.rb +4 -3
- data/lib/rspec/matchers/operator_matcher.rb +1 -1
- data/lib/rspec/matchers/pretty.rb +5 -1
- data/spec/rspec/expectations/differ_spec.rb +8 -15
- data/spec/rspec/expectations/expectation_target_spec.rb +18 -8
- data/spec/rspec/expectations/extensions/kernel_spec.rb +15 -15
- data/spec/rspec/expectations/fail_with_spec.rb +41 -16
- data/spec/rspec/expectations/handler_spec.rb +13 -13
- data/spec/rspec/expectations/syntax_spec.rb +70 -8
- data/spec/rspec/matchers/base_matcher_spec.rb +14 -12
- data/spec/rspec/matchers/be_close_spec.rb +1 -1
- data/spec/rspec/matchers/be_instance_of_spec.rb +14 -8
- data/spec/rspec/matchers/be_kind_of_spec.rb +12 -8
- data/spec/rspec/matchers/be_spec.rb +212 -148
- data/spec/rspec/matchers/be_within_spec.rb +91 -42
- data/spec/rspec/matchers/change_spec.rb +52 -38
- data/spec/rspec/matchers/configuration_spec.rb +19 -15
- data/spec/rspec/matchers/cover_spec.rb +19 -19
- data/spec/rspec/matchers/description_generation_spec.rb +86 -86
- data/spec/rspec/matchers/dsl_spec.rb +7 -7
- data/spec/rspec/matchers/eq_spec.rb +17 -11
- data/spec/rspec/matchers/eql_spec.rb +10 -10
- data/spec/rspec/matchers/equal_spec.rb +27 -9
- data/spec/rspec/matchers/exist_spec.rb +35 -21
- data/spec/rspec/matchers/has_spec.rb +33 -29
- data/spec/rspec/matchers/have_spec.rb +165 -151
- data/spec/rspec/matchers/include_matcher_integration_spec.rb +30 -0
- data/spec/rspec/matchers/include_spec.rb +282 -124
- data/spec/rspec/matchers/match_array_spec.rb +90 -49
- data/spec/rspec/matchers/match_spec.rb +21 -21
- data/spec/rspec/matchers/matcher_spec.rb +85 -48
- data/spec/rspec/matchers/matchers_spec.rb +12 -6
- data/spec/rspec/matchers/method_missing_spec.rb +5 -1
- data/spec/rspec/matchers/operator_matcher_spec.rb +216 -237
- data/spec/rspec/matchers/raise_error_spec.rb +132 -132
- data/spec/rspec/matchers/respond_to_spec.rb +109 -112
- data/spec/rspec/matchers/satisfy_spec.rb +16 -16
- data/spec/rspec/matchers/start_with_end_with_spec.rb +36 -32
- data/spec/rspec/matchers/throw_symbol_spec.rb +24 -24
- data/spec/rspec/matchers/yield_spec.rb +7 -7
- data/spec/spec_helper.rb +46 -19
- data/spec/support/in_sub_process.rb +27 -20
- metadata +81 -83
data/Changelog.md
CHANGED
@@ -1,3 +1,34 @@
|
|
1
|
+
### 2.13.0 / 2013-02-23
|
2
|
+
[full changelog](http://github.com/rspec/rspec-expectations/compare/v2.12.1...v2.13.0)
|
3
|
+
|
4
|
+
Enhancements
|
5
|
+
|
6
|
+
* Add support for percent deltas to `be_within` matcher:
|
7
|
+
`expect(value).to be_within(10).percent_of(expected)`
|
8
|
+
(Myron Marston).
|
9
|
+
* Add support to `include` matcher to allow it to be given a list
|
10
|
+
of matchers as the expecteds to match against (Luke Redpath).
|
11
|
+
|
12
|
+
Bug fixes
|
13
|
+
|
14
|
+
* Fix `change` matcher so that it dups strings in order to handle
|
15
|
+
mutated strings (Myron Marston).
|
16
|
+
* Fix `should be =~ /some regex/` / `expect(...).to be =~ /some regex/`.
|
17
|
+
Previously, these either failed with a confusing `undefined method
|
18
|
+
matches?' for false:FalseClass` error or were no-ops that didn't
|
19
|
+
actually verify anything (Myron Marston).
|
20
|
+
* Add compatibility for diff-lcs 1.2 and relax the version
|
21
|
+
constraint (Peter Goldstein).
|
22
|
+
* Fix DSL-generated matchers to allow multiple instances of the
|
23
|
+
same matcher in the same example to have different description
|
24
|
+
and failure messages based on the expected value (Myron Marston).
|
25
|
+
* Prevent `undefined method #split for Array` error when dumping
|
26
|
+
the diff of an array of multiline strings (Myron Marston).
|
27
|
+
* Don't blow up when comparing strings that are in an encoding
|
28
|
+
that is not ASCII compatible (Myron Marston).
|
29
|
+
* Remove confusing "Check the implementation of #==" message
|
30
|
+
printed for empty diffs (Myron Marston).
|
31
|
+
|
1
32
|
### 2.12.1 / 2012-12-15
|
2
33
|
[full changelog](http://github.com/rspec/rspec-expectations/compare/v2.12.0...v2.12.1)
|
3
34
|
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# RSpec Expectations [![Build Status](https://secure.travis-ci.org/rspec/rspec-expectations.png?branch=master)](http://travis-ci.org/rspec/rspec-expectations) [![Code Climate](https://codeclimate.com/
|
1
|
+
# RSpec Expectations [![Build Status](https://secure.travis-ci.org/rspec/rspec-expectations.png?branch=master)](http://travis-ci.org/rspec/rspec-expectations) [![Code Climate](https://codeclimate.com/github/rspec/rspec-expectations.png)](https://codeclimate.com/github/rspec/rspec-expectations)
|
2
2
|
|
3
3
|
RSpec::Expectations lets you express expected outcomes on an object in an
|
4
4
|
example.
|
@@ -2,10 +2,12 @@ Feature: "be" matchers
|
|
2
2
|
|
3
3
|
There are several related "be" matchers:
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
```ruby
|
6
|
+
obj.should be_true # passes if obj is truthy (not nil or false)
|
7
|
+
obj.should be_false # passes if obj is falsy (nil or false)
|
8
|
+
obj.should be_nil # passes if obj is nil
|
9
|
+
obj.should be # passes if obj is truthy (not nil or false)
|
10
|
+
```
|
9
11
|
|
10
12
|
Scenario: be_true matcher
|
11
13
|
Given a file named "be_true_spec.rb" with:
|
@@ -13,7 +13,9 @@ Feature: be_within matcher
|
|
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:
|
15
15
|
|
16
|
-
|
16
|
+
```ruby
|
17
|
+
area_of_circle.should be_within(0.1).of(28.3)
|
18
|
+
```
|
17
19
|
|
18
20
|
Note that the difference between the actual and expected values must be
|
19
21
|
smaller than your delta; if it is equal, the matcher will fail.
|
@@ -5,9 +5,11 @@ Feature: cover matcher
|
|
5
5
|
expected objects. This works on any object that responds to #cover? (such
|
6
6
|
as a Range):
|
7
7
|
|
8
|
+
```ruby
|
8
9
|
(1..10).should cover(5)
|
9
10
|
(1..10).should cover(4, 6)
|
10
11
|
(1..10).should_not cover(11)
|
12
|
+
```
|
11
13
|
|
12
14
|
Scenario: range usage
|
13
15
|
Given a file named "range_cover_matcher_spec.rb" with:
|
@@ -3,9 +3,11 @@ Feature: end_with matcher
|
|
3
3
|
Use the `end_with` matcher to specify that a string or array ends with the
|
4
4
|
expected characters or elements.
|
5
5
|
|
6
|
+
```ruby
|
6
7
|
"this string".should end_with "string"
|
7
8
|
"this string".should_not end_with "stringy"
|
8
9
|
[0, 1, 2].should end_with 1, 2
|
10
|
+
```
|
9
11
|
|
10
12
|
Scenario: string usage
|
11
13
|
Given a file named "example_spec.rb" with:
|
@@ -12,14 +12,18 @@ Feature: equality matchers
|
|
12
12
|
|
13
13
|
rspec-expectations ships with matchers that align with each of these methods:
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
15
|
+
```ruby
|
16
|
+
a.should equal(b) # passes if a.equal?(b)
|
17
|
+
a.should eql(b) # passes if a.eql?(b)
|
18
|
+
a.should == b # passes if a == b
|
19
|
+
```
|
18
20
|
|
19
21
|
It also ships with two matchers that have more of a DSL feel to them:
|
20
22
|
|
21
|
-
|
22
|
-
|
23
|
+
```ruby
|
24
|
+
a.should be(b) # passes if a.equal?(b)
|
25
|
+
a.should eq(b) # passes if a == b
|
26
|
+
```
|
23
27
|
|
24
28
|
These are a useful pair if you wish to avoid the warning that Ruby emits on
|
25
29
|
`a.should == b`
|
@@ -27,8 +31,6 @@ Feature: equality matchers
|
|
27
31
|
Scenario: compare using eq (==)
|
28
32
|
Given a file named "compare_using_eq.rb" with:
|
29
33
|
"""ruby
|
30
|
-
require 'spec_helper'
|
31
|
-
|
32
34
|
describe "a string" do
|
33
35
|
it "is equal to another string of the same value" do
|
34
36
|
"this string".should eq("this string")
|
@@ -51,8 +53,6 @@ Feature: equality matchers
|
|
51
53
|
Scenario: compare using ==
|
52
54
|
Given a file named "compare_using_==.rb" with:
|
53
55
|
"""ruby
|
54
|
-
require 'spec_helper'
|
55
|
-
|
56
56
|
describe "a string" do
|
57
57
|
it "is equal to another string of the same value" do
|
58
58
|
"this string".should == "this string"
|
@@ -75,8 +75,6 @@ Feature: equality matchers
|
|
75
75
|
Scenario: compare using eql (eql?)
|
76
76
|
Given a file named "compare_using_eql.rb" with:
|
77
77
|
"""ruby
|
78
|
-
require 'spec_helper'
|
79
|
-
|
80
78
|
describe "an integer" do
|
81
79
|
it "is equal to another integer of the same value" do
|
82
80
|
5.should eql(5)
|
@@ -98,8 +96,6 @@ Feature: equality matchers
|
|
98
96
|
Scenario: compare using equal (equal?)
|
99
97
|
Given a file named "compare_using_equal.rb" with:
|
100
98
|
"""ruby
|
101
|
-
require 'spec_helper'
|
102
|
-
|
103
99
|
describe "a string" do
|
104
100
|
it "is equal to itself" do
|
105
101
|
string = "this string"
|
@@ -122,8 +118,6 @@ Feature: equality matchers
|
|
122
118
|
Scenario: compare using be (equal?)
|
123
119
|
Given a file named "compare_using_be.rb" with:
|
124
120
|
"""ruby
|
125
|
-
require 'spec_helper'
|
126
|
-
|
127
121
|
describe "a string" do
|
128
122
|
it "is equal to itself" do
|
129
123
|
string = "this string"
|
@@ -3,7 +3,9 @@ Feature: exist matcher
|
|
3
3
|
The exist matcher is used to specify that something exists
|
4
4
|
(as indicated by #exist? or #exists?):
|
5
5
|
|
6
|
+
```ruby
|
6
7
|
obj.should exist # passes if obj.exist? or obj.exists?
|
8
|
+
```
|
7
9
|
|
8
10
|
Scenario: basic usage
|
9
11
|
Given a file named "exist_matcher_spec.rb" with:
|
@@ -3,11 +3,15 @@ Feature: raise_error matcher
|
|
3
3
|
Use the `raise_error` matcher to specify that a block of code raises an
|
4
4
|
error. The most basic form passes if any error is thrown:
|
5
5
|
|
6
|
-
|
6
|
+
```ruby
|
7
|
+
expect { raise StandardError }.to raise_error
|
8
|
+
```
|
7
9
|
|
8
10
|
You can use `raise_exception` instead if you prefer that wording:
|
9
11
|
|
10
|
-
|
12
|
+
```ruby
|
13
|
+
expect { 3 / 0 }.to raise_exception
|
14
|
+
```
|
11
15
|
|
12
16
|
`raise_error` and `raise_exception` are functionally interchangeable, so use
|
13
17
|
the one that makes the most sense to you in any given context.
|
@@ -15,12 +19,14 @@ Feature: raise_error matcher
|
|
15
19
|
In addition to the basic form, above, there are a number of ways to specify
|
16
20
|
details of an error/exception:
|
17
21
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
22
|
+
```ruby
|
23
|
+
expect { raise "oops" }.to raise_error
|
24
|
+
expect { raise "oops" }.to raise_error(RuntimeError)
|
25
|
+
expect { raise "oops" }.to raise_error("oops")
|
26
|
+
expect { raise "oops" }.to raise_error(/op/)
|
27
|
+
expect { raise "oops" }.to raise_error(RuntimeError, "oops")
|
28
|
+
expect { raise "oops" }.to raise_error(RuntimeError, /op/)
|
29
|
+
```
|
24
30
|
|
25
31
|
Scenario: expect any error
|
26
32
|
Given a file named "example_spec" with:
|
@@ -3,9 +3,11 @@ Feature: have(n).items matcher
|
|
3
3
|
RSpec provides several matchers that make it easy to set expectations about the
|
4
4
|
size of a collection. There are three basic forms:
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
6
|
+
```ruby
|
7
|
+
collection.should have(x).items
|
8
|
+
collection.should have_at_least(x).items
|
9
|
+
collection.should have_at_most(x).items
|
10
|
+
```
|
9
11
|
|
10
12
|
In addition, #have_exactly is provided as an alias to #have.
|
11
13
|
|
@@ -14,14 +16,18 @@ Feature: have(n).items matcher
|
|
14
16
|
the #items call is pure syntactic sugar. You can use anything you want here. These
|
15
17
|
are equivalent:
|
16
18
|
|
17
|
-
|
18
|
-
|
19
|
+
```ruby
|
20
|
+
collection.should have(x).items
|
21
|
+
collection.should have(x).things
|
22
|
+
```
|
19
23
|
|
20
24
|
You can also use this matcher on a non-collection object that returns a collection
|
21
25
|
from one of its methods. For example, Dir#entries returns an array, so you could
|
22
26
|
set an expectation using the following:
|
23
27
|
|
28
|
+
```ruby
|
24
29
|
Dir.new("my/directory").should have(7).entries
|
30
|
+
```
|
25
31
|
|
26
32
|
Scenario: have(x).items on a collection
|
27
33
|
Given a file named "have_items_spec.rb" with:
|
@@ -4,6 +4,7 @@ Feature: include matcher
|
|
4
4
|
expected objects. This works on any object that responds to #include? (such
|
5
5
|
as a string or array):
|
6
6
|
|
7
|
+
```ruby
|
7
8
|
"a string".should include("a")
|
8
9
|
"a string".should include("str")
|
9
10
|
"a string".should include("str", "g")
|
@@ -12,9 +13,11 @@ Feature: include matcher
|
|
12
13
|
[1, 2].should include(1)
|
13
14
|
[1, 2].should include(1, 2)
|
14
15
|
[1, 2].should_not include(17)
|
16
|
+
```
|
15
17
|
|
16
18
|
The matcher also provides flexible handling for hashes:
|
17
19
|
|
20
|
+
```ruby
|
18
21
|
{:a => 1, :b => 2}.should include(:a)
|
19
22
|
{:a => 1, :b => 2}.should include(:a, :b)
|
20
23
|
{:a => 1, :b => 2}.should include(:a => 1)
|
@@ -22,6 +25,7 @@ Feature: include matcher
|
|
22
25
|
{:a => 1, :b => 2}.should_not include(:c)
|
23
26
|
{:a => 1, :b => 2}.should_not include(:a => 2)
|
24
27
|
{:a => 1, :b => 2}.should_not include(:c => 3)
|
28
|
+
```
|
25
29
|
|
26
30
|
Scenario: array usage
|
27
31
|
Given a file named "array_include_matcher_spec.rb" with:
|
@@ -119,3 +123,52 @@ Feature: include matcher
|
|
119
123
|
"""
|
120
124
|
When I run `rspec hash_include_matcher_spec.rb`
|
121
125
|
Then the output should contain "13 failure"
|
126
|
+
|
127
|
+
Scenario: fuzzy usage with matchers
|
128
|
+
Given a file named "fuzzy_include_matcher_spec.rb" with:
|
129
|
+
"""
|
130
|
+
require 'ostruct'
|
131
|
+
|
132
|
+
class User < OpenStruct
|
133
|
+
def inspect
|
134
|
+
name
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
RSpec::Matchers.define :a_user_named do |expected|
|
139
|
+
match do |actual|
|
140
|
+
actual.is_a?(User) && (actual.name == expected)
|
141
|
+
end
|
142
|
+
description do
|
143
|
+
"a user named '#{expected}'"
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
describe "Collection of users" do
|
148
|
+
subject do
|
149
|
+
[User.new(:name => "Joe"),
|
150
|
+
User.new(:name => "Fred"),
|
151
|
+
User.new(:name => "John"),
|
152
|
+
User.new(:name => "Luke"),
|
153
|
+
User.new(:name => "David")]
|
154
|
+
end
|
155
|
+
|
156
|
+
it { should include( a_user_named "Joe" ) }
|
157
|
+
it { should include( a_user_named "Luke" ) }
|
158
|
+
it { should_not include( a_user_named "Richard" ) }
|
159
|
+
it { should_not include( a_user_named "Hayley" ) }
|
160
|
+
|
161
|
+
# deliberate failures
|
162
|
+
it { should include( a_user_named "Richard" ) }
|
163
|
+
it { should_not include( a_user_named "Fred" ) }
|
164
|
+
it { should include( a_user_named "Sarah" ) }
|
165
|
+
it { should_not include( a_user_named "Luke" ) }
|
166
|
+
end
|
167
|
+
"""
|
168
|
+
When I run `rspec fuzzy_include_matcher_spec.rb`
|
169
|
+
Then the output should contain all of these:
|
170
|
+
| 8 examples, 4 failures |
|
171
|
+
| expected [Joe, Fred, John, Luke, David] to include a user named 'Richard' |
|
172
|
+
| expected [Joe, Fred, John, Luke, David] not to include a user named 'Fred' |
|
173
|
+
| expected [Joe, Fred, John, Luke, David] to include a user named 'Sarah' |
|
174
|
+
| expected [Joe, Fred, John, Luke, David] not to include a user named 'Luke' |
|
@@ -4,10 +4,12 @@ Feature: match matcher
|
|
4
4
|
truthy (not false or nil) value. Regexp and String both provide a #match
|
5
5
|
method.
|
6
6
|
|
7
|
+
```ruby
|
7
8
|
"a string".should match(/str/) # passes
|
8
9
|
"a string".should match(/foo/) # fails
|
9
10
|
/foo/.should match("food") # passes
|
10
11
|
/foo/.should match("drinks") # fails
|
12
|
+
```
|
11
13
|
|
12
14
|
This is equivalent to using the =~ matcher (see the operator matchers
|
13
15
|
feature for more details).
|
@@ -4,25 +4,31 @@ Feature: operator matchers
|
|
4
4
|
operators. These pretty much work like you expect. For example, each of these
|
5
5
|
pass:
|
6
6
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
7
|
+
```ruby
|
8
|
+
7.should == 7
|
9
|
+
[1, 2, 3].should == [1, 2, 3]
|
10
|
+
"this is a string".should =~ /^this/
|
11
|
+
"this is a string".should_not =~ /^that/
|
12
|
+
String.should === "this is a string"
|
13
|
+
```
|
12
14
|
|
13
15
|
You can also use comparison operators combined with the "be" matcher like
|
14
16
|
this:
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
18
|
+
```ruby
|
19
|
+
37.should be < 100
|
20
|
+
37.should be <= 38
|
21
|
+
37.should be >= 2
|
22
|
+
37.should be > 7
|
23
|
+
```
|
20
24
|
|
21
25
|
RSpec also provides a `=~` matcher for arrays that disregards differences in
|
22
26
|
the ording between the actual and expected array. For example:
|
23
27
|
|
24
|
-
|
25
|
-
|
28
|
+
```ruby
|
29
|
+
[1, 2, 3].should =~ [2, 3, 1] # pass
|
30
|
+
[:a, :c, :b].should =~ [:a, :c] # fail
|
31
|
+
```
|
26
32
|
|
27
33
|
Scenario: numeric operator matchers
|
28
34
|
Given a file named "numeric_operator_matchers_spec.rb" with:
|
@@ -2,16 +2,20 @@ Feature: predicate matchers
|
|
2
2
|
|
3
3
|
Ruby objects commonly provide predicate methods:
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
5
|
+
```ruby
|
6
|
+
7.zero? # => false
|
7
|
+
0.zero? # => true
|
8
|
+
[1].empty? # => false
|
9
|
+
[].empty? # => true
|
10
|
+
{ :a => 5 }.has_key?(:b) # => false
|
11
|
+
{ :b => 5 }.has_key?(:b) # => true
|
12
|
+
```
|
11
13
|
|
12
14
|
You could use a basic equality matcher to set expectations on these:
|
13
15
|
|
14
|
-
|
16
|
+
```ruby
|
17
|
+
7.zero?.should == true # fails with "expected true, got false (using ==)"
|
18
|
+
```
|
15
19
|
|
16
20
|
...but RSpec provides dynamic predicate matchers that are more readable and
|
17
21
|
provide better failure output.
|
@@ -19,20 +23,24 @@ Feature: predicate matchers
|
|
19
23
|
For any predicate method, RSpec gives you a corresponding matcher. Simply
|
20
24
|
prefix the method with `be_` and remove the question mark. Examples:
|
21
25
|
|
22
|
-
|
23
|
-
|
24
|
-
|
26
|
+
```ruby
|
27
|
+
7.should_not be_zero # calls 7.zero?
|
28
|
+
[].should be_empty # calls [].empty?
|
29
|
+
x.should be_multiple_of(3) # calls x.multiple_of?(3)
|
30
|
+
```
|
25
31
|
|
26
32
|
Alternately, for a predicate method that begins with `has_` like
|
27
33
|
`Hash#has_key?`, RSpec allows you to use an alternate form since `be_has_key`
|
28
34
|
makes no sense.
|
29
35
|
|
30
|
-
|
31
|
-
|
36
|
+
```ruby
|
37
|
+
hash.should have_key(:foo) # calls hash.has_key?(:foo)
|
38
|
+
array.should_not have_odd_values # calls array.has_odd_values?
|
39
|
+
```
|
32
40
|
|
33
41
|
In either case, RSpec provides nice, clear error messages, such as:
|
34
42
|
|
35
|
-
|
43
|
+
`expected zero? to return true, got false`
|
36
44
|
|
37
45
|
Any arguments passed to the matcher will be passed on to the predicate method.
|
38
46
|
|
@@ -3,18 +3,24 @@ Feature: respond_to matcher
|
|
3
3
|
Use the respond_to matcher to specify details of an object's interface. In
|
4
4
|
its most basic form:
|
5
5
|
|
6
|
+
```ruby
|
6
7
|
obj.should respond_to(:foo) # pass if obj.respond_to?(:foo)
|
8
|
+
```
|
7
9
|
|
8
10
|
You can specify that an object responds to multiple messages in a single
|
9
11
|
statement with multiple arguments passed to the matcher:
|
10
|
-
|
12
|
+
|
13
|
+
```ruby
|
11
14
|
obj.should respond_to(:foo, :bar) # passes if obj.respond_to?(:foo) && obj.respond_to?(:bar)
|
15
|
+
```
|
12
16
|
|
13
17
|
If the number of arguments accepted by the method is important to you,
|
14
18
|
you can specify that as well:
|
15
19
|
|
20
|
+
```ruby
|
16
21
|
obj.should respond_to(:foo).with(1).argument
|
17
22
|
obj.should respond_to(:bar).with(2).arguments
|
23
|
+
```
|
18
24
|
|
19
25
|
Note that this matcher relies entirely upon #respond_to?. If an object
|
20
26
|
dynamically responds to a message via #method_missing, but does not indicate
|