rspec-expectations 3.0.0.beta2 → 3.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +2 -2
- data/.yardopts +0 -1
- data/Changelog.md +115 -35
- data/README.md +2 -2
- data/lib/rspec/expectations.rb +13 -8
- data/lib/rspec/{matchers → expectations}/configuration.rb +38 -13
- data/lib/rspec/expectations/expectation_target.rb +72 -8
- data/lib/rspec/expectations/fail_with.rb +10 -52
- data/lib/rspec/expectations/handler.rb +9 -11
- data/lib/rspec/expectations/syntax.rb +37 -35
- data/lib/rspec/expectations/version.rb +1 -1
- data/lib/rspec/matchers.rb +60 -9
- data/lib/rspec/matchers/aliased_matcher.rb +6 -0
- data/lib/rspec/matchers/built_in.rb +9 -1
- data/lib/rspec/matchers/built_in/all.rb +78 -0
- data/lib/rspec/matchers/built_in/base_matcher.rb +39 -1
- data/lib/rspec/matchers/built_in/be.rb +117 -42
- data/lib/rspec/matchers/built_in/be_between.rb +22 -0
- data/lib/rspec/matchers/built_in/be_instance_of.rb +11 -3
- data/lib/rspec/matchers/built_in/be_kind_of.rb +5 -0
- data/lib/rspec/matchers/built_in/be_within.rb +26 -6
- data/lib/rspec/matchers/built_in/change.rb +89 -13
- data/lib/rspec/matchers/built_in/compound.rb +19 -3
- data/lib/rspec/matchers/built_in/contain_exactly.rb +17 -6
- data/lib/rspec/matchers/built_in/cover.rb +3 -0
- data/lib/rspec/matchers/built_in/eq.rb +20 -5
- data/lib/rspec/matchers/built_in/eql.rb +15 -3
- data/lib/rspec/matchers/built_in/equal.rb +23 -6
- data/lib/rspec/matchers/built_in/exist.rb +74 -10
- data/lib/rspec/matchers/built_in/has.rb +58 -3
- data/lib/rspec/matchers/built_in/include.rb +16 -1
- data/lib/rspec/matchers/built_in/match.rb +14 -4
- data/lib/rspec/matchers/built_in/operators.rb +16 -0
- data/lib/rspec/matchers/built_in/output.rb +47 -5
- data/lib/rspec/matchers/built_in/raise_error.rb +40 -23
- data/lib/rspec/matchers/built_in/respond_to.rb +37 -16
- data/lib/rspec/matchers/built_in/satisfy.rb +15 -0
- data/lib/rspec/matchers/built_in/start_and_end_with.rb +29 -14
- data/lib/rspec/matchers/built_in/throw_symbol.rb +32 -3
- data/lib/rspec/matchers/built_in/yield.rb +148 -44
- data/lib/rspec/matchers/composable.rb +48 -7
- data/lib/rspec/matchers/dsl.rb +45 -17
- data/lib/rspec/matchers/generated_descriptions.rb +7 -0
- data/lib/rspec/matchers/matcher_delegator.rb +6 -2
- data/lib/rspec/matchers/pretty.rb +15 -19
- metadata +33 -236
- metadata.gz.sig +0 -0
- data/features/README.md +0 -48
- data/features/Upgrade.md +0 -53
- data/features/built_in_matchers/README.md +0 -96
- data/features/built_in_matchers/be.feature +0 -175
- data/features/built_in_matchers/be_within.feature +0 -48
- data/features/built_in_matchers/comparisons.feature +0 -97
- data/features/built_in_matchers/contain_exactly.feature +0 -46
- 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 -136
- 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/include.feature +0 -126
- data/features/built_in_matchers/match.feature +0 -51
- data/features/built_in_matchers/output.feature +0 -70
- data/features/built_in_matchers/predicates.feature +0 -161
- 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/composing_matchers.feature +0 -250
- data/features/compound_expectations.feature +0 -45
- 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 -340
- data/features/custom_matchers/define_matcher_outside_rspec.feature +0 -34
- data/features/custom_matchers/define_matcher_with_fluent_interface.feature +0 -24
- data/features/customized_message.feature +0 -39
- 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 -21
- data/features/support/rubinius.rb +0 -6
- data/features/syntax_configuration.feature +0 -71
- data/features/test_frameworks/minitest.feature +0 -44
- data/lib/rspec-expectations.rb +0 -1
- data/lib/rspec/expectations/diff_presenter.rb +0 -141
- data/lib/rspec/expectations/differ.rb +0 -44
- data/lib/rspec/expectations/encoded_string.rb +0 -56
- data/spec/rspec/expectations/diff_presenter_spec.rb +0 -249
- data/spec/rspec/expectations/encoded_string_spec.rb +0 -74
- 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 -205
- data/spec/rspec/expectations/minitest_integration_spec.rb +0 -27
- data/spec/rspec/expectations/syntax_spec.rb +0 -89
- data/spec/rspec/expectations_spec.rb +0 -12
- data/spec/rspec/matchers/aliased_matcher_spec.rb +0 -48
- data/spec/rspec/matchers/aliases_spec.rb +0 -449
- data/spec/rspec/matchers/built_in/base_matcher_spec.rb +0 -83
- data/spec/rspec/matchers/built_in/be_between_spec.rb +0 -159
- data/spec/rspec/matchers/built_in/be_instance_of_spec.rb +0 -63
- data/spec/rspec/matchers/built_in/be_kind_of_spec.rb +0 -41
- data/spec/rspec/matchers/built_in/be_spec.rb +0 -592
- data/spec/rspec/matchers/built_in/be_within_spec.rb +0 -141
- data/spec/rspec/matchers/built_in/change_spec.rb +0 -808
- data/spec/rspec/matchers/built_in/compound_spec.rb +0 -292
- data/spec/rspec/matchers/built_in/contain_exactly_spec.rb +0 -441
- data/spec/rspec/matchers/built_in/cover_spec.rb +0 -69
- data/spec/rspec/matchers/built_in/eq_spec.rb +0 -156
- data/spec/rspec/matchers/built_in/eql_spec.rb +0 -41
- data/spec/rspec/matchers/built_in/equal_spec.rb +0 -106
- data/spec/rspec/matchers/built_in/exist_spec.rb +0 -124
- data/spec/rspec/matchers/built_in/has_spec.rb +0 -161
- data/spec/rspec/matchers/built_in/include_spec.rb +0 -540
- data/spec/rspec/matchers/built_in/match_spec.rb +0 -102
- data/spec/rspec/matchers/built_in/operators_spec.rb +0 -252
- data/spec/rspec/matchers/built_in/output_spec.rb +0 -165
- data/spec/rspec/matchers/built_in/raise_error_spec.rb +0 -461
- data/spec/rspec/matchers/built_in/respond_to_spec.rb +0 -292
- data/spec/rspec/matchers/built_in/satisfy_spec.rb +0 -44
- data/spec/rspec/matchers/built_in/start_and_end_with_spec.rb +0 -253
- data/spec/rspec/matchers/built_in/throw_symbol_spec.rb +0 -135
- data/spec/rspec/matchers/built_in/yield_spec.rb +0 -627
- data/spec/rspec/matchers/configuration_spec.rb +0 -213
- data/spec/rspec/matchers/description_generation_spec.rb +0 -191
- data/spec/rspec/matchers/dsl_spec.rb +0 -895
- data/spec/rspec/matchers/legacy_spec.rb +0 -101
- data/spec/rspec/matchers_spec.rb +0 -74
- data/spec/spec_helper.rb +0 -85
- data/spec/support/matchers.rb +0 -22
- data/spec/support/shared_examples.rb +0 -35
@@ -1,46 +0,0 @@
|
|
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
|
-
|
@@ -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
|
-
expect(1..10).to cover(5)
|
10
|
-
expect(1..10).to cover(4, 6)
|
11
|
-
expect(1..10).not_to 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
|
-
expect("this string").to end_with "string"
|
8
|
-
expect("this string").not_to end_with "stringy"
|
9
|
-
expect([0, 1, 2]).to 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,136 +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
|
-
expect(a).to equal(b) # passes if a.equal?(b)
|
17
|
-
expect(a).to eql(b) # passes if a.eql?(b)
|
18
|
-
expect(a).to be == 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
|
-
expect(a).to be(b) # passes if a.equal?(b)
|
25
|
-
expect(a).to eq(b) # passes if a == b
|
26
|
-
```
|
27
|
-
|
28
|
-
Scenario: compare using eq (==)
|
29
|
-
Given a file named "compare_using_eq.rb" with:
|
30
|
-
"""ruby
|
31
|
-
describe "a string" do
|
32
|
-
it "is equal to another string of the same value" do
|
33
|
-
expect("this string").to eq("this string")
|
34
|
-
end
|
35
|
-
|
36
|
-
it "is not equal to another string of a different value" do
|
37
|
-
expect("this string").not_to eq("a different string")
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
describe "an integer" do
|
42
|
-
it "is equal to a float of the same value" do
|
43
|
-
expect(5).to eq(5.0)
|
44
|
-
end
|
45
|
-
end
|
46
|
-
"""
|
47
|
-
When I run `rspec compare_using_eq.rb`
|
48
|
-
Then the output should contain "3 examples, 0 failures"
|
49
|
-
|
50
|
-
Scenario: compare using ==
|
51
|
-
Given a file named "compare_using_==.rb" with:
|
52
|
-
"""ruby
|
53
|
-
describe "a string" do
|
54
|
-
it "is equal to another string of the same value" do
|
55
|
-
expect("this string").to be == "this string"
|
56
|
-
end
|
57
|
-
|
58
|
-
it "is not equal to another string of a different value" do
|
59
|
-
expect("this string").not_to be == "a different string"
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
describe "an integer" do
|
64
|
-
it "is equal to a float of the same value" do
|
65
|
-
expect(5).to be == 5.0
|
66
|
-
end
|
67
|
-
end
|
68
|
-
"""
|
69
|
-
When I run `rspec compare_using_==.rb`
|
70
|
-
Then the output should contain "3 examples, 0 failures"
|
71
|
-
|
72
|
-
Scenario: compare using eql (eql?)
|
73
|
-
Given a file named "compare_using_eql.rb" with:
|
74
|
-
"""ruby
|
75
|
-
describe "an integer" do
|
76
|
-
it "is equal to another integer of the same value" do
|
77
|
-
expect(5).to eql(5)
|
78
|
-
end
|
79
|
-
|
80
|
-
it "is not equal to another integer of a different value" do
|
81
|
-
expect(5).not_to eql(6)
|
82
|
-
end
|
83
|
-
|
84
|
-
it "is not equal to a float of the same value" do
|
85
|
-
expect(5).not_to eql(5.0)
|
86
|
-
end
|
87
|
-
|
88
|
-
end
|
89
|
-
"""
|
90
|
-
When I run `rspec compare_using_eql.rb`
|
91
|
-
Then the output should contain "3 examples, 0 failures"
|
92
|
-
|
93
|
-
Scenario: compare using equal (equal?)
|
94
|
-
Given a file named "compare_using_equal.rb" with:
|
95
|
-
"""ruby
|
96
|
-
describe "a string" do
|
97
|
-
it "is equal to itself" do
|
98
|
-
string = "this string"
|
99
|
-
expect(string).to equal(string)
|
100
|
-
end
|
101
|
-
|
102
|
-
it "is not equal to another string of the same value" do
|
103
|
-
expect("this string").not_to equal("this string")
|
104
|
-
end
|
105
|
-
|
106
|
-
it "is not equal to another string of a different value" do
|
107
|
-
expect("this string").not_to equal("a different string")
|
108
|
-
end
|
109
|
-
|
110
|
-
end
|
111
|
-
"""
|
112
|
-
When I run `rspec compare_using_equal.rb`
|
113
|
-
Then the output should contain "3 examples, 0 failures"
|
114
|
-
|
115
|
-
Scenario: compare using be (equal?)
|
116
|
-
Given a file named "compare_using_be.rb" with:
|
117
|
-
"""ruby
|
118
|
-
describe "a string" do
|
119
|
-
it "is equal to itself" do
|
120
|
-
string = "this string"
|
121
|
-
expect(string).to be(string)
|
122
|
-
end
|
123
|
-
|
124
|
-
it "is not equal to another string of the same value" do
|
125
|
-
expect("this string").not_to be("this string")
|
126
|
-
end
|
127
|
-
|
128
|
-
it "is not equal to another string of a different value" do
|
129
|
-
expect("this string").not_to be("a different string")
|
130
|
-
end
|
131
|
-
|
132
|
-
end
|
133
|
-
"""
|
134
|
-
When I run `rspec compare_using_be.rb`
|
135
|
-
Then the output should contain "3 examples, 0 failures"
|
136
|
-
|
@@ -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
|
-
expect(obj).to 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 { expect(earth).to exist }
|
32
|
-
specify { expect(earth).not_to exist } # deliberate failure
|
33
|
-
end
|
34
|
-
|
35
|
-
describe "Tatooine" do
|
36
|
-
let(:tatooine) { Planet.new("Tatooine") }
|
37
|
-
specify { expect(tatooine).to exist } # deliberate failure
|
38
|
-
specify { expect(tatooine).not_to 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 "expected result to have 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 "expected result not to have changed, but did change from 1 to 2"
|
@@ -1,144 +0,0 @@
|
|
1
|
-
Feature: raise_error matcher
|
2
|
-
|
3
|
-
Use the `raise_error` matcher to specify that a block of code raises an
|
4
|
-
error. The most basic form passes if any error is thrown:
|
5
|
-
|
6
|
-
```ruby
|
7
|
-
expect { raise StandardError }.to raise_error
|
8
|
-
```
|
9
|
-
|
10
|
-
You can use `raise_exception` instead if you prefer that wording:
|
11
|
-
|
12
|
-
```ruby
|
13
|
-
expect { 3 / 0 }.to raise_exception
|
14
|
-
```
|
15
|
-
|
16
|
-
`raise_error` and `raise_exception` are functionally interchangeable, so use
|
17
|
-
the one that makes the most sense to you in any given context.
|
18
|
-
|
19
|
-
In addition to the basic form, above, there are a number of ways to specify
|
20
|
-
details of an error/exception:
|
21
|
-
|
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
|
-
```
|
30
|
-
|
31
|
-
Scenario: expect any error
|
32
|
-
Given a file named "example_spec" with:
|
33
|
-
"""
|
34
|
-
describe "calling a missing method" do
|
35
|
-
it "raises" do
|
36
|
-
expect { Object.new.foo }.to raise_error
|
37
|
-
end
|
38
|
-
end
|
39
|
-
"""
|
40
|
-
When I run `rspec example_spec`
|
41
|
-
Then the example should pass
|
42
|
-
|
43
|
-
Scenario: expect specific error
|
44
|
-
Given a file named "example_spec" with:
|
45
|
-
"""
|
46
|
-
describe "calling a missing method" do
|
47
|
-
it "raises" do
|
48
|
-
expect { Object.new.foo }.to raise_error(NameError)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
"""
|
52
|
-
When I run `rspec example_spec`
|
53
|
-
Then the example should pass
|
54
|
-
|
55
|
-
Scenario: match message with a string
|
56
|
-
Given a file named "example_spec.rb" with:
|
57
|
-
"""ruby
|
58
|
-
describe "matching error message with string" do
|
59
|
-
it "matches the error message" do
|
60
|
-
expect { raise StandardError, 'this message exactly'}.
|
61
|
-
to raise_error('this message exactly')
|
62
|
-
end
|
63
|
-
end
|
64
|
-
"""
|
65
|
-
When I run `rspec example_spec.rb`
|
66
|
-
Then the example should pass
|
67
|
-
|
68
|
-
Scenario: match message with a regexp
|
69
|
-
Given a file named "example_spec.rb" with:
|
70
|
-
"""ruby
|
71
|
-
describe "matching error message with regex" do
|
72
|
-
it "matches the error message" do
|
73
|
-
expect { raise StandardError, "my message" }.
|
74
|
-
to raise_error(/my mess/)
|
75
|
-
end
|
76
|
-
end
|
77
|
-
"""
|
78
|
-
When I run `rspec example_spec.rb`
|
79
|
-
Then the example should pass
|
80
|
-
|
81
|
-
Scenario: matching message with `with_message`
|
82
|
-
Given a file named "example_spec.rb" with:
|
83
|
-
"""ruby
|
84
|
-
describe "matching error message with regex" do
|
85
|
-
it "matches the error message" do
|
86
|
-
expect { raise StandardError, "my message" }.
|
87
|
-
to raise_error.with_message(/my mess/)
|
88
|
-
end
|
89
|
-
end
|
90
|
-
"""
|
91
|
-
When I run `rspec example_spec.rb`
|
92
|
-
Then the example should pass
|
93
|
-
|
94
|
-
Scenario: match type + message with string
|
95
|
-
Given a file named "example_spec.rb" with:
|
96
|
-
"""ruby
|
97
|
-
describe "matching error message with string" do
|
98
|
-
it "matches the error message" do
|
99
|
-
expect { raise StandardError, 'this message exactly'}.
|
100
|
-
to raise_error(StandardError, 'this message exactly')
|
101
|
-
end
|
102
|
-
end
|
103
|
-
"""
|
104
|
-
When I run `rspec example_spec.rb`
|
105
|
-
Then the example should pass
|
106
|
-
|
107
|
-
Scenario: match type + message with regexp
|
108
|
-
Given a file named "example_spec.rb" with:
|
109
|
-
"""ruby
|
110
|
-
describe "matching error message with regex" do
|
111
|
-
it "matches the error message" do
|
112
|
-
expect { raise StandardError, "my message" }.
|
113
|
-
to raise_error(StandardError, /my mess/)
|
114
|
-
end
|
115
|
-
end
|
116
|
-
"""
|
117
|
-
When I run `rspec example_spec.rb`
|
118
|
-
Then the example should pass
|
119
|
-
|
120
|
-
Scenario: set expectations on error object passed to block
|
121
|
-
Given a file named "example_spec" with:
|
122
|
-
"""
|
123
|
-
describe "#foo" do
|
124
|
-
it "raises NameError" do
|
125
|
-
expect { Object.new.foo }.to raise_error { |error|
|
126
|
-
expect(error).to be_a(NameError)
|
127
|
-
}
|
128
|
-
end
|
129
|
-
end
|
130
|
-
"""
|
131
|
-
When I run `rspec example_spec`
|
132
|
-
Then the example should pass
|
133
|
-
|
134
|
-
Scenario: expect no error at all
|
135
|
-
Given a file named "example_spec" with:
|
136
|
-
"""
|
137
|
-
describe "#to_s" do
|
138
|
-
it "does not raise" do
|
139
|
-
expect { Object.new.to_s }.not_to raise_error
|
140
|
-
end
|
141
|
-
end
|
142
|
-
"""
|
143
|
-
When I run `rspec example_spec`
|
144
|
-
Then the example should pass
|