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,33 +0,0 @@
|
|
1
|
-
Feature: satisfy matcher
|
2
|
-
|
3
|
-
The satisfy matcher is extremely flexible and can handle almost anything
|
4
|
-
you want to specify. It passes if the block you provide returns true:
|
5
|
-
|
6
|
-
```ruby
|
7
|
-
expect(10).to satisfy { |v| v % 5 == 0 }
|
8
|
-
expect(7).not_to satisfy { |v| v % 5 == 0 }
|
9
|
-
```
|
10
|
-
|
11
|
-
This flexibility comes at a cost, however: the failure message
|
12
|
-
("expected [actual] to satisfy block") is not very descriptive
|
13
|
-
or helpful. You will usually be better served by using one of
|
14
|
-
the other built-in matchers, or writing a custom matcher.
|
15
|
-
|
16
|
-
Scenario: basic usage
|
17
|
-
Given a file named "satisfy_matcher_spec.rb" with:
|
18
|
-
"""ruby
|
19
|
-
describe 10 do
|
20
|
-
it { should satisfy { |v| v > 5 } }
|
21
|
-
it { should_not satisfy { |v| v > 15 } }
|
22
|
-
|
23
|
-
# deliberate failures
|
24
|
-
it { should_not satisfy { |v| v > 5 } }
|
25
|
-
it { should satisfy { |v| v > 15 } }
|
26
|
-
end
|
27
|
-
"""
|
28
|
-
When I run `rspec satisfy_matcher_spec.rb`
|
29
|
-
Then the output should contain all of these:
|
30
|
-
| 4 examples, 2 failures |
|
31
|
-
| expected 10 not to satisfy block |
|
32
|
-
| expected 10 to satisfy block |
|
33
|
-
|
@@ -1,48 +0,0 @@
|
|
1
|
-
Feature: start_with matcher
|
2
|
-
|
3
|
-
Use the `start_with` matcher to specify that a string or array starts with
|
4
|
-
the expected characters or elements.
|
5
|
-
|
6
|
-
```ruby
|
7
|
-
expect("this string").to start_with("this")
|
8
|
-
expect("this string").not_to start_with("that")
|
9
|
-
expect([0,1,2]).to start_with(0, 1)
|
10
|
-
```
|
11
|
-
|
12
|
-
Scenario: with a string
|
13
|
-
Given a file named "example_spec.rb" with:
|
14
|
-
"""ruby
|
15
|
-
describe "this string" do
|
16
|
-
it { should start_with "this" }
|
17
|
-
it { should_not start_with "that" }
|
18
|
-
|
19
|
-
# deliberate failures
|
20
|
-
it { should_not start_with "this" }
|
21
|
-
it { should start_with "that" }
|
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 start with "this" |
|
28
|
-
| expected "this string" to start with "that" |
|
29
|
-
|
30
|
-
Scenario: with an array
|
31
|
-
Given a file named "example_spec.rb" with:
|
32
|
-
"""ruby
|
33
|
-
describe [0, 1, 2, 3, 4] do
|
34
|
-
it { should start_with 0 }
|
35
|
-
it { should start_with(0, 1)}
|
36
|
-
it { should_not start_with(2) }
|
37
|
-
it { should_not start_with(0, 1, 2, 3, 4, 5) }
|
38
|
-
|
39
|
-
# deliberate failures
|
40
|
-
it { should_not start_with 0 }
|
41
|
-
it { should start_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 start with 0 |
|
48
|
-
| expected [0, 1, 2, 3, 4] to start with 3 |
|
@@ -1,91 +0,0 @@
|
|
1
|
-
Feature: throw_symbol matcher
|
2
|
-
|
3
|
-
The throw_symbol matcher is used to specify that a block of code
|
4
|
-
throws a symbol. The most basic form passes if any symbol is thrown:
|
5
|
-
|
6
|
-
```ruby
|
7
|
-
expect { throw :foo }.to throw_symbol
|
8
|
-
```
|
9
|
-
|
10
|
-
You'll often want to specify that a particular symbol is thrown:
|
11
|
-
|
12
|
-
```ruby
|
13
|
-
expect { throw :foo }.to throw_symbol(:foo)
|
14
|
-
```
|
15
|
-
|
16
|
-
If you care about the additional argument given to throw, you can
|
17
|
-
specify that as well:
|
18
|
-
|
19
|
-
```ruby
|
20
|
-
expect { throw :foo, 7 }.to throw_symbol(:foo, 7)
|
21
|
-
```
|
22
|
-
|
23
|
-
Scenario: basic usage
|
24
|
-
Given a file named "throw_symbol_matcher_spec.rb" with:
|
25
|
-
"""ruby
|
26
|
-
describe "throw" do
|
27
|
-
specify { expect { throw :foo }.to throw_symbol }
|
28
|
-
specify { expect { throw :bar, 7 }.to throw_symbol }
|
29
|
-
specify { expect { 5 + 5 }.not_to throw_symbol }
|
30
|
-
|
31
|
-
# deliberate failures
|
32
|
-
specify { expect { throw :foo }.not_to throw_symbol }
|
33
|
-
specify { expect { throw :bar, 7 }.not_to throw_symbol }
|
34
|
-
specify { expect { 5 + 5 }.to throw_symbol }
|
35
|
-
end
|
36
|
-
"""
|
37
|
-
When I run `rspec throw_symbol_matcher_spec.rb`
|
38
|
-
Then the output should contain all of these:
|
39
|
-
| 6 examples, 3 failures |
|
40
|
-
| expected no Symbol to be thrown, got :foo |
|
41
|
-
| expected no Symbol to be thrown, got :bar |
|
42
|
-
| expected a Symbol to be thrown, got nothing |
|
43
|
-
|
44
|
-
Scenario: specify thrown symbol
|
45
|
-
Given a file named "throw_symbol_matcher_spec.rb" with:
|
46
|
-
"""ruby
|
47
|
-
describe "throw symbol" do
|
48
|
-
specify { expect { throw :foo }.to throw_symbol(:foo) }
|
49
|
-
specify { expect { throw :foo, 7 }.to throw_symbol(:foo) }
|
50
|
-
specify { expect { 5 + 5 }.not_to throw_symbol(:foo) }
|
51
|
-
specify { expect { throw :bar }.not_to throw_symbol(:foo) }
|
52
|
-
|
53
|
-
# deliberate failures
|
54
|
-
specify { expect { throw :foo }.not_to throw_symbol(:foo) }
|
55
|
-
specify { expect { throw :foo, 7 }.not_to throw_symbol(:foo) }
|
56
|
-
specify { expect { 5 + 5 }.to throw_symbol(:foo) }
|
57
|
-
specify { expect { throw :bar }.to throw_symbol(:foo) }
|
58
|
-
end
|
59
|
-
"""
|
60
|
-
When I run `rspec throw_symbol_matcher_spec.rb`
|
61
|
-
Then the output should contain all of these:
|
62
|
-
| 8 examples, 4 failures |
|
63
|
-
| expected :foo not to be thrown, got :foo |
|
64
|
-
| expected :foo not to be thrown, got :foo with 7 |
|
65
|
-
| expected :foo to be thrown, got nothing |
|
66
|
-
| expected :foo to be thrown, got :bar |
|
67
|
-
|
68
|
-
Scenario: specify thrown symbol and argument
|
69
|
-
Given a file named "throw_symbol_argument_matcher_spec.rb" with:
|
70
|
-
"""ruby
|
71
|
-
describe "throw symbol with argument" do
|
72
|
-
specify { expect { throw :foo, 7 }.to throw_symbol(:foo, 7) }
|
73
|
-
specify { expect { throw :foo, 8 }.not_to throw_symbol(:foo, 7) }
|
74
|
-
specify { expect { throw :bar, 7 }.not_to throw_symbol(:foo, 7) }
|
75
|
-
specify { expect { throw :foo }.not_to throw_symbol(:foo, 7) }
|
76
|
-
|
77
|
-
# deliberate failures
|
78
|
-
specify { expect { throw :foo, 7 }.not_to throw_symbol(:foo, 7) }
|
79
|
-
specify { expect { throw :foo, 8 }.to throw_symbol(:foo, 7) }
|
80
|
-
specify { expect { throw :bar, 7 }.to throw_symbol(:foo, 7) }
|
81
|
-
specify { expect { throw :foo }.to throw_symbol(:foo, 7) }
|
82
|
-
end
|
83
|
-
"""
|
84
|
-
When I run `rspec throw_symbol_argument_matcher_spec.rb`
|
85
|
-
Then the output should contain all of these:
|
86
|
-
| 8 examples, 4 failures |
|
87
|
-
| expected :foo with 7 not to be thrown, got :foo with 7 |
|
88
|
-
| expected :foo with 7 to be thrown, got :foo with 8 |
|
89
|
-
| expected :foo with 7 to be thrown, got :bar |
|
90
|
-
| expected :foo with 7 to be thrown, got :foo with no argument |
|
91
|
-
|
@@ -1,116 +0,0 @@
|
|
1
|
-
Feature: specify types of objects
|
2
|
-
|
3
|
-
rspec-expectations includes two matchers to specify types of objects:
|
4
|
-
|
5
|
-
* `expect(obj).to be_kind_of(type)`: calls `obj.kind_of?(type)`, which returns
|
6
|
-
true if type is in obj's class hierarchy or is a module and is
|
7
|
-
included in a class in obj's class hierarchy.
|
8
|
-
* `expect(obj).to be_instance_of(type)`: calls `obj.instance_of?(type)`, which
|
9
|
-
returns true if and only if type if obj's class.
|
10
|
-
|
11
|
-
Both of these matchers have aliases:
|
12
|
-
|
13
|
-
```ruby
|
14
|
-
expect(obj).to be_a_kind_of(type) # same as expect(obj).to be_kind_of(type)
|
15
|
-
expect(obj).to be_a(type) # same as expect(obj).to be_kind_of(type)
|
16
|
-
expect(obj).to be_an(type) # same as expect(obj).to be_kind_of(type)
|
17
|
-
expect(obj).to be_an_instance_of(type) # same as expect(obj).to be_instance_of(type)
|
18
|
-
```
|
19
|
-
|
20
|
-
Scenario: be_(a_)kind_of matcher
|
21
|
-
Given a file named "be_kind_of_matcher_spec.rb" with:
|
22
|
-
"""ruby
|
23
|
-
module MyModule; end
|
24
|
-
|
25
|
-
class Fixnum
|
26
|
-
include MyModule
|
27
|
-
end
|
28
|
-
|
29
|
-
describe 17 do
|
30
|
-
# the actual class
|
31
|
-
it { should be_kind_of(Fixnum) }
|
32
|
-
it { should be_a_kind_of(Fixnum) }
|
33
|
-
it { should be_a(Fixnum) }
|
34
|
-
|
35
|
-
# the superclass
|
36
|
-
it { should be_kind_of(Integer) }
|
37
|
-
it { should be_a_kind_of(Integer) }
|
38
|
-
it { should be_an(Integer) }
|
39
|
-
|
40
|
-
# an included module
|
41
|
-
it { should be_kind_of(MyModule) }
|
42
|
-
it { should be_a_kind_of(MyModule) }
|
43
|
-
it { should be_a(MyModule) }
|
44
|
-
|
45
|
-
# negative passing case
|
46
|
-
it { should_not be_kind_of(String) }
|
47
|
-
it { should_not be_a_kind_of(String) }
|
48
|
-
it { should_not be_a(String) }
|
49
|
-
|
50
|
-
# deliberate failures
|
51
|
-
it { should_not be_kind_of(Fixnum) }
|
52
|
-
it { should_not be_a_kind_of(Fixnum) }
|
53
|
-
it { should_not be_a(Fixnum) }
|
54
|
-
it { should_not be_kind_of(Integer) }
|
55
|
-
it { should_not be_a_kind_of(Integer) }
|
56
|
-
it { should_not be_an(Integer) }
|
57
|
-
it { should_not be_kind_of(MyModule) }
|
58
|
-
it { should_not be_a_kind_of(MyModule) }
|
59
|
-
it { should_not be_a(MyModule) }
|
60
|
-
it { should be_kind_of(String) }
|
61
|
-
it { should be_a_kind_of(String) }
|
62
|
-
it { should be_a(String) }
|
63
|
-
end
|
64
|
-
"""
|
65
|
-
When I run `rspec be_kind_of_matcher_spec.rb`
|
66
|
-
Then the output should contain all of these:
|
67
|
-
| 24 examples, 12 failures |
|
68
|
-
| expected 17 not to be a kind of Fixnum |
|
69
|
-
| expected 17 not to be a kind of Integer |
|
70
|
-
| expected 17 not to be a kind of MyModule |
|
71
|
-
| expected 17 to be a kind of String |
|
72
|
-
|
73
|
-
Scenario: be_(an_)instance_of matcher
|
74
|
-
Given a file named "be_instance_of_matcher_spec.rb" with:
|
75
|
-
"""ruby
|
76
|
-
module MyModule; end
|
77
|
-
|
78
|
-
class Fixnum
|
79
|
-
include MyModule
|
80
|
-
end
|
81
|
-
|
82
|
-
describe 17 do
|
83
|
-
# the actual class
|
84
|
-
it { should be_instance_of(Fixnum) }
|
85
|
-
it { should be_an_instance_of(Fixnum) }
|
86
|
-
|
87
|
-
# the superclass
|
88
|
-
it { should_not be_instance_of(Integer) }
|
89
|
-
it { should_not be_an_instance_of(Integer) }
|
90
|
-
|
91
|
-
# an included module
|
92
|
-
it { should_not be_instance_of(MyModule) }
|
93
|
-
it { should_not be_an_instance_of(MyModule) }
|
94
|
-
|
95
|
-
# another class with no relation to the subject's hierarchy
|
96
|
-
it { should_not be_instance_of(String) }
|
97
|
-
it { should_not be_an_instance_of(String) }
|
98
|
-
|
99
|
-
# deliberate failures
|
100
|
-
it { should_not be_instance_of(Fixnum) }
|
101
|
-
it { should_not be_an_instance_of(Fixnum) }
|
102
|
-
it { should be_instance_of(Integer) }
|
103
|
-
it { should be_an_instance_of(Integer) }
|
104
|
-
it { should be_instance_of(MyModule) }
|
105
|
-
it { should be_an_instance_of(MyModule) }
|
106
|
-
it { should be_instance_of(String) }
|
107
|
-
it { should be_an_instance_of(String) }
|
108
|
-
end
|
109
|
-
"""
|
110
|
-
When I run `rspec be_instance_of_matcher_spec.rb`
|
111
|
-
Then the output should contain all of these:
|
112
|
-
| 16 examples, 8 failures |
|
113
|
-
| expected 17 not to be an instance of Fixnum |
|
114
|
-
| expected 17 to be an instance of Integer |
|
115
|
-
| expected 17 to be an instance of MyModule |
|
116
|
-
| expected 17 to be an instance of String |
|
@@ -1,161 +0,0 @@
|
|
1
|
-
Feature: yield matchers
|
2
|
-
|
3
|
-
There are four related matchers that allow you to specify whether
|
4
|
-
or not a method yields, how many times it yields, whether or not
|
5
|
-
it yields with arguments, and what those arguments are.
|
6
|
-
|
7
|
-
* `yield_control` matches if the method-under-test yields, regardless
|
8
|
-
of whether or not arguments are yielded.
|
9
|
-
* `yield_with_args` matches if the method-under-test yields with
|
10
|
-
arguments. If arguments are provided to this matcher, it will
|
11
|
-
only pass if the actual yielded arguments match the expected ones
|
12
|
-
using `===` or `==`.
|
13
|
-
* `yield_with_no_args` matches if the method-under-test yields with
|
14
|
-
no arguments.
|
15
|
-
* `yield_successive_args` is designed for iterators, and will match
|
16
|
-
if the method-under-test yields the same number of times as arguments
|
17
|
-
passed to this matcher, and all actual yielded arguments match the
|
18
|
-
expected ones using `===` or `==`.
|
19
|
-
|
20
|
-
Note: your expect block _must_ accept an argument that is then passed on to
|
21
|
-
the method-under-test as a block. This acts as a "probe" that allows the matcher
|
22
|
-
to detect whether or not your method yields, and, if so, how many times and what
|
23
|
-
the yielded arguments are.
|
24
|
-
|
25
|
-
Background:
|
26
|
-
Given a file named "my_class.rb" with:
|
27
|
-
"""ruby
|
28
|
-
class MyClass
|
29
|
-
def self.yield_once_with(*args)
|
30
|
-
yield *args
|
31
|
-
end
|
32
|
-
|
33
|
-
def self.yield_twice_with(*args)
|
34
|
-
2.times { yield *args }
|
35
|
-
end
|
36
|
-
|
37
|
-
def self.raw_yield
|
38
|
-
yield
|
39
|
-
end
|
40
|
-
|
41
|
-
def self.dont_yield
|
42
|
-
end
|
43
|
-
end
|
44
|
-
"""
|
45
|
-
|
46
|
-
Scenario: yield_control matcher
|
47
|
-
Given a file named "yield_control_spec.rb" with:
|
48
|
-
"""ruby
|
49
|
-
require './my_class'
|
50
|
-
|
51
|
-
describe "yield_control matcher" do
|
52
|
-
specify { expect { |b| MyClass.yield_once_with(1, &b) }.to yield_control }
|
53
|
-
specify { expect { |b| MyClass.dont_yield(&b) }.not_to yield_control }
|
54
|
-
specify { expect { |b| MyClass.yield_twice_with(1, &b) }.to yield_control.twice }
|
55
|
-
specify { expect { |b| MyClass.yield_twice_with(1, &b) }.to yield_control.exactly(2).times }
|
56
|
-
specify { expect { |b| MyClass.yield_twice_with(1, &b) }.to yield_control.at_least(1) }
|
57
|
-
specify { expect { |b| MyClass.yield_twice_with(1, &b) }.to yield_control.at_most(3).times }
|
58
|
-
|
59
|
-
# deliberate failures
|
60
|
-
specify { expect { |b| MyClass.yield_once_with(1, &b) }.not_to yield_control }
|
61
|
-
specify { expect { |b| MyClass.dont_yield(&b) }.to yield_control }
|
62
|
-
specify { expect { |b| MyClass.yield_once_with(1, &b) }.to yield_control.at_least(2).times }
|
63
|
-
specify { expect { |b| MyClass.yield_twice_with(1, &b) }.not_to yield_control.twice }
|
64
|
-
specify { expect { |b| MyClass.yield_twice_with(1, &b) }.not_to yield_control.at_least(2).times }
|
65
|
-
specify { expect { |b| MyClass.yield_twice_with(1, &b) }.not_to yield_control.at_least(1) }
|
66
|
-
specify { expect { |b| MyClass.yield_twice_with(1, &b) }.not_to yield_control.at_most(3).times }
|
67
|
-
end
|
68
|
-
"""
|
69
|
-
When I run `rspec yield_control_spec.rb`
|
70
|
-
Then the output should contain all of these:
|
71
|
-
| 13 examples, 7 failures |
|
72
|
-
| expected given block to yield control |
|
73
|
-
| expected given block not to yield control |
|
74
|
-
| expected given block not to yield control at least twice |
|
75
|
-
| expected given block not to yield control at most 3 times |
|
76
|
-
|
77
|
-
Scenario: yield_with_args matcher
|
78
|
-
Given a file named "yield_with_args_spec.rb" with:
|
79
|
-
"""ruby
|
80
|
-
require './my_class'
|
81
|
-
|
82
|
-
describe "yield_with_args matcher" do
|
83
|
-
specify { expect { |b| MyClass.yield_once_with("foo", &b) }.to yield_with_args }
|
84
|
-
specify { expect { |b| MyClass.yield_once_with("foo", &b) }.to yield_with_args("foo") }
|
85
|
-
specify { expect { |b| MyClass.yield_once_with("foo", &b) }.to yield_with_args(String) }
|
86
|
-
specify { expect { |b| MyClass.yield_once_with("foo", &b) }.to yield_with_args(/oo/) }
|
87
|
-
|
88
|
-
specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.to yield_with_args("foo", "bar") }
|
89
|
-
specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.to yield_with_args(String, String) }
|
90
|
-
specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.to yield_with_args(/fo/, /ar/) }
|
91
|
-
|
92
|
-
specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.not_to yield_with_args(17, "baz") }
|
93
|
-
|
94
|
-
# deliberate failures
|
95
|
-
specify { expect { |b| MyClass.yield_once_with("foo", &b) }.not_to yield_with_args }
|
96
|
-
specify { expect { |b| MyClass.yield_once_with("foo", &b) }.not_to yield_with_args("foo") }
|
97
|
-
specify { expect { |b| MyClass.yield_once_with("foo", &b) }.not_to yield_with_args(String) }
|
98
|
-
specify { expect { |b| MyClass.yield_once_with("foo", &b) }.not_to yield_with_args(/oo/) }
|
99
|
-
specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.not_to yield_with_args("foo", "bar") }
|
100
|
-
specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.to yield_with_args(17, "baz") }
|
101
|
-
end
|
102
|
-
"""
|
103
|
-
When I run `rspec yield_with_args_spec.rb`
|
104
|
-
Then the output should contain all of these:
|
105
|
-
| 14 examples, 6 failures |
|
106
|
-
| expected given block not to yield with arguments, but did |
|
107
|
-
| expected given block not to yield with arguments, but yielded with expected arguments |
|
108
|
-
| expected given block to yield with arguments, but yielded with unexpected arguments |
|
109
|
-
|
110
|
-
Scenario: yield_with_no_args matcher
|
111
|
-
Given a file named "yield_with_no_args_spec.rb" with:
|
112
|
-
"""ruby
|
113
|
-
require './my_class'
|
114
|
-
|
115
|
-
describe "yield_with_no_args matcher" do
|
116
|
-
specify { expect { |b| MyClass.raw_yield(&b) }.to yield_with_no_args }
|
117
|
-
specify { expect { |b| MyClass.dont_yield(&b) }.not_to yield_with_no_args }
|
118
|
-
specify { expect { |b| MyClass.yield_once_with("a", &b) }.not_to yield_with_no_args }
|
119
|
-
|
120
|
-
# deliberate failures
|
121
|
-
specify { expect { |b| MyClass.raw_yield(&b) }.not_to yield_with_no_args }
|
122
|
-
specify { expect { |b| MyClass.dont_yield(&b) }.to yield_with_no_args }
|
123
|
-
specify { expect { |b| MyClass.yield_once_with("a", &b) }.to yield_with_no_args }
|
124
|
-
end
|
125
|
-
"""
|
126
|
-
When I run `rspec yield_with_no_args_spec.rb`
|
127
|
-
Then the output should contain all of these:
|
128
|
-
| 6 examples, 3 failures |
|
129
|
-
| expected given block not to yield with no arguments, but did |
|
130
|
-
| expected given block to yield with no arguments, but did not yield |
|
131
|
-
| expected given block to yield with no arguments, but yielded with arguments: ["a"] |
|
132
|
-
|
133
|
-
Scenario: yield_successive_args matcher
|
134
|
-
Given a file named "yield_successive_args_spec.rb" with:
|
135
|
-
"""ruby
|
136
|
-
def array
|
137
|
-
[1, 2, 3]
|
138
|
-
end
|
139
|
-
|
140
|
-
def array_of_tuples
|
141
|
-
[[:a, :b], [:c, :d]]
|
142
|
-
end
|
143
|
-
|
144
|
-
describe "yield_successive_args matcher" do
|
145
|
-
specify { expect { |b| array.each(&b) }.to yield_successive_args(1, 2, 3) }
|
146
|
-
specify { expect { |b| array_of_tuples.each(&b) }.to yield_successive_args([:a, :b], [:c, :d]) }
|
147
|
-
specify { expect { |b| array.each(&b) }.to yield_successive_args(Fixnum, Fixnum, Fixnum) }
|
148
|
-
specify { expect { |b| array.each(&b) }.not_to yield_successive_args(1, 2) }
|
149
|
-
|
150
|
-
# deliberate failures
|
151
|
-
specify { expect { |b| array.each(&b) }.not_to yield_successive_args(1, 2, 3) }
|
152
|
-
specify { expect { |b| array_of_tuples.each(&b) }.not_to yield_successive_args([:a, :b], [:c, :d]) }
|
153
|
-
specify { expect { |b| array.each(&b) }.not_to yield_successive_args(Fixnum, Fixnum, Fixnum) }
|
154
|
-
specify { expect { |b| array.each(&b) }.to yield_successive_args(1, 2) }
|
155
|
-
end
|
156
|
-
"""
|
157
|
-
When I run `rspec yield_successive_args_spec.rb`
|
158
|
-
Then the output should contain all of these:
|
159
|
-
| 8 examples, 4 failures |
|
160
|
-
| expected given block not to yield successively with arguments, but yielded with expected arguments |
|
161
|
-
| expected given block to yield successively with arguments, but yielded with unexpected arguments |
|