rspec-expectations 2.14.0 → 3.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data/.document +1 -1
- data/.yardopts +1 -1
- data/Changelog.md +976 -25
- data/{License.txt → LICENSE.md} +5 -3
- data/README.md +162 -26
- data/lib/rspec/expectations/block_snippet_extractor.rb +253 -0
- data/lib/rspec/expectations/configuration.rb +230 -0
- data/lib/rspec/expectations/expectation_target.rb +127 -51
- data/lib/rspec/expectations/fail_with.rb +17 -57
- data/lib/rspec/expectations/failure_aggregator.rb +229 -0
- data/lib/rspec/expectations/handler.rb +146 -32
- data/lib/rspec/expectations/minitest_integration.rb +58 -0
- data/lib/rspec/expectations/syntax.rb +68 -100
- data/lib/rspec/expectations/version.rb +1 -1
- data/lib/rspec/expectations.rb +58 -23
- data/lib/rspec/matchers/aliased_matcher.rb +116 -0
- data/lib/rspec/matchers/built_in/all.rb +86 -0
- data/lib/rspec/matchers/built_in/base_matcher.rb +191 -20
- data/lib/rspec/matchers/built_in/be.rb +114 -114
- data/lib/rspec/matchers/built_in/be_between.rb +77 -0
- data/lib/rspec/matchers/built_in/be_instance_of.rb +15 -4
- data/lib/rspec/matchers/built_in/be_kind_of.rb +10 -1
- data/lib/rspec/matchers/built_in/be_within.rb +35 -18
- data/lib/rspec/matchers/built_in/change.rb +389 -80
- data/lib/rspec/matchers/built_in/compound.rb +290 -0
- data/lib/rspec/matchers/built_in/contain_exactly.rb +310 -0
- data/lib/rspec/matchers/built_in/count_expectation.rb +169 -0
- data/lib/rspec/matchers/built_in/cover.rb +3 -0
- data/lib/rspec/matchers/built_in/eq.rb +30 -8
- data/lib/rspec/matchers/built_in/eql.rb +23 -8
- data/lib/rspec/matchers/built_in/equal.rb +55 -22
- data/lib/rspec/matchers/built_in/exist.rb +74 -10
- data/lib/rspec/matchers/built_in/has.rb +141 -22
- data/lib/rspec/matchers/built_in/have_attributes.rb +114 -0
- data/lib/rspec/matchers/built_in/include.rb +184 -32
- data/lib/rspec/matchers/built_in/match.rb +95 -1
- data/lib/rspec/matchers/built_in/operators.rb +128 -0
- data/lib/rspec/matchers/built_in/output.rb +207 -0
- data/lib/rspec/matchers/built_in/raise_error.rb +192 -44
- data/lib/rspec/matchers/built_in/respond_to.rb +154 -28
- data/lib/rspec/matchers/built_in/satisfy.rb +39 -9
- data/lib/rspec/matchers/built_in/start_or_end_with.rb +94 -0
- data/lib/rspec/matchers/built_in/throw_symbol.rb +58 -14
- data/lib/rspec/matchers/built_in/yield.rb +240 -161
- data/lib/rspec/matchers/built_in.rb +47 -33
- data/lib/rspec/matchers/composable.rb +171 -0
- data/lib/rspec/matchers/dsl.rb +531 -10
- data/lib/rspec/matchers/english_phrasing.rb +58 -0
- data/lib/rspec/matchers/fail_matchers.rb +42 -0
- data/lib/rspec/matchers/generated_descriptions.rb +14 -8
- data/lib/rspec/matchers/matcher_delegator.rb +61 -0
- data/lib/rspec/matchers/matcher_protocol.rb +105 -0
- data/lib/rspec/matchers/multi_matcher_diff.rb +82 -0
- data/lib/rspec/matchers.rb +520 -173
- data.tar.gz.sig +0 -0
- metadata +141 -242
- metadata.gz.sig +2 -0
- data/features/README.md +0 -48
- data/features/Upgrade.md +0 -53
- data/features/built_in_matchers/README.md +0 -90
- data/features/built_in_matchers/be.feature +0 -175
- data/features/built_in_matchers/be_within.feature +0 -48
- data/features/built_in_matchers/cover.feature +0 -47
- data/features/built_in_matchers/end_with.feature +0 -48
- data/features/built_in_matchers/equality.feature +0 -139
- data/features/built_in_matchers/exist.feature +0 -45
- data/features/built_in_matchers/expect_change.feature +0 -59
- data/features/built_in_matchers/expect_error.feature +0 -144
- data/features/built_in_matchers/have.feature +0 -109
- data/features/built_in_matchers/include.feature +0 -174
- data/features/built_in_matchers/match.feature +0 -52
- data/features/built_in_matchers/operators.feature +0 -227
- data/features/built_in_matchers/predicates.feature +0 -137
- data/features/built_in_matchers/respond_to.feature +0 -84
- data/features/built_in_matchers/satisfy.feature +0 -33
- data/features/built_in_matchers/start_with.feature +0 -48
- data/features/built_in_matchers/throw_symbol.feature +0 -91
- data/features/built_in_matchers/types.feature +0 -116
- data/features/built_in_matchers/yield.feature +0 -161
- data/features/custom_matchers/access_running_example.feature +0 -53
- data/features/custom_matchers/define_diffable_matcher.feature +0 -27
- data/features/custom_matchers/define_matcher.feature +0 -368
- data/features/custom_matchers/define_matcher_outside_rspec.feature +0 -38
- data/features/custom_matchers/define_matcher_with_fluent_interface.feature +0 -24
- data/features/customized_message.feature +0 -22
- data/features/diffing.feature +0 -85
- data/features/implicit_docstrings.feature +0 -52
- data/features/step_definitions/additional_cli_steps.rb +0 -22
- data/features/support/env.rb +0 -14
- data/features/syntax_configuration.feature +0 -71
- data/features/test_frameworks/test_unit.feature +0 -44
- data/lib/rspec/expectations/deprecation.rb +0 -17
- data/lib/rspec/expectations/differ.rb +0 -133
- data/lib/rspec/expectations/errors.rb +0 -9
- data/lib/rspec/expectations/extensions/array.rb +0 -9
- data/lib/rspec/expectations/extensions/object.rb +0 -29
- data/lib/rspec/expectations/extensions.rb +0 -2
- data/lib/rspec/matchers/be_close.rb +0 -9
- data/lib/rspec/matchers/built_in/have.rb +0 -124
- data/lib/rspec/matchers/built_in/match_array.rb +0 -51
- data/lib/rspec/matchers/built_in/start_and_end_with.rb +0 -48
- data/lib/rspec/matchers/compatibility.rb +0 -14
- data/lib/rspec/matchers/configuration.rb +0 -108
- data/lib/rspec/matchers/extensions/instance_eval_with_args.rb +0 -39
- data/lib/rspec/matchers/matcher.rb +0 -300
- data/lib/rspec/matchers/method_missing.rb +0 -12
- data/lib/rspec/matchers/operator_matcher.rb +0 -109
- data/lib/rspec/matchers/pretty.rb +0 -70
- data/lib/rspec/matchers/test_unit_integration.rb +0 -11
- data/lib/rspec-expectations.rb +0 -1
- data/spec/rspec/expectations/differ_spec.rb +0 -192
- data/spec/rspec/expectations/expectation_target_spec.rb +0 -82
- data/spec/rspec/expectations/extensions/kernel_spec.rb +0 -67
- data/spec/rspec/expectations/fail_with_spec.rb +0 -114
- data/spec/rspec/expectations/handler_spec.rb +0 -227
- data/spec/rspec/expectations/syntax_spec.rb +0 -139
- data/spec/rspec/matchers/base_matcher_spec.rb +0 -62
- data/spec/rspec/matchers/be_close_spec.rb +0 -22
- data/spec/rspec/matchers/be_instance_of_spec.rb +0 -63
- data/spec/rspec/matchers/be_kind_of_spec.rb +0 -41
- data/spec/rspec/matchers/be_spec.rb +0 -516
- data/spec/rspec/matchers/be_within_spec.rb +0 -137
- data/spec/rspec/matchers/change_spec.rb +0 -553
- data/spec/rspec/matchers/configuration_spec.rb +0 -206
- data/spec/rspec/matchers/cover_spec.rb +0 -69
- data/spec/rspec/matchers/description_generation_spec.rb +0 -190
- data/spec/rspec/matchers/dsl_spec.rb +0 -57
- data/spec/rspec/matchers/eq_spec.rb +0 -60
- data/spec/rspec/matchers/eql_spec.rb +0 -41
- data/spec/rspec/matchers/equal_spec.rb +0 -78
- data/spec/rspec/matchers/exist_spec.rb +0 -124
- data/spec/rspec/matchers/has_spec.rb +0 -122
- data/spec/rspec/matchers/have_spec.rb +0 -455
- data/spec/rspec/matchers/include_matcher_integration_spec.rb +0 -30
- data/spec/rspec/matchers/include_spec.rb +0 -531
- data/spec/rspec/matchers/match_array_spec.rb +0 -194
- data/spec/rspec/matchers/match_spec.rb +0 -61
- data/spec/rspec/matchers/matcher_spec.rb +0 -471
- data/spec/rspec/matchers/matchers_spec.rb +0 -37
- data/spec/rspec/matchers/method_missing_spec.rb +0 -28
- data/spec/rspec/matchers/operator_matcher_spec.rb +0 -223
- data/spec/rspec/matchers/raise_error_spec.rb +0 -485
- data/spec/rspec/matchers/respond_to_spec.rb +0 -292
- data/spec/rspec/matchers/satisfy_spec.rb +0 -44
- data/spec/rspec/matchers/start_with_end_with_spec.rb +0 -186
- data/spec/rspec/matchers/throw_symbol_spec.rb +0 -116
- data/spec/rspec/matchers/yield_spec.rb +0 -514
- data/spec/spec_helper.rb +0 -54
- data/spec/support/classes.rb +0 -56
- data/spec/support/in_sub_process.rb +0 -38
- data/spec/support/matchers.rb +0 -22
- data/spec/support/ruby_version.rb +0 -10
- data/spec/support/shared_examples.rb +0 -13
@@ -1,175 +0,0 @@
|
|
1
|
-
Feature: "be" matchers
|
2
|
-
|
3
|
-
There are several related "be" matchers:
|
4
|
-
|
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
|
-
```
|
11
|
-
|
12
|
-
Scenario: be_true matcher
|
13
|
-
Given a file named "be_true_spec.rb" with:
|
14
|
-
"""ruby
|
15
|
-
describe "be_true matcher" do
|
16
|
-
specify { true.should be_true }
|
17
|
-
specify { 7.should be_true }
|
18
|
-
specify { "foo".should be_true }
|
19
|
-
specify { nil.should_not be_true }
|
20
|
-
specify { false.should_not be_true }
|
21
|
-
|
22
|
-
# deliberate failures
|
23
|
-
specify { true.should_not be_true }
|
24
|
-
specify { 7.should_not be_true }
|
25
|
-
specify { "foo".should_not be_true }
|
26
|
-
specify { nil.should be_true }
|
27
|
-
specify { false.should be_true }
|
28
|
-
end
|
29
|
-
"""
|
30
|
-
When I run `rspec be_true_spec.rb`
|
31
|
-
Then the output should contain "10 examples, 5 failures"
|
32
|
-
And the output should contain:
|
33
|
-
"""
|
34
|
-
expected: non-true value
|
35
|
-
got: true
|
36
|
-
"""
|
37
|
-
And the output should contain:
|
38
|
-
"""
|
39
|
-
expected: non-true value
|
40
|
-
got: 7
|
41
|
-
"""
|
42
|
-
And the output should contain:
|
43
|
-
"""
|
44
|
-
expected: non-true value
|
45
|
-
got: "foo"
|
46
|
-
"""
|
47
|
-
And the output should contain:
|
48
|
-
"""
|
49
|
-
expected: true value
|
50
|
-
got: nil
|
51
|
-
"""
|
52
|
-
And the output should contain:
|
53
|
-
"""
|
54
|
-
expected: true value
|
55
|
-
got: false
|
56
|
-
"""
|
57
|
-
|
58
|
-
Scenario: be_false matcher
|
59
|
-
Given a file named "be_false_spec.rb" with:
|
60
|
-
"""ruby
|
61
|
-
describe "be_false matcher" do
|
62
|
-
specify { nil.should be_false }
|
63
|
-
specify { false.should be_false }
|
64
|
-
specify { true.should_not be_false }
|
65
|
-
specify { 7.should_not be_false }
|
66
|
-
specify { "foo".should_not be_false }
|
67
|
-
|
68
|
-
# deliberate failures
|
69
|
-
specify { nil.should_not be_false }
|
70
|
-
specify { false.should_not be_false }
|
71
|
-
specify { true.should be_false }
|
72
|
-
specify { 7.should be_false }
|
73
|
-
specify { "foo".should be_false }
|
74
|
-
end
|
75
|
-
"""
|
76
|
-
When I run `rspec be_false_spec.rb`
|
77
|
-
Then the output should contain "10 examples, 5 failures"
|
78
|
-
And the output should contain:
|
79
|
-
"""
|
80
|
-
expected: non-false value
|
81
|
-
got: nil
|
82
|
-
"""
|
83
|
-
And the output should contain:
|
84
|
-
"""
|
85
|
-
expected: non-false value
|
86
|
-
got: false
|
87
|
-
"""
|
88
|
-
And the output should contain:
|
89
|
-
"""
|
90
|
-
expected: false value
|
91
|
-
got: true
|
92
|
-
"""
|
93
|
-
And the output should contain:
|
94
|
-
"""
|
95
|
-
expected: false value
|
96
|
-
got: 7
|
97
|
-
"""
|
98
|
-
And the output should contain:
|
99
|
-
"""
|
100
|
-
expected: false value
|
101
|
-
got: "foo"
|
102
|
-
"""
|
103
|
-
|
104
|
-
Scenario: be_nil matcher
|
105
|
-
Given a file named "be_nil_spec.rb" with:
|
106
|
-
"""ruby
|
107
|
-
describe "be_nil matcher" do
|
108
|
-
specify { nil.should be_nil }
|
109
|
-
specify { false.should_not be_nil }
|
110
|
-
specify { true.should_not be_nil }
|
111
|
-
specify { 7.should_not be_nil }
|
112
|
-
specify { "foo".should_not be_nil }
|
113
|
-
|
114
|
-
# deliberate failures
|
115
|
-
specify { nil.should_not be_nil }
|
116
|
-
specify { false.should be_nil }
|
117
|
-
specify { true.should be_nil }
|
118
|
-
specify { 7.should be_nil }
|
119
|
-
specify { "foo".should be_nil }
|
120
|
-
end
|
121
|
-
"""
|
122
|
-
When I run `rspec be_nil_spec.rb`
|
123
|
-
Then the output should contain "10 examples, 5 failures"
|
124
|
-
And the output should contain:
|
125
|
-
"""
|
126
|
-
expected: not nil
|
127
|
-
got: nil
|
128
|
-
"""
|
129
|
-
And the output should contain:
|
130
|
-
"""
|
131
|
-
expected: nil
|
132
|
-
got: false
|
133
|
-
"""
|
134
|
-
And the output should contain:
|
135
|
-
"""
|
136
|
-
expected: nil
|
137
|
-
got: true
|
138
|
-
"""
|
139
|
-
And the output should contain:
|
140
|
-
"""
|
141
|
-
expected: nil
|
142
|
-
got: 7
|
143
|
-
"""
|
144
|
-
And the output should contain:
|
145
|
-
"""
|
146
|
-
expected: nil
|
147
|
-
got: "foo"
|
148
|
-
"""
|
149
|
-
|
150
|
-
Scenario: be matcher
|
151
|
-
Given a file named "be_spec.rb" with:
|
152
|
-
"""ruby
|
153
|
-
describe "be_matcher" do
|
154
|
-
specify { true.should be }
|
155
|
-
specify { 7.should be }
|
156
|
-
specify { "foo".should be }
|
157
|
-
specify { nil.should_not be }
|
158
|
-
specify { false.should_not be }
|
159
|
-
|
160
|
-
# deliberate failures
|
161
|
-
specify { true.should_not be }
|
162
|
-
specify { 7.should_not be }
|
163
|
-
specify { "foo".should_not be }
|
164
|
-
specify { nil.should be }
|
165
|
-
specify { false.should be }
|
166
|
-
end
|
167
|
-
"""
|
168
|
-
When I run `rspec be_spec.rb`
|
169
|
-
Then the output should contain all of these:
|
170
|
-
| 10 examples, 5 failures |
|
171
|
-
| expected true to evaluate to false |
|
172
|
-
| expected 7 to evaluate to false |
|
173
|
-
| expected "foo" to evaluate to false |
|
174
|
-
| expected nil to evaluate to true |
|
175
|
-
| expected false to evaluate to true |
|
@@ -1,48 +0,0 @@
|
|
1
|
-
Feature: be_within matcher
|
2
|
-
|
3
|
-
Normal equality expectations do not work well for floating point values.
|
4
|
-
Consider this irb session:
|
5
|
-
|
6
|
-
> radius = 3
|
7
|
-
=> 3
|
8
|
-
> area_of_circle = radius * radius * Math::PI
|
9
|
-
=> 28.2743338823081
|
10
|
-
> area_of_circle == 28.2743338823081
|
11
|
-
=> false
|
12
|
-
|
13
|
-
Instead, you should use the be_within matcher to check that the value
|
14
|
-
is within a delta of your expected value:
|
15
|
-
|
16
|
-
```ruby
|
17
|
-
area_of_circle.should be_within(0.1).of(28.3)
|
18
|
-
```
|
19
|
-
|
20
|
-
Note that the difference between the actual and expected values must be
|
21
|
-
smaller than your delta; if it is equal, the matcher will fail.
|
22
|
-
|
23
|
-
Scenario: basic usage
|
24
|
-
Given a file named "be_within_matcher_spec.rb" with:
|
25
|
-
"""ruby
|
26
|
-
describe 27.5 do
|
27
|
-
it { should be_within(0.5).of(27.9) }
|
28
|
-
it { should be_within(0.5).of(28.0) }
|
29
|
-
it { should be_within(0.5).of(27.1) }
|
30
|
-
it { should be_within(0.5).of(27.0) }
|
31
|
-
|
32
|
-
it { should_not be_within(0.5).of(28.1) }
|
33
|
-
it { should_not be_within(0.5).of(26.9) }
|
34
|
-
|
35
|
-
# deliberate failures
|
36
|
-
it { should_not be_within(0.5).of(28) }
|
37
|
-
it { should_not be_within(0.5).of(27) }
|
38
|
-
it { should be_within(0.5).of(28.1) }
|
39
|
-
it { should be_within(0.5).of(26.9) }
|
40
|
-
end
|
41
|
-
"""
|
42
|
-
When I run `rspec be_within_matcher_spec.rb`
|
43
|
-
Then the output should contain all of these:
|
44
|
-
| 10 examples, 4 failures |
|
45
|
-
| expected 27.5 not to be within 0.5 of 28 |
|
46
|
-
| expected 27.5 not to be within 0.5 of 27 |
|
47
|
-
| expected 27.5 to be within 0.5 of 28.1 |
|
48
|
-
| expected 27.5 to be within 0.5 of 26.9 |
|
@@ -1,47 +0,0 @@
|
|
1
|
-
@ruby-1.9
|
2
|
-
Feature: cover matcher
|
3
|
-
|
4
|
-
Use the cover matcher to specify that a range covers one or more
|
5
|
-
expected objects. This works on any object that responds to #cover? (such
|
6
|
-
as a Range):
|
7
|
-
|
8
|
-
```ruby
|
9
|
-
(1..10).should cover(5)
|
10
|
-
(1..10).should cover(4, 6)
|
11
|
-
(1..10).should_not cover(11)
|
12
|
-
```
|
13
|
-
|
14
|
-
Scenario: range usage
|
15
|
-
Given a file named "range_cover_matcher_spec.rb" with:
|
16
|
-
"""ruby
|
17
|
-
describe (1..10) do
|
18
|
-
it { should cover(4) }
|
19
|
-
it { should cover(6) }
|
20
|
-
it { should cover(8) }
|
21
|
-
it { should cover(4, 6) }
|
22
|
-
it { should cover(4, 6, 8) }
|
23
|
-
it { should_not cover(11) }
|
24
|
-
it { should_not cover(11, 12) }
|
25
|
-
|
26
|
-
# deliberate failures
|
27
|
-
it { should cover(11) }
|
28
|
-
it { should_not cover(4) }
|
29
|
-
it { should_not cover(6) }
|
30
|
-
it { should_not cover(8) }
|
31
|
-
it { should_not cover(4, 6, 8) }
|
32
|
-
|
33
|
-
# both of these should fail since it covers 5 but not 11
|
34
|
-
it { should cover(5, 11) }
|
35
|
-
it { should_not cover(5, 11) }
|
36
|
-
end
|
37
|
-
"""
|
38
|
-
When I run `rspec range_cover_matcher_spec.rb`
|
39
|
-
Then the output should contain all of these:
|
40
|
-
| 14 examples, 7 failures |
|
41
|
-
| expected 1..10 to cover 11 |
|
42
|
-
| expected 1..10 not to cover 4 |
|
43
|
-
| expected 1..10 not to cover 6 |
|
44
|
-
| expected 1..10 not to cover 8 |
|
45
|
-
| expected 1..10 not to cover 4, 6, and 8 |
|
46
|
-
| expected 1..10 to cover 5 and 11 |
|
47
|
-
| expected 1..10 not to cover 5 and 11 |
|
@@ -1,48 +0,0 @@
|
|
1
|
-
Feature: end_with matcher
|
2
|
-
|
3
|
-
Use the `end_with` matcher to specify that a string or array ends with the
|
4
|
-
expected characters or elements.
|
5
|
-
|
6
|
-
```ruby
|
7
|
-
"this string".should end_with "string"
|
8
|
-
"this string".should_not end_with "stringy"
|
9
|
-
[0, 1, 2].should end_with 1, 2
|
10
|
-
```
|
11
|
-
|
12
|
-
Scenario: string usage
|
13
|
-
Given a file named "example_spec.rb" with:
|
14
|
-
"""ruby
|
15
|
-
describe "this string" do
|
16
|
-
it { should end_with "string" }
|
17
|
-
it { should_not end_with "stringy" }
|
18
|
-
|
19
|
-
# deliberate failures
|
20
|
-
it { should_not end_with "string" }
|
21
|
-
it { should end_with "stringy" }
|
22
|
-
end
|
23
|
-
"""
|
24
|
-
When I run `rspec example_spec.rb`
|
25
|
-
Then the output should contain all of these:
|
26
|
-
| 4 examples, 2 failures |
|
27
|
-
| expected "this string" not to end with "string" |
|
28
|
-
| expected "this string" to end with "stringy" |
|
29
|
-
|
30
|
-
Scenario: array usage
|
31
|
-
Given a file named "example_spec.rb" with:
|
32
|
-
"""ruby
|
33
|
-
describe [0, 1, 2, 3, 4] do
|
34
|
-
it { should end_with 4 }
|
35
|
-
it { should end_with 3, 4 }
|
36
|
-
it { should_not end_with 3 }
|
37
|
-
it { should_not end_with 0, 1, 2, 3, 4, 5 }
|
38
|
-
|
39
|
-
# deliberate failures
|
40
|
-
it { should_not end_with 4 }
|
41
|
-
it { should end_with 3 }
|
42
|
-
end
|
43
|
-
"""
|
44
|
-
When I run `rspec example_spec.rb`
|
45
|
-
Then the output should contain all of these:
|
46
|
-
| 6 examples, 2 failures |
|
47
|
-
| expected [0, 1, 2, 3, 4] not to end with 4 |
|
48
|
-
| expected [0, 1, 2, 3, 4] to end with 3 |
|
@@ -1,139 +0,0 @@
|
|
1
|
-
Feature: equality matchers
|
2
|
-
|
3
|
-
Ruby exposes several different methods for handling equality:
|
4
|
-
|
5
|
-
a.equal?(b) # object identity - a and b refer to the same object
|
6
|
-
a.eql?(b) # object equivalence - a and b have the same value
|
7
|
-
a == b # object equivalence - a and b have the same value with type conversions
|
8
|
-
|
9
|
-
Note that these descriptions are guidelines but are not forced by the
|
10
|
-
language. Any object can implement any of these methods with its own
|
11
|
-
semantics.
|
12
|
-
|
13
|
-
rspec-expectations ships with matchers that align with each of these methods:
|
14
|
-
|
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
|
-
```
|
20
|
-
|
21
|
-
It also ships with two matchers that have more of a DSL feel to them:
|
22
|
-
|
23
|
-
```ruby
|
24
|
-
a.should be(b) # passes if a.equal?(b)
|
25
|
-
a.should eq(b) # passes if a == b
|
26
|
-
```
|
27
|
-
|
28
|
-
These are a useful pair if you wish to avoid the warning that Ruby emits on
|
29
|
-
`a.should == b`
|
30
|
-
|
31
|
-
Scenario: compare using eq (==)
|
32
|
-
Given a file named "compare_using_eq.rb" with:
|
33
|
-
"""ruby
|
34
|
-
describe "a string" do
|
35
|
-
it "is equal to another string of the same value" do
|
36
|
-
"this string".should eq("this string")
|
37
|
-
end
|
38
|
-
|
39
|
-
it "is not equal to another string of a different value" do
|
40
|
-
"this string".should_not eq("a different string")
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
describe "an integer" do
|
45
|
-
it "is equal to a float of the same value" do
|
46
|
-
5.should eq(5.0)
|
47
|
-
end
|
48
|
-
end
|
49
|
-
"""
|
50
|
-
When I run `rspec compare_using_eq.rb`
|
51
|
-
Then the output should contain "3 examples, 0 failures"
|
52
|
-
|
53
|
-
Scenario: compare using ==
|
54
|
-
Given a file named "compare_using_==.rb" with:
|
55
|
-
"""ruby
|
56
|
-
describe "a string" do
|
57
|
-
it "is equal to another string of the same value" do
|
58
|
-
"this string".should == "this string"
|
59
|
-
end
|
60
|
-
|
61
|
-
it "is not equal to another string of a different value" do
|
62
|
-
"this string".should_not == "a different string"
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
describe "an integer" do
|
67
|
-
it "is equal to a float of the same value" do
|
68
|
-
5.should == 5.0
|
69
|
-
end
|
70
|
-
end
|
71
|
-
"""
|
72
|
-
When I run `rspec compare_using_==.rb`
|
73
|
-
Then the output should contain "3 examples, 0 failures"
|
74
|
-
|
75
|
-
Scenario: compare using eql (eql?)
|
76
|
-
Given a file named "compare_using_eql.rb" with:
|
77
|
-
"""ruby
|
78
|
-
describe "an integer" do
|
79
|
-
it "is equal to another integer of the same value" do
|
80
|
-
5.should eql(5)
|
81
|
-
end
|
82
|
-
|
83
|
-
it "is not equal to another integer of a different value" do
|
84
|
-
5.should_not eql(6)
|
85
|
-
end
|
86
|
-
|
87
|
-
it "is not equal to a float of the same value" do
|
88
|
-
5.should_not eql(5.0)
|
89
|
-
end
|
90
|
-
|
91
|
-
end
|
92
|
-
"""
|
93
|
-
When I run `rspec compare_using_eql.rb`
|
94
|
-
Then the output should contain "3 examples, 0 failures"
|
95
|
-
|
96
|
-
Scenario: compare using equal (equal?)
|
97
|
-
Given a file named "compare_using_equal.rb" with:
|
98
|
-
"""ruby
|
99
|
-
describe "a string" do
|
100
|
-
it "is equal to itself" do
|
101
|
-
string = "this string"
|
102
|
-
string.should equal(string)
|
103
|
-
end
|
104
|
-
|
105
|
-
it "is not equal to another string of the same value" do
|
106
|
-
"this string".should_not equal("this string")
|
107
|
-
end
|
108
|
-
|
109
|
-
it "is not equal to another string of a different value" do
|
110
|
-
"this string".should_not equal("a different string")
|
111
|
-
end
|
112
|
-
|
113
|
-
end
|
114
|
-
"""
|
115
|
-
When I run `rspec compare_using_equal.rb`
|
116
|
-
Then the output should contain "3 examples, 0 failures"
|
117
|
-
|
118
|
-
Scenario: compare using be (equal?)
|
119
|
-
Given a file named "compare_using_be.rb" with:
|
120
|
-
"""ruby
|
121
|
-
describe "a string" do
|
122
|
-
it "is equal to itself" do
|
123
|
-
string = "this string"
|
124
|
-
string.should be(string)
|
125
|
-
end
|
126
|
-
|
127
|
-
it "is not equal to another string of the same value" do
|
128
|
-
"this string".should_not be("this string")
|
129
|
-
end
|
130
|
-
|
131
|
-
it "is not equal to another string of a different value" do
|
132
|
-
"this string".should_not be("a different string")
|
133
|
-
end
|
134
|
-
|
135
|
-
end
|
136
|
-
"""
|
137
|
-
When I run `rspec compare_using_be.rb`
|
138
|
-
Then the output should contain "3 examples, 0 failures"
|
139
|
-
|
@@ -1,45 +0,0 @@
|
|
1
|
-
Feature: exist matcher
|
2
|
-
|
3
|
-
The exist matcher is used to specify that something exists
|
4
|
-
(as indicated by #exist? or #exists?):
|
5
|
-
|
6
|
-
```ruby
|
7
|
-
obj.should exist # passes if obj.exist? or obj.exists?
|
8
|
-
```
|
9
|
-
|
10
|
-
Scenario: basic usage
|
11
|
-
Given a file named "exist_matcher_spec.rb" with:
|
12
|
-
"""ruby
|
13
|
-
class Planet
|
14
|
-
attr_reader :name
|
15
|
-
|
16
|
-
def initialize(name)
|
17
|
-
@name = name
|
18
|
-
end
|
19
|
-
|
20
|
-
def inspect
|
21
|
-
"<Planet: #{name}>"
|
22
|
-
end
|
23
|
-
|
24
|
-
def exist? # also works with exists?
|
25
|
-
%w[Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune].include?(name)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
describe "Earth" do
|
30
|
-
let(:earth) { Planet.new("Earth") }
|
31
|
-
specify { earth.should exist }
|
32
|
-
specify { earth.should_not exist } # deliberate failure
|
33
|
-
end
|
34
|
-
|
35
|
-
describe "Tatooine" do
|
36
|
-
let(:tatooine) { Planet.new("Tatooine") }
|
37
|
-
it { tatooine.should exist } # deliberate failure
|
38
|
-
it { tatooine.should_not exist }
|
39
|
-
end
|
40
|
-
"""
|
41
|
-
When I run `rspec exist_matcher_spec.rb`
|
42
|
-
Then the output should contain all of these:
|
43
|
-
| 4 examples, 2 failures |
|
44
|
-
| expected <Planet: Earth> not to exist |
|
45
|
-
| expected <Planet: Tatooine> to exist |
|
@@ -1,59 +0,0 @@
|
|
1
|
-
Feature: expect change
|
2
|
-
|
3
|
-
Expect the execution of a block of code to change the state of an object.
|
4
|
-
|
5
|
-
Background:
|
6
|
-
Given a file named "lib/counter.rb" with:
|
7
|
-
"""ruby
|
8
|
-
class Counter
|
9
|
-
class << self
|
10
|
-
def increment
|
11
|
-
@count ||= 0
|
12
|
-
@count += 1
|
13
|
-
end
|
14
|
-
|
15
|
-
def count
|
16
|
-
@count ||= 0
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
"""
|
21
|
-
|
22
|
-
Scenario: expect change
|
23
|
-
Given a file named "spec/example_spec.rb" with:
|
24
|
-
"""ruby
|
25
|
-
require "counter"
|
26
|
-
|
27
|
-
describe Counter, "#increment" do
|
28
|
-
it "should increment the count" do
|
29
|
-
expect { Counter.increment }.to change{Counter.count}.from(0).to(1)
|
30
|
-
end
|
31
|
-
|
32
|
-
# deliberate failure
|
33
|
-
it "should increment the count by 2" do
|
34
|
-
expect { Counter.increment }.to change{Counter.count}.by(2)
|
35
|
-
end
|
36
|
-
end
|
37
|
-
"""
|
38
|
-
When I run `rspec spec/example_spec.rb`
|
39
|
-
Then the output should contain "1 failure"
|
40
|
-
Then the output should contain "should have been changed by 2, but was changed by 1"
|
41
|
-
|
42
|
-
Scenario: expect no change
|
43
|
-
Given a file named "spec/example_spec.rb" with:
|
44
|
-
"""ruby
|
45
|
-
require "counter"
|
46
|
-
|
47
|
-
describe Counter, "#increment" do
|
48
|
-
it "should not increment the count by 1 (using not_to)" do
|
49
|
-
expect { Counter.increment }.not_to change{Counter.count}
|
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
|
-
end
|
56
|
-
"""
|
57
|
-
When I run `rspec spec/example_spec.rb`
|
58
|
-
Then the output should contain "2 failures"
|
59
|
-
Then the output should contain "should not have changed, but did change from 1 to 2"
|