rspec-expectations 3.0.0.beta1 → 3.0.0.beta2
Sign up to get free protection for your applications and to get access to all the features.
- data.tar.gz.sig +2 -2
- data/.yardopts +1 -0
- data/Changelog.md +138 -0
- data/README.md +75 -8
- data/features/README.md +2 -2
- data/features/built_in_matchers/README.md +12 -9
- data/features/built_in_matchers/comparisons.feature +2 -2
- data/features/built_in_matchers/contain_exactly.feature +46 -0
- data/features/built_in_matchers/expect_change.feature +2 -2
- data/features/built_in_matchers/include.feature +0 -48
- data/features/built_in_matchers/output.feature +70 -0
- data/features/composing_matchers.feature +250 -0
- data/features/compound_expectations.feature +45 -0
- data/features/custom_matchers/access_running_example.feature +1 -1
- data/features/custom_matchers/define_matcher.feature +6 -6
- data/features/custom_matchers/define_matcher_outside_rspec.feature +4 -8
- data/features/test_frameworks/{test_unit.feature → minitest.feature} +11 -11
- data/lib/rspec/expectations.rb +31 -42
- data/lib/rspec/expectations/diff_presenter.rb +141 -0
- data/lib/rspec/expectations/differ.rb +22 -132
- data/lib/rspec/expectations/encoded_string.rb +56 -0
- data/lib/rspec/expectations/expectation_target.rb +0 -30
- data/lib/rspec/expectations/fail_with.rb +2 -2
- data/lib/rspec/expectations/handler.rb +128 -31
- data/lib/rspec/expectations/minitest_integration.rb +16 -0
- data/lib/rspec/expectations/syntax.rb +4 -58
- data/lib/rspec/expectations/version.rb +1 -1
- data/lib/rspec/matchers.rb +298 -60
- data/lib/rspec/matchers/aliased_matcher.rb +35 -0
- data/lib/rspec/matchers/built_in.rb +37 -33
- data/lib/rspec/matchers/built_in/base_matcher.rb +25 -15
- data/lib/rspec/matchers/built_in/be.rb +23 -31
- data/lib/rspec/matchers/built_in/be_between.rb +55 -0
- data/lib/rspec/matchers/built_in/be_within.rb +15 -11
- data/lib/rspec/matchers/built_in/change.rb +198 -81
- data/lib/rspec/matchers/built_in/compound.rb +106 -0
- data/lib/rspec/matchers/built_in/contain_exactly.rb +245 -0
- data/lib/rspec/matchers/built_in/eq.rb +43 -4
- data/lib/rspec/matchers/built_in/eql.rb +2 -2
- data/lib/rspec/matchers/built_in/equal.rb +35 -18
- data/lib/rspec/matchers/built_in/has.rb +16 -15
- data/lib/rspec/matchers/built_in/include.rb +45 -23
- data/lib/rspec/matchers/built_in/match.rb +6 -3
- data/lib/rspec/matchers/built_in/operators.rb +103 -0
- data/lib/rspec/matchers/built_in/output.rb +108 -0
- data/lib/rspec/matchers/built_in/raise_error.rb +9 -15
- data/lib/rspec/matchers/built_in/respond_to.rb +5 -4
- data/lib/rspec/matchers/built_in/satisfy.rb +4 -3
- data/lib/rspec/matchers/built_in/start_and_end_with.rb +37 -16
- data/lib/rspec/matchers/built_in/throw_symbol.rb +6 -5
- data/lib/rspec/matchers/built_in/yield.rb +31 -29
- data/lib/rspec/matchers/composable.rb +138 -0
- data/lib/rspec/matchers/dsl.rb +330 -0
- data/lib/rspec/matchers/generated_descriptions.rb +6 -6
- data/lib/rspec/matchers/matcher_delegator.rb +33 -0
- data/lib/rspec/matchers/pretty.rb +13 -2
- data/spec/rspec/expectations/{differ_spec.rb → diff_presenter_spec.rb} +56 -36
- data/spec/rspec/expectations/encoded_string_spec.rb +74 -0
- data/spec/rspec/expectations/extensions/kernel_spec.rb +11 -11
- data/spec/rspec/expectations/fail_with_spec.rb +8 -8
- data/spec/rspec/expectations/handler_spec.rb +27 -49
- data/spec/rspec/expectations/minitest_integration_spec.rb +27 -0
- data/spec/rspec/expectations/syntax_spec.rb +17 -67
- data/spec/rspec/expectations_spec.rb +7 -52
- data/spec/rspec/matchers/aliased_matcher_spec.rb +48 -0
- data/spec/rspec/matchers/aliases_spec.rb +449 -0
- data/spec/rspec/matchers/{base_matcher_spec.rb → built_in/base_matcher_spec.rb} +24 -3
- data/spec/rspec/matchers/built_in/be_between_spec.rb +159 -0
- data/spec/rspec/matchers/{be_instance_of_spec.rb → built_in/be_instance_of_spec.rb} +0 -0
- data/spec/rspec/matchers/{be_kind_of_spec.rb → built_in/be_kind_of_spec.rb} +0 -0
- data/spec/rspec/matchers/{be_spec.rb → built_in/be_spec.rb} +76 -32
- data/spec/rspec/matchers/{be_within_spec.rb → built_in/be_within_spec.rb} +6 -2
- data/spec/rspec/matchers/{change_spec.rb → built_in/change_spec.rb} +310 -69
- data/spec/rspec/matchers/built_in/compound_spec.rb +292 -0
- data/spec/rspec/matchers/built_in/contain_exactly_spec.rb +441 -0
- data/spec/rspec/matchers/{cover_spec.rb → built_in/cover_spec.rb} +0 -0
- data/spec/rspec/matchers/built_in/eq_spec.rb +156 -0
- data/spec/rspec/matchers/{eql_spec.rb → built_in/eql_spec.rb} +2 -2
- data/spec/rspec/matchers/built_in/equal_spec.rb +106 -0
- data/spec/rspec/matchers/{exist_spec.rb → built_in/exist_spec.rb} +1 -1
- data/spec/rspec/matchers/{has_spec.rb → built_in/has_spec.rb} +39 -0
- data/spec/rspec/matchers/{include_spec.rb → built_in/include_spec.rb} +118 -109
- data/spec/rspec/matchers/{match_spec.rb → built_in/match_spec.rb} +30 -2
- data/spec/rspec/matchers/{operator_matcher_spec.rb → built_in/operators_spec.rb} +26 -26
- data/spec/rspec/matchers/built_in/output_spec.rb +165 -0
- data/spec/rspec/matchers/{raise_error_spec.rb → built_in/raise_error_spec.rb} +81 -11
- data/spec/rspec/matchers/{respond_to_spec.rb → built_in/respond_to_spec.rb} +0 -0
- data/spec/rspec/matchers/{satisfy_spec.rb → built_in/satisfy_spec.rb} +0 -0
- data/spec/rspec/matchers/{start_with_end_with_spec.rb → built_in/start_and_end_with_spec.rb} +82 -15
- data/spec/rspec/matchers/{throw_symbol_spec.rb → built_in/throw_symbol_spec.rb} +29 -10
- data/spec/rspec/matchers/{yield_spec.rb → built_in/yield_spec.rb} +90 -0
- data/spec/rspec/matchers/configuration_spec.rb +7 -39
- data/spec/rspec/matchers/description_generation_spec.rb +22 -6
- data/spec/rspec/matchers/dsl_spec.rb +838 -0
- data/spec/rspec/matchers/legacy_spec.rb +101 -0
- data/spec/rspec/matchers_spec.rb +74 -0
- data/spec/spec_helper.rb +35 -21
- data/spec/support/shared_examples.rb +26 -4
- metadata +172 -116
- metadata.gz.sig +3 -4
- checksums.yaml +0 -15
- checksums.yaml.gz.sig +0 -0
- data/features/built_in_matchers/match_array.feature +0 -37
- data/lib/rspec/expectations/errors.rb +0 -9
- data/lib/rspec/expectations/extensions.rb +0 -1
- data/lib/rspec/expectations/extensions/object.rb +0 -29
- data/lib/rspec/matchers/built_in/match_array.rb +0 -51
- data/lib/rspec/matchers/compatibility.rb +0 -14
- data/lib/rspec/matchers/matcher.rb +0 -301
- data/lib/rspec/matchers/method_missing.rb +0 -12
- data/lib/rspec/matchers/operator_matcher.rb +0 -99
- data/lib/rspec/matchers/test_unit_integration.rb +0 -11
- data/spec/rspec/matchers/eq_spec.rb +0 -60
- data/spec/rspec/matchers/equal_spec.rb +0 -78
- data/spec/rspec/matchers/include_matcher_integration_spec.rb +0 -30
- data/spec/rspec/matchers/match_array_spec.rb +0 -194
- data/spec/rspec/matchers/matcher_spec.rb +0 -706
- data/spec/rspec/matchers/matchers_spec.rb +0 -36
- data/spec/rspec/matchers/method_missing_spec.rb +0 -28
- data/spec/support/classes.rb +0 -56
- data/spec/support/in_sub_process.rb +0 -37
- data/spec/support/ruby_version.rb +0 -10
data.tar.gz.sig
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
|
2
|
-
{
|
1
|
+
b��y�8W����O~����(���^WK� "ڴj�z\�fF�~���0"�6��KSV�v/�t��K�rz[x��(�����n�BA?L�.=R���O}l=D�8�.��dI��S���~��4�Y����A+�����8��`��H�W��Oh"k���D�_�
|
2
|
+
_���{�0�]��e��Ƭj����*Pz@�d�o�cLt�t���C�;��F(J����6���<7�v�X�r�gmi��I�-�B0
|
data/.yardopts
CHANGED
data/Changelog.md
CHANGED
@@ -1,3 +1,101 @@
|
|
1
|
+
### 3.0.0.beta2 / 2014-02-17
|
2
|
+
[full changelog](http://github.com/rspec/rspec-expectations/compare/v3.0.0.beta1...v3.0.0.beta2)
|
3
|
+
|
4
|
+
Breaking Changes for 3.0.0:
|
5
|
+
|
6
|
+
* Remove deprecated support for accessing the `RSpec` constant using
|
7
|
+
`Rspec` or `Spec`. (Myron Marston)
|
8
|
+
* Remove deprecated `RSpec::Expectations.differ=`. (Myron Marston)
|
9
|
+
* Remove support for deprecated `expect(...).should`. (Myron Marston)
|
10
|
+
* Explicitly disallow `expect { }.not_to change { }` with `by`,
|
11
|
+
`by_at_least`, `by_at_most` or `to`. These have never been supported
|
12
|
+
but did not raise explicit errors. (Myron Marston)
|
13
|
+
* Provide `===` rather than `==` as an alias of `matches?` for
|
14
|
+
all matchers. The semantics of `===` are closer to an RSpec
|
15
|
+
matcher than `==`. (Myron Marston)
|
16
|
+
* Remove deprecated `RSpec::Matchers::OperatorMatcher` constant.
|
17
|
+
(Myron Marston)
|
18
|
+
* Make `RSpec::Expectations::ExpectationNotMetError` subclass
|
19
|
+
`Exception` rather than `StandardError` so they can bypass
|
20
|
+
a bare `rescue` in end-user code (e.g. when an expectation is
|
21
|
+
set from within a rspec-mocks stub implementation). (Myron Marston)
|
22
|
+
* Remove Test::Unit and Minitest 4.x integration. (Myron Marston)
|
23
|
+
|
24
|
+
Enhancements:
|
25
|
+
|
26
|
+
* Simplify the failure message of the `be` matcher when matching against:
|
27
|
+
`true`, `false` and `nil`. (Sam Phippen)
|
28
|
+
* Update matcher protocol and custom matcher DSL to better align
|
29
|
+
with the newer `expect` syntax. If you want your matchers to
|
30
|
+
maintain compatibility with multiple versions of RSpec, you can
|
31
|
+
alias the new names to the old. (Myron Marston)
|
32
|
+
* `failure_message_for_should` => `failure_message`
|
33
|
+
* `failure_message_for_should_not` => `failure_message_when_negated`
|
34
|
+
* `match_for_should` => `match`
|
35
|
+
* `match_for_should_not` => `match_when_negated`
|
36
|
+
* Improve generated descriptions from `change` matcher. (Myron Marston)
|
37
|
+
* Add support for compound matcher expressions using `and` and `or`.
|
38
|
+
Simply chain them off of any existing matcher to create an expression
|
39
|
+
like `expect(alphabet).to start_with("a").and end_with("z")`.
|
40
|
+
(Eloy Espinaco)
|
41
|
+
* Add `contain_exactly` as a less ambiguous version of `match_array`.
|
42
|
+
Note that it expects the expected array to be splatted as
|
43
|
+
individual args: `expect(array).to contain_exactly(1, 2)` is
|
44
|
+
the same as `expect(array).to match_array([1, 2])`. (Myron Marston)
|
45
|
+
* Update `contain_exactly`/`match_array` so that it can match against
|
46
|
+
other non-array collections (such as a `Set`). (Myron Marston)
|
47
|
+
* Update built-in matchers so that they can accept matchers as arguments
|
48
|
+
to allow you to compose matchers in arbitrary ways. (Myron Marston)
|
49
|
+
* Add `RSpec::Matchers::Composable` mixin that can be used to make
|
50
|
+
a custom matcher composable as well. Note that custom matchers
|
51
|
+
defined via `RSpec::Matchers.define` already have this. (Myron
|
52
|
+
Marston)
|
53
|
+
* Define noun-phrase aliases for built-in matchers, which can be
|
54
|
+
used when creating composed matcher expressions that read better
|
55
|
+
and provide better failure messages. (Myron Marston)
|
56
|
+
* Add `RSpec::Machers.alias_matcher` so users can define their own
|
57
|
+
matcher aliases. The `description` of the matcher will reflect the
|
58
|
+
alternate matcher name. (Myron Marston)
|
59
|
+
* Add explicit `be_between` matcher. `be_between` has worked for a
|
60
|
+
long time as a dynamic predicate matcher, but the failure message
|
61
|
+
was suboptimal. The new matcher provides a much better failure
|
62
|
+
message. (Erik Michaels-Ober)
|
63
|
+
* Enhance the `be_between` matcher to allow for `inclusive` or `exclusive`
|
64
|
+
comparison (e.g. inclusive of min/max or exclusive of min/max).
|
65
|
+
(Pedro Gimenez)
|
66
|
+
* Make failure message for `not_to be #{operator}` less confusing by
|
67
|
+
only saying it's confusing when comparison operators are used.
|
68
|
+
(Prathamesh Sonpatki)
|
69
|
+
* Improve failure message of `eq` matcher when `Time` or `DateTime`
|
70
|
+
objects are used so that the full sub-second precision is included.
|
71
|
+
(Thomas Holmes, Jeff Wallace)
|
72
|
+
* Add `output` matcher for expecting that a block outputs `to_stdout`
|
73
|
+
or `to_stderr`. (Luca Pette, Matthias Günther)
|
74
|
+
* Forward a provided block on to the `has_xyz?` method call when
|
75
|
+
the `have_xyz` matcher is used. (Damian Galarza)
|
76
|
+
* Provide integration with Minitest 5.x. Require
|
77
|
+
`rspec/expectations/minitest_integration` after loading minitest
|
78
|
+
to use rspec-expectations with minitest. (Myron Marston)
|
79
|
+
|
80
|
+
Bug Fixes:
|
81
|
+
|
82
|
+
* Fix wrong matcher descriptions with falsey expected value (yujinakayama)
|
83
|
+
* Fix `expect { }.not_to change { }.from(x)` so that the matcher only
|
84
|
+
passes if the starting value is `x`. (Tyler Rick, Myron Marston)
|
85
|
+
* Fix hash diffing, so that it colorizes properly and doesn't consider trailing
|
86
|
+
commas when performing the diff. (Jared Norman)
|
87
|
+
* Fix built-in matchers to fail normally rather than raising
|
88
|
+
`ArgumentError` when given an object of the wrong type to match
|
89
|
+
against, so that they work well in composite matcher expressions like
|
90
|
+
`expect([1.51, "foo"]).to include(a_string_matching(/foo/), a_value_within(0.1).of(1.5))`.
|
91
|
+
(Myron Marston)
|
92
|
+
|
93
|
+
Deprecations:
|
94
|
+
|
95
|
+
* Retain support for RSpec 2 matcher protocol (e.g. for matchers
|
96
|
+
in 3rd party extension gems like `shoulda`), but it will print
|
97
|
+
a deprecation warning. (Myron Marston)
|
98
|
+
|
1
99
|
### 3.0.0.beta1 / 2013-11-07
|
2
100
|
[full changelog](http://github.com/rspec/rspec-expectations/compare/v2.99.0.beta1...v3.0.0.beta1)
|
3
101
|
|
@@ -6,6 +104,10 @@ Breaking Changes for 3.0.0:
|
|
6
104
|
* Remove explicit support for 1.8.6. (Jon Rowe)
|
7
105
|
* Remove the deprecated `be_close` matcher, preferring `be_within` instead.
|
8
106
|
(Sam Phippen)
|
107
|
+
* Remove the deprecated `have`, `have_at_least` and `have_at_most` matchers.
|
108
|
+
You can continue using those matchers through https://github.com/rspec/rspec-collection_matchers,
|
109
|
+
or you can rewrite your expectations with something like
|
110
|
+
`expect(your_object.size).to eq(num)`. (Hugo Baraúna)
|
9
111
|
* Rename `be_true` and `be_false` to `be_truthy` and `be_falsey`. (Sam Phippen)
|
10
112
|
* Make `expect { }.to_not raise_error(SomeSpecificClass, message)`,
|
11
113
|
`expect { }.to_not raise_error(SomeSpecificClass)` and
|
@@ -17,6 +119,7 @@ Breaking Changes for 3.0.0:
|
|
17
119
|
available to the `match` block (or any of the others). Instead
|
18
120
|
`include` your helper module and define the helper method as an
|
19
121
|
instance method. (Myron Marston)
|
122
|
+
* Force upgrading Diff::LCS for encoding compatbility with diffs. (Jon Rowe)
|
20
123
|
|
21
124
|
Enhancements:
|
22
125
|
|
@@ -34,6 +137,7 @@ Bug Fixes:
|
|
34
137
|
|
35
138
|
* Allow `include` and `match` matchers to be used from within a
|
36
139
|
DSL-defined custom matcher's `match` block. (Myron Marston)
|
140
|
+
* Correct encoding error message on diff failure (Jon Rowe)
|
37
141
|
|
38
142
|
Deprecations:
|
39
143
|
|
@@ -41,6 +145,32 @@ Deprecations:
|
|
41
145
|
It will continue to work but will emit a deprecation warning in RSpec 3 if
|
42
146
|
you do not explicitly enable it. (Sam Phippen)
|
43
147
|
|
148
|
+
### 2.99.0.beta2 / 2014-02-17
|
149
|
+
[full changelog](http://github.com/rspec/rspec-expectations/compare/v2.99.0.beta1...v2.99.0.beta2)
|
150
|
+
|
151
|
+
Deprecations:
|
152
|
+
|
153
|
+
* Deprecate chaining `by`, `by_at_least`, `by_at_most` or `to` off of
|
154
|
+
`expect { }.not_to change { }`. The docs have always said these are
|
155
|
+
not supported for the negative form but now they explicitly raise
|
156
|
+
errors in RSpec 3. (Myron Marston)
|
157
|
+
* Change the semantics of `expect { }.not_to change { x }.from(y)`.
|
158
|
+
In RSpec 2.x, this expectation would only fail if `x` started with
|
159
|
+
the value of `y` and changed. If it started with a different value
|
160
|
+
and changed, it would pass. In RSpec 3, it will pass only if the
|
161
|
+
value starts at `y` and it does not change. (Myron Marston)
|
162
|
+
* Deprecate `matcher == value` as an alias for `matcher.matches?(value)`,
|
163
|
+
in favor of `matcher === value`. (Myron Marston)
|
164
|
+
* Deprecate `RSpec::Matchers::OperatorMatcher` in favor of
|
165
|
+
`RSpec::Matchers::BuiltIn::OperatorMatcher`. (Myron Marston)
|
166
|
+
* Deprecate auto-integration with Test::Unit and minitest.
|
167
|
+
Instead, include `RSpec::Matchers` in the appropriate test case
|
168
|
+
base class yourself. (Myron Marston)
|
169
|
+
* Deprecate treating `#expected` on a DSL-generated custom matcher
|
170
|
+
as an array when only 1 argument is passed to the matcher method.
|
171
|
+
In RSpec 3 it will be the single value in order to make diffs
|
172
|
+
work properly. (Jon Rowe)
|
173
|
+
|
44
174
|
### 2.99.0.beta1 / 2013-11-07
|
45
175
|
[full changelog](http://github.com/rspec/rspec-expectations/compare/v2.14.4...v2.99.0.beta1)
|
46
176
|
|
@@ -66,6 +196,14 @@ Deprecations
|
|
66
196
|
* RSpec 2.x allowed helper methods defined either way to be used for
|
67
197
|
either purpose, but RSpec 3.0 will not.
|
68
198
|
|
199
|
+
### 2.14.5 / 2014-02-01
|
200
|
+
[full changelog](http://github.com/rspec/rspec-expectations/compare/v2.14.4...v2.14.5)
|
201
|
+
|
202
|
+
Bug fixes
|
203
|
+
|
204
|
+
* Fix wrong matcher descriptions with falsey expected value
|
205
|
+
(yujinakayama)
|
206
|
+
|
69
207
|
### 2.14.4 / 2013-11-06
|
70
208
|
[full changelog](http://github.com/rspec/rspec-expectations/compare/v2.14.3...v2.14.4)
|
71
209
|
|
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/github/rspec/rspec-expectations.png)](https://codeclimate.com/github/rspec/rspec-expectations)
|
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.
|
@@ -71,7 +71,6 @@ expect(actual).to be >= expected
|
|
71
71
|
expect(actual).to be <= expected
|
72
72
|
expect(actual).to be < expected
|
73
73
|
expect(actual).to be_within(delta).of(expected)
|
74
|
-
expect(array).to match_array(expected)
|
75
74
|
```
|
76
75
|
|
77
76
|
### Regular expressions
|
@@ -152,21 +151,27 @@ expect(1..10).to cover(3)
|
|
152
151
|
expect(actual).to include(expected)
|
153
152
|
expect(actual).to start_with(expected)
|
154
153
|
expect(actual).to end_with(expected)
|
154
|
+
|
155
|
+
expect(actual).to contain_exactly(individual, items)
|
156
|
+
# ...which is the same as:
|
157
|
+
expect(actual).to match_array(expected_array)
|
155
158
|
```
|
156
159
|
|
157
160
|
#### Examples
|
158
161
|
|
159
162
|
```ruby
|
160
|
-
expect([1,2,3]).to include(1)
|
161
|
-
expect([1,2,3]).to include(1, 2)
|
162
|
-
expect([1,2,3]).to start_with(1)
|
163
|
-
expect([1,2,3]).to start_with(1,2)
|
164
|
-
expect([1,2,3]).to end_with(3)
|
165
|
-
expect([1,2,3]).to end_with(2,3)
|
163
|
+
expect([1, 2, 3]).to include(1)
|
164
|
+
expect([1, 2, 3]).to include(1, 2)
|
165
|
+
expect([1, 2, 3]).to start_with(1)
|
166
|
+
expect([1, 2, 3]).to start_with(1, 2)
|
167
|
+
expect([1, 2, 3]).to end_with(3)
|
168
|
+
expect([1, 2, 3]).to end_with(2, 3)
|
166
169
|
expect({:a => 'b'}).to include(:a => 'b')
|
167
170
|
expect("this string").to include("is str")
|
168
171
|
expect("this string").to start_with("this")
|
169
172
|
expect("this string").to end_with("ring")
|
173
|
+
expect([1, 2, 3]).to contain_exactly(2, 3, 1)
|
174
|
+
expect([1, 2, 3]).to match_array([3, 2, 1])
|
170
175
|
```
|
171
176
|
|
172
177
|
## `should` syntax
|
@@ -182,6 +187,68 @@ actual.should be > 3
|
|
182
187
|
|
183
188
|
See [detailed information on the `should` syntax and its usage.](https://github.com/rspec/rspec-expectations/blob/master/Should.md)
|
184
189
|
|
190
|
+
## Compound Matcher Expressions
|
191
|
+
|
192
|
+
You can also create compound matcher expressions using `and` or `or`:
|
193
|
+
|
194
|
+
``` ruby
|
195
|
+
expect(alphabet).to start_with("a").and end_with("z")
|
196
|
+
expect(stoplight.color).to eq("red").or eq("green").or eq("yellow")
|
197
|
+
```
|
198
|
+
|
199
|
+
## Composing Matchers
|
200
|
+
|
201
|
+
Many of the built-in matchers are designed to take matchers as
|
202
|
+
arguments, to allow you to flexibly specify only the essential
|
203
|
+
aspects of an object or data structure. In addition, all of the
|
204
|
+
built-in matchers have one or more aliases that provide better
|
205
|
+
phrasing for when they are used as arguments to another matcher.
|
206
|
+
|
207
|
+
### Examples
|
208
|
+
|
209
|
+
```ruby
|
210
|
+
expect { k += 1.05 }.to change { k }.by( a_value_within(0.1).of(1.0) )
|
211
|
+
|
212
|
+
expect { s = "barn" }.to change { s }
|
213
|
+
.from( a_string_matching(/foo/) )
|
214
|
+
.to( a_string_matching(/bar/) )
|
215
|
+
|
216
|
+
expect(["barn", 2.45]).to contain_exactly(
|
217
|
+
a_value_within(0.1).of(2.5),
|
218
|
+
a_string_starting_with("bar")
|
219
|
+
)
|
220
|
+
|
221
|
+
expect(["barn", "food", 2.45]).to end_with(
|
222
|
+
a_string_matching("foo"),
|
223
|
+
a_value > 2
|
224
|
+
)
|
225
|
+
|
226
|
+
expect(["barn", 2.45]).to include( a_string_starting_with("bar") )
|
227
|
+
|
228
|
+
expect(:a => "food", :b => "good").to include(:a => a_string_matching(/foo/))
|
229
|
+
|
230
|
+
hash = {
|
231
|
+
:a => {
|
232
|
+
:b => ["foo", 5],
|
233
|
+
:c => { :d => 2.05 }
|
234
|
+
}
|
235
|
+
}
|
236
|
+
|
237
|
+
expect(hash).to match(
|
238
|
+
:a => {
|
239
|
+
:b => a_collection_containing_exactly(
|
240
|
+
a_string_starting_with("f"),
|
241
|
+
an_instance_of(Fixnum)
|
242
|
+
),
|
243
|
+
:c => { :d => (a_value < 3) }
|
244
|
+
}
|
245
|
+
)
|
246
|
+
|
247
|
+
expect { |probe|
|
248
|
+
[1, 2, 3].each(&probe)
|
249
|
+
}.to yield_successive_args( a_value < 2, 2, a_value > 2 )
|
250
|
+
```
|
251
|
+
|
185
252
|
## Also see
|
186
253
|
|
187
254
|
* [http://github.com/rspec/rspec](http://github.com/rspec/rspec)
|
data/features/README.md
CHANGED
@@ -26,12 +26,12 @@ Note: You can also use `expect(..).to_not` instead of `expect(..).not_to`.
|
|
26
26
|
A Matcher is any object that responds to the following methods:
|
27
27
|
|
28
28
|
matches?(actual)
|
29
|
-
|
29
|
+
failure_message
|
30
30
|
|
31
31
|
These methods are also part of the matcher protocol, but are optional:
|
32
32
|
|
33
33
|
does_not_match?(actual)
|
34
|
-
|
34
|
+
failure_message_when_negated
|
35
35
|
description
|
36
36
|
|
37
37
|
RSpec ships with a number of built-in matchers and a DSL for writing custom
|
@@ -46,10 +46,11 @@ e.g.
|
|
46
46
|
|
47
47
|
## Truthiness and existentialism
|
48
48
|
|
49
|
-
expect(actual).to
|
50
|
-
expect(actual).to
|
51
|
-
expect(actual).to
|
52
|
-
expect(actual).to be
|
49
|
+
expect(actual).to be_truthy # passes if actual is truthy (not nil or false)
|
50
|
+
expect(actual).to be true # passes if actual == true
|
51
|
+
expect(actual).to be_falsey # passes if actual is falsy (nil or false)
|
52
|
+
expect(actual).to be false # passes if actual == false
|
53
|
+
expect(actual).to be_nil # passes if actual is nil
|
53
54
|
|
54
55
|
## Expecting errors
|
55
56
|
|
@@ -77,16 +78,18 @@ e.g.
|
|
77
78
|
## Collection membership
|
78
79
|
|
79
80
|
expect(actual).to include(expected)
|
80
|
-
expect(array).to match_array(
|
81
|
+
expect(array).to match_array(expected_array)
|
82
|
+
# ...which is the same as:
|
83
|
+
expect(array).to contain_exactly(individual, elements)
|
81
84
|
|
82
85
|
### Examples
|
83
86
|
|
84
|
-
expect([1,2,3]).to
|
85
|
-
expect([1,2,3]).to
|
87
|
+
expect([1, 2, 3]).to include(1)
|
88
|
+
expect([1, 2, 3]).to include(1, 2)
|
86
89
|
expect(:a => 'b').to include(:a => 'b')
|
87
90
|
expect("this string").to include("is str")
|
88
|
-
expect([1,2,3]).to
|
89
|
-
expect([1,2,3]).to
|
91
|
+
expect([1, 2, 3]).to contain_exactly(2, 1, 3)
|
92
|
+
expect([1, 2, 3]).to match_array([3, 2, 1])
|
90
93
|
|
91
94
|
## Ranges (1.9 only)
|
92
95
|
|
@@ -1,7 +1,7 @@
|
|
1
|
-
Feature:
|
1
|
+
Feature: comparison matchers
|
2
2
|
|
3
3
|
RSpec provides a number of matchers that are based on Ruby's built-in
|
4
|
-
operators. These can be used for generalized
|
4
|
+
operators. These can be used for generalized comparison of values. E.g.
|
5
5
|
|
6
6
|
```ruby
|
7
7
|
expect(9).to be > 6
|
@@ -0,0 +1,46 @@
|
|
1
|
+
Feature: contain_exactly matcher
|
2
|
+
|
3
|
+
The `contain_exactly` matcher provides a way to test arrays against each other
|
4
|
+
in a way that disregards differences in the ordering between the actual
|
5
|
+
and expected array. For example:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
expect([1, 2, 3]).to contain_exactly(2, 3, 1) # pass
|
9
|
+
expect([:a, :c, :b]).to contain_exactly(:a, :c ) # fail
|
10
|
+
```
|
11
|
+
|
12
|
+
This matcher is also available as `match_array`, which expects the
|
13
|
+
expected array to be given as a single array argument rather than
|
14
|
+
as individual splatted elements. The above could also be written as:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
expect([1, 2, 3]).to match_array [2, 3, 1] # pass
|
18
|
+
expect([:a, :c, :b]).to match_array [:a, :c] # fail
|
19
|
+
```
|
20
|
+
|
21
|
+
Scenario: array operator matchers
|
22
|
+
Given a file named "contain_exactly_matcher_spec.rb" with:
|
23
|
+
"""ruby
|
24
|
+
describe do
|
25
|
+
example { expect([1, 2, 3]).to contain_exactly(1, 2, 3) }
|
26
|
+
example { expect([1, 2, 3]).to contain_exactly(1, 3, 2) }
|
27
|
+
example { expect([1, 2, 3]).to contain_exactly(2, 1, 3) }
|
28
|
+
example { expect([1, 2, 3]).to contain_exactly(2, 3, 1) }
|
29
|
+
example { expect([1, 2, 3]).to contain_exactly(3, 1, 2) }
|
30
|
+
example { expect([1, 2, 3]).to contain_exactly(3, 2, 1) }
|
31
|
+
|
32
|
+
# deliberate failures
|
33
|
+
example { expect([1, 2, 3]).to contain_exactly(1, 2, 1) }
|
34
|
+
end
|
35
|
+
"""
|
36
|
+
When I run `rspec contain_exactly_matcher_spec.rb`
|
37
|
+
Then the output should contain "7 examples, 1 failure"
|
38
|
+
And the output should contain:
|
39
|
+
"""
|
40
|
+
Failure/Error: example { expect([1, 2, 3]).to contain_exactly(1, 2, 1) }
|
41
|
+
expected collection contained: [1, 1, 2]
|
42
|
+
actual collection contained: [1, 2, 3]
|
43
|
+
the missing elements were: [1]
|
44
|
+
the extra elements were: [3]
|
45
|
+
"""
|
46
|
+
|
@@ -37,7 +37,7 @@ Feature: expect change
|
|
37
37
|
"""
|
38
38
|
When I run `rspec spec/example_spec.rb`
|
39
39
|
Then the output should contain "1 failure"
|
40
|
-
Then the output should contain "
|
40
|
+
Then the output should contain "expected result to have changed by 2, but was changed by 1"
|
41
41
|
|
42
42
|
Scenario: expect no change
|
43
43
|
Given a file named "spec/example_spec.rb" with:
|
@@ -56,4 +56,4 @@ Feature: expect change
|
|
56
56
|
"""
|
57
57
|
When I run `rspec spec/example_spec.rb`
|
58
58
|
Then the output should contain "2 failures"
|
59
|
-
Then the output should contain "
|
59
|
+
Then the output should contain "expected result not to have changed, but did change from 1 to 2"
|
@@ -124,51 +124,3 @@ Feature: include matcher
|
|
124
124
|
When I run `rspec hash_include_matcher_spec.rb`
|
125
125
|
Then the output should contain "13 failure"
|
126
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' |
|