rspec-expectations 2.11.3 → 3.11.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 +1026 -21
- data/{License.txt → LICENSE.md} +5 -3
- data/README.md +174 -78
- 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 +130 -55
- data/lib/rspec/expectations/fail_with.rb +17 -33
- data/lib/rspec/expectations/failure_aggregator.rb +212 -0
- data/lib/rspec/expectations/handler.rb +163 -29
- data/lib/rspec/expectations/minitest_integration.rb +58 -0
- data/lib/rspec/expectations/syntax.rb +68 -54
- data/lib/rspec/expectations/version.rb +1 -1
- data/lib/rspec/expectations.rb +59 -24
- 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 +150 -20
- data/lib/rspec/matchers/built_in/be.rb +115 -109
- data/lib/rspec/matchers/built_in/be_between.rb +77 -0
- data/lib/rspec/matchers/built_in/be_instance_of.rb +16 -1
- data/lib/rspec/matchers/built_in/be_kind_of.rb +10 -1
- data/lib/rspec/matchers/built_in/be_within.rb +43 -17
- data/lib/rspec/matchers/built_in/change.rb +392 -75
- data/lib/rspec/matchers/built_in/compound.rb +290 -0
- data/lib/rspec/matchers/built_in/contain_exactly.rb +302 -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 +26 -8
- data/lib/rspec/matchers/built_in/eql.rb +19 -8
- data/lib/rspec/matchers/built_in/equal.rb +56 -19
- 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 +175 -20
- 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 +212 -38
- data/lib/rspec/matchers/built_in/respond_to.rb +155 -29
- 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 +252 -98
- data/lib/rspec/matchers/built_in.rb +47 -33
- data/lib/rspec/matchers/composable.rb +171 -0
- data/lib/rspec/matchers/dsl.rb +530 -10
- data/lib/rspec/matchers/english_phrasing.rb +58 -0
- data/lib/rspec/matchers/expecteds_for_multiple_diffs.rb +82 -0
- data/lib/rspec/matchers/fail_matchers.rb +42 -0
- data/lib/rspec/matchers/generated_descriptions.rb +15 -10
- data/lib/rspec/matchers/matcher_delegator.rb +35 -0
- data/lib/rspec/matchers/matcher_protocol.rb +105 -0
- data/lib/rspec/matchers.rb +604 -252
- data.tar.gz.sig +0 -0
- metadata +178 -278
- metadata.gz.sig +0 -0
- data/features/README.md +0 -49
- data/features/Upgrade.md +0 -53
- data/features/built_in_matchers/README.md +0 -90
- data/features/built_in_matchers/be.feature +0 -173
- data/features/built_in_matchers/be_within.feature +0 -46
- data/features/built_in_matchers/cover.feature +0 -45
- data/features/built_in_matchers/end_with.feature +0 -46
- data/features/built_in_matchers/equality.feature +0 -145
- data/features/built_in_matchers/exist.feature +0 -43
- data/features/built_in_matchers/expect_change.feature +0 -59
- data/features/built_in_matchers/expect_error.feature +0 -138
- data/features/built_in_matchers/have.feature +0 -103
- data/features/built_in_matchers/include.feature +0 -121
- data/features/built_in_matchers/match.feature +0 -50
- data/features/built_in_matchers/operators.feature +0 -221
- data/features/built_in_matchers/predicates.feature +0 -128
- data/features/built_in_matchers/respond_to.feature +0 -78
- data/features/built_in_matchers/satisfy.feature +0 -31
- data/features/built_in_matchers/start_with.feature +0 -46
- data/features/built_in_matchers/throw_symbol.feature +0 -85
- data/features/built_in_matchers/types.feature +0 -114
- data/features/built_in_matchers/yield.feature +0 -146
- 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 -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 -5
- data/features/syntax_configuration.feature +0 -68
- data/features/test_frameworks/test_unit.feature +0 -46
- data/lib/rspec/expectations/deprecation.rb +0 -38
- data/lib/rspec/expectations/differ.rb +0 -81
- 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 -39
- 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 -108
- data/lib/rspec/matchers/built_in/match_array.rb +0 -45
- 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 -66
- data/lib/rspec/matchers/extensions/instance_eval_with_args.rb +0 -39
- data/lib/rspec/matchers/matcher.rb +0 -299
- data/lib/rspec/matchers/method_missing.rb +0 -12
- data/lib/rspec/matchers/operator_matcher.rb +0 -84
- data/lib/rspec/matchers/pretty.rb +0 -60
- data/lib/rspec-expectations.rb +0 -1
- data/spec/rspec/expectations/differ_spec.rb +0 -153
- data/spec/rspec/expectations/expectation_target_spec.rb +0 -65
- data/spec/rspec/expectations/extensions/kernel_spec.rb +0 -67
- data/spec/rspec/expectations/fail_with_spec.rb +0 -70
- data/spec/rspec/expectations/handler_spec.rb +0 -206
- data/spec/rspec/matchers/base_matcher_spec.rb +0 -60
- data/spec/rspec/matchers/be_close_spec.rb +0 -22
- data/spec/rspec/matchers/be_instance_of_spec.rb +0 -40
- data/spec/rspec/matchers/be_kind_of_spec.rb +0 -37
- data/spec/rspec/matchers/be_spec.rb +0 -452
- data/spec/rspec/matchers/be_within_spec.rb +0 -80
- data/spec/rspec/matchers/change_spec.rb +0 -528
- data/spec/rspec/matchers/configuration_spec.rb +0 -202
- data/spec/rspec/matchers/cover_spec.rb +0 -69
- data/spec/rspec/matchers/description_generation_spec.rb +0 -176
- data/spec/rspec/matchers/dsl_spec.rb +0 -57
- data/spec/rspec/matchers/eq_spec.rb +0 -54
- data/spec/rspec/matchers/eql_spec.rb +0 -41
- data/spec/rspec/matchers/equal_spec.rb +0 -60
- data/spec/rspec/matchers/exist_spec.rb +0 -110
- data/spec/rspec/matchers/has_spec.rb +0 -118
- data/spec/rspec/matchers/have_spec.rb +0 -461
- data/spec/rspec/matchers/include_spec.rb +0 -367
- data/spec/rspec/matchers/match_array_spec.rb +0 -124
- data/spec/rspec/matchers/match_spec.rb +0 -61
- data/spec/rspec/matchers/matcher_spec.rb +0 -434
- data/spec/rspec/matchers/matchers_spec.rb +0 -31
- data/spec/rspec/matchers/method_missing_spec.rb +0 -24
- data/spec/rspec/matchers/operator_matcher_spec.rb +0 -221
- data/spec/rspec/matchers/raise_error_spec.rb +0 -344
- data/spec/rspec/matchers/respond_to_spec.rb +0 -295
- data/spec/rspec/matchers/satisfy_spec.rb +0 -44
- data/spec/rspec/matchers/start_with_end_with_spec.rb +0 -182
- data/spec/rspec/matchers/throw_symbol_spec.rb +0 -116
- data/spec/rspec/matchers/yield_spec.rb +0 -402
- data/spec/spec_helper.rb +0 -27
- data/spec/support/classes.rb +0 -56
- data/spec/support/in_sub_process.rb +0 -31
- data/spec/support/matchers.rb +0 -22
- data/spec/support/ruby_version.rb +0 -10
- data/spec/support/shared_examples.rb +0 -13
@@ -1,128 +0,0 @@
|
|
1
|
-
Feature: predicate matchers
|
2
|
-
|
3
|
-
Ruby objects commonly provide predicate methods:
|
4
|
-
|
5
|
-
7.zero? # => false
|
6
|
-
0.zero? # => true
|
7
|
-
[1].empty? # => false
|
8
|
-
[].empty? # => true
|
9
|
-
{ :a => 5 }.has_key?(:b) # => false
|
10
|
-
{ :b => 5 }.has_key?(:b) # => true
|
11
|
-
|
12
|
-
You could use a basic equality matcher to set expectations on these:
|
13
|
-
|
14
|
-
7.zero?.should == true # fails with "expected true, got false (using ==)"
|
15
|
-
|
16
|
-
...but RSpec provides dynamic predicate matchers that are more readable and
|
17
|
-
provide better failure output.
|
18
|
-
|
19
|
-
For any predicate method, RSpec gives you a corresponding matcher. Simply
|
20
|
-
prefix the method with "be_" and remove the question mark. Examples:
|
21
|
-
|
22
|
-
7.should_not be_zero # calls 7.zero?
|
23
|
-
[].should be_empty # calls [].empty?
|
24
|
-
x.should be_multiple_of(3) # calls x.multiple_of?(3)
|
25
|
-
|
26
|
-
Alternately, for a predicate method that begins with "has_" like Hash#has_key?,
|
27
|
-
RSpec allows you to use an alternate form since "be_has_key" makes no sense.
|
28
|
-
|
29
|
-
hash.should have_key(:foo) # calls hash.has_key?(:foo)
|
30
|
-
array.should_not have_odd_values # calls array.has_odd_values?
|
31
|
-
|
32
|
-
In either case, RSpec provides nice, clear error messages, such as:
|
33
|
-
|
34
|
-
expected zero? to return true, got false
|
35
|
-
|
36
|
-
Any arguments passed to the matcher will be passed on to the predicate method.
|
37
|
-
|
38
|
-
Scenario: should be_zero (based on Fixnum#zero?)
|
39
|
-
Given a file named "should_be_zero_spec.rb" with:
|
40
|
-
"""
|
41
|
-
describe 0 do
|
42
|
-
it { should be_zero }
|
43
|
-
end
|
44
|
-
|
45
|
-
describe 7 do
|
46
|
-
it { should be_zero } # deliberate failure
|
47
|
-
end
|
48
|
-
"""
|
49
|
-
When I run `rspec should_be_zero_spec.rb`
|
50
|
-
Then the output should contain "2 examples, 1 failure"
|
51
|
-
And the output should contain "expected zero? to return true, got false"
|
52
|
-
|
53
|
-
Scenario: should_not be_empty (based on Array#empty?)
|
54
|
-
Given a file named "should_not_be_empty_spec.rb" with:
|
55
|
-
"""
|
56
|
-
describe [1, 2, 3] do
|
57
|
-
it { should_not be_empty }
|
58
|
-
end
|
59
|
-
|
60
|
-
describe [] do
|
61
|
-
it { should_not be_empty } # deliberate failure
|
62
|
-
end
|
63
|
-
"""
|
64
|
-
When I run `rspec should_not_be_empty_spec.rb`
|
65
|
-
Then the output should contain "2 examples, 1 failure"
|
66
|
-
And the output should contain "expected empty? to return false, got true"
|
67
|
-
|
68
|
-
Scenario: should have_key (based on Hash#has_key?)
|
69
|
-
Given a file named "should_have_key_spec.rb" with:
|
70
|
-
"""
|
71
|
-
describe Hash do
|
72
|
-
subject { { :foo => 7 } }
|
73
|
-
it { should have_key(:foo) }
|
74
|
-
it { should have_key(:bar) } # deliberate failure
|
75
|
-
end
|
76
|
-
"""
|
77
|
-
When I run `rspec should_have_key_spec.rb`
|
78
|
-
Then the output should contain "2 examples, 1 failure"
|
79
|
-
And the output should contain "expected #has_key?(:bar) to return true, got false"
|
80
|
-
|
81
|
-
Scenario: should_not have_all_string_keys (based on custom #has_all_string_keys? method)
|
82
|
-
Given a file named "should_not_have_all_string_keys_spec.rb" with:
|
83
|
-
"""
|
84
|
-
class Hash
|
85
|
-
def has_all_string_keys?
|
86
|
-
keys.all? { |k| String === k }
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
describe Hash do
|
91
|
-
context 'with symbol keys' do
|
92
|
-
subject { { :foo => 7, :bar => 5 } }
|
93
|
-
it { should_not have_all_string_keys }
|
94
|
-
end
|
95
|
-
|
96
|
-
context 'with string keys' do
|
97
|
-
subject { { 'foo' => 7, 'bar' => 5 } }
|
98
|
-
it { should_not have_all_string_keys } # deliberate failure
|
99
|
-
end
|
100
|
-
end
|
101
|
-
"""
|
102
|
-
When I run `rspec should_not_have_all_string_keys_spec.rb`
|
103
|
-
Then the output should contain "2 examples, 1 failure"
|
104
|
-
And the output should contain "expected #has_all_string_keys? to return false, got true"
|
105
|
-
|
106
|
-
Scenario: matcher arguments are passed on to the predicate method
|
107
|
-
Given a file named "predicate_matcher_argument_spec.rb" with:
|
108
|
-
"""
|
109
|
-
class Fixnum
|
110
|
-
def multiple_of?(x)
|
111
|
-
(self % x).zero?
|
112
|
-
end
|
113
|
-
end
|
114
|
-
|
115
|
-
describe 12 do
|
116
|
-
it { should be_multiple_of(3) }
|
117
|
-
it { should_not be_multiple_of(7) }
|
118
|
-
|
119
|
-
# deliberate failures
|
120
|
-
it { should_not be_multiple_of(4) }
|
121
|
-
it { should be_multiple_of(5) }
|
122
|
-
end
|
123
|
-
"""
|
124
|
-
When I run `rspec predicate_matcher_argument_spec.rb`
|
125
|
-
Then the output should contain "4 examples, 2 failures"
|
126
|
-
And the output should contain "expected multiple_of?(4) to return false, got true"
|
127
|
-
And the output should contain "expected multiple_of?(5) to return true, got false"
|
128
|
-
|
@@ -1,78 +0,0 @@
|
|
1
|
-
Feature: respond_to matcher
|
2
|
-
|
3
|
-
Use the respond_to matcher to specify details of an object's interface. In
|
4
|
-
its most basic form:
|
5
|
-
|
6
|
-
obj.should respond_to(:foo) # pass if obj.respond_to?(:foo)
|
7
|
-
|
8
|
-
You can specify that an object responds to multiple messages in a single
|
9
|
-
statement with multiple arguments passed to the matcher:
|
10
|
-
|
11
|
-
obj.should respond_to(:foo, :bar) # passes if obj.respond_to?(:foo) && obj.respond_to?(:bar)
|
12
|
-
|
13
|
-
If the number of arguments accepted by the method is important to you,
|
14
|
-
you can specify that as well:
|
15
|
-
|
16
|
-
obj.should respond_to(:foo).with(1).argument
|
17
|
-
obj.should respond_to(:bar).with(2).arguments
|
18
|
-
|
19
|
-
Note that this matcher relies entirely upon #respond_to?. If an object
|
20
|
-
dynamically responds to a message via #method_missing, but does not indicate
|
21
|
-
this via #respond_to?, then this matcher will give you false results.
|
22
|
-
|
23
|
-
Scenario: basic usage
|
24
|
-
Given a file named "respond_to_matcher_spec.rb" with:
|
25
|
-
"""
|
26
|
-
describe "a string" do
|
27
|
-
it { should respond_to(:length) }
|
28
|
-
it { should respond_to(:hash, :class, :to_s) }
|
29
|
-
it { should_not respond_to(:to_model) }
|
30
|
-
it { should_not respond_to(:compact, :flatten) }
|
31
|
-
|
32
|
-
# deliberate failures
|
33
|
-
it { should respond_to(:to_model) }
|
34
|
-
it { should respond_to(:compact, :flatten) }
|
35
|
-
it { should_not respond_to(:length) }
|
36
|
-
it { should_not respond_to(:hash, :class, :to_s) }
|
37
|
-
|
38
|
-
# mixed examples--String responds to :length but not :flatten
|
39
|
-
# both specs should fail
|
40
|
-
it { should respond_to(:length, :flatten) }
|
41
|
-
it { should_not respond_to(:length, :flatten) }
|
42
|
-
end
|
43
|
-
"""
|
44
|
-
When I run `rspec respond_to_matcher_spec.rb`
|
45
|
-
Then the output should contain all of these:
|
46
|
-
| 10 examples, 6 failures |
|
47
|
-
| expected "a string" to respond to :to_model |
|
48
|
-
| expected "a string" to respond to :compact, :flatten |
|
49
|
-
| expected "a string" not to respond to :length |
|
50
|
-
| expected "a string" not to respond to :hash, :class, :to_s |
|
51
|
-
| expected "a string" to respond to :flatten |
|
52
|
-
| expected "a string" not to respond to :length |
|
53
|
-
|
54
|
-
Scenario: specify arguments
|
55
|
-
Given a file named "respond_to_matcher_argument_checking_spec.rb" with:
|
56
|
-
"""
|
57
|
-
describe 7 do
|
58
|
-
it { should respond_to(:zero?).with(0).arguments }
|
59
|
-
it { should_not respond_to(:zero?).with(1).argument }
|
60
|
-
|
61
|
-
it { should respond_to(:between?).with(2).arguments }
|
62
|
-
it { should_not respond_to(:between?).with(7).arguments }
|
63
|
-
|
64
|
-
# deliberate failures
|
65
|
-
it { should respond_to(:zero?).with(1).argument }
|
66
|
-
it { should_not respond_to(:zero?).with(0).arguments }
|
67
|
-
|
68
|
-
it { should respond_to(:between?).with(7).arguments }
|
69
|
-
it { should_not respond_to(:between?).with(2).arguments }
|
70
|
-
end
|
71
|
-
"""
|
72
|
-
When I run `rspec respond_to_matcher_argument_checking_spec.rb`
|
73
|
-
Then the output should contain all of these:
|
74
|
-
| 8 examples, 4 failures |
|
75
|
-
| expected 7 to respond to :zero? with 1 argument |
|
76
|
-
| expected 7 not to respond to :zero? with 0 arguments |
|
77
|
-
| expected 7 to respond to :between? with 7 arguments |
|
78
|
-
| expected 7 not to respond to :between? with 2 arguments |
|
@@ -1,31 +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
|
-
10.should satisfy { |v| v % 5 == 0 }
|
7
|
-
7.should_not satisfy { |v| v % 5 == 0 }
|
8
|
-
|
9
|
-
This flexibility comes at a cost, however: the failure message
|
10
|
-
("expected [actual] to satisfy block") is not very descriptive
|
11
|
-
or helpful. You will usually be better served by using one of
|
12
|
-
the other built-in matchers, or writing a custom matcher.
|
13
|
-
|
14
|
-
Scenario: basic usage
|
15
|
-
Given a file named "satisfy_matcher_spec.rb" with:
|
16
|
-
"""
|
17
|
-
describe 10 do
|
18
|
-
it { should satisfy { |v| v > 5 } }
|
19
|
-
it { should_not satisfy { |v| v > 15 } }
|
20
|
-
|
21
|
-
# deliberate failures
|
22
|
-
it { should_not satisfy { |v| v > 5 } }
|
23
|
-
it { should satisfy { |v| v > 15 } }
|
24
|
-
end
|
25
|
-
"""
|
26
|
-
When I run `rspec satisfy_matcher_spec.rb`
|
27
|
-
Then the output should contain all of these:
|
28
|
-
| 4 examples, 2 failures |
|
29
|
-
| expected 10 not to satisfy block |
|
30
|
-
| expected 10 to satisfy block |
|
31
|
-
|
@@ -1,46 +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
|
-
"this string".should start_with("this")
|
7
|
-
"this string".should_not start_with("that")
|
8
|
-
[0,1,2].should start_with(0, 1)
|
9
|
-
|
10
|
-
Scenario: with a string
|
11
|
-
Given a file named "example_spec.rb" with:
|
12
|
-
"""
|
13
|
-
describe "this string" do
|
14
|
-
it { should start_with "this" }
|
15
|
-
it { should_not start_with "that" }
|
16
|
-
|
17
|
-
# deliberate failures
|
18
|
-
it { should_not start_with "this" }
|
19
|
-
it { should start_with "that" }
|
20
|
-
end
|
21
|
-
"""
|
22
|
-
When I run `rspec example_spec.rb`
|
23
|
-
Then the output should contain all of these:
|
24
|
-
| 4 examples, 2 failures |
|
25
|
-
| expected "this string" not to start with "this" |
|
26
|
-
| expected "this string" to start with "that" |
|
27
|
-
|
28
|
-
Scenario: with an array
|
29
|
-
Given a file named "example_spec.rb" with:
|
30
|
-
"""
|
31
|
-
describe [0, 1, 2, 3, 4] do
|
32
|
-
it { should start_with 0 }
|
33
|
-
it { should start_with(0, 1)}
|
34
|
-
it { should_not start_with(2) }
|
35
|
-
it { should_not start_with(0, 1, 2, 3, 4, 5) }
|
36
|
-
|
37
|
-
# deliberate failures
|
38
|
-
it { should_not start_with 0 }
|
39
|
-
it { should start_with 3 }
|
40
|
-
end
|
41
|
-
"""
|
42
|
-
When I run `rspec example_spec.rb`
|
43
|
-
Then the output should contain all of these:
|
44
|
-
| 6 examples, 2 failures |
|
45
|
-
| expected [0, 1, 2, 3, 4] not to start with 0 |
|
46
|
-
| expected [0, 1, 2, 3, 4] to start with 3 |
|
@@ -1,85 +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
|
-
expect { throw :foo }.to throw_symbol
|
7
|
-
|
8
|
-
You'll often want to specify that a particular symbol is thrown:
|
9
|
-
|
10
|
-
expect { throw :foo }.to throw_symbol(:foo)
|
11
|
-
|
12
|
-
If you care about the additional argument given to throw, you can
|
13
|
-
specify that as well:
|
14
|
-
|
15
|
-
expect { throw :foo, 7 }.to throw_symbol(:foo, 7)
|
16
|
-
|
17
|
-
Scenario: basic usage
|
18
|
-
Given a file named "throw_symbol_matcher_spec.rb" with:
|
19
|
-
"""
|
20
|
-
describe "throw" do
|
21
|
-
specify { expect { throw :foo }.to throw_symbol }
|
22
|
-
specify { expect { throw :bar, 7 }.to throw_symbol }
|
23
|
-
specify { expect { 5 + 5 }.to_not throw_symbol }
|
24
|
-
|
25
|
-
# deliberate failures
|
26
|
-
specify { expect { throw :foo }.to_not throw_symbol }
|
27
|
-
specify { expect { throw :bar, 7 }.to_not throw_symbol }
|
28
|
-
specify { expect { 5 + 5 }.to throw_symbol }
|
29
|
-
end
|
30
|
-
"""
|
31
|
-
When I run `rspec throw_symbol_matcher_spec.rb`
|
32
|
-
Then the output should contain all of these:
|
33
|
-
| 6 examples, 3 failures |
|
34
|
-
| expected no Symbol to be thrown, got :foo |
|
35
|
-
| expected no Symbol to be thrown, got :bar |
|
36
|
-
| expected a Symbol to be thrown, got nothing |
|
37
|
-
|
38
|
-
Scenario: specify thrown symbol
|
39
|
-
Given a file named "throw_symbol_matcher_spec.rb" with:
|
40
|
-
"""
|
41
|
-
describe "throw symbol" do
|
42
|
-
specify { expect { throw :foo }.to throw_symbol(:foo) }
|
43
|
-
specify { expect { throw :foo, 7 }.to throw_symbol(:foo) }
|
44
|
-
specify { expect { 5 + 5 }.to_not throw_symbol(:foo) }
|
45
|
-
specify { expect { throw :bar }.to_not throw_symbol(:foo) }
|
46
|
-
|
47
|
-
# deliberate failures
|
48
|
-
specify { expect { throw :foo }.to_not throw_symbol(:foo) }
|
49
|
-
specify { expect { throw :foo, 7 }.to_not throw_symbol(:foo) }
|
50
|
-
specify { expect { 5 + 5 }.to throw_symbol(:foo) }
|
51
|
-
specify { expect { throw :bar }.to throw_symbol(:foo) }
|
52
|
-
end
|
53
|
-
"""
|
54
|
-
When I run `rspec throw_symbol_matcher_spec.rb`
|
55
|
-
Then the output should contain all of these:
|
56
|
-
| 8 examples, 4 failures |
|
57
|
-
| expected :foo not to be thrown, got :foo |
|
58
|
-
| expected :foo not to be thrown, got :foo with 7 |
|
59
|
-
| expected :foo to be thrown, got nothing |
|
60
|
-
| expected :foo to be thrown, got :bar |
|
61
|
-
|
62
|
-
Scenario: specify thrown symbol and argument
|
63
|
-
Given a file named "throw_symbol_argument_matcher_spec.rb" with:
|
64
|
-
"""
|
65
|
-
describe "throw symbol with argument" do
|
66
|
-
specify { expect { throw :foo, 7 }.to throw_symbol(:foo, 7) }
|
67
|
-
specify { expect { throw :foo, 8 }.to_not throw_symbol(:foo, 7) }
|
68
|
-
specify { expect { throw :bar, 7 }.to_not throw_symbol(:foo, 7) }
|
69
|
-
specify { expect { throw :foo }.to_not throw_symbol(:foo, 7) }
|
70
|
-
|
71
|
-
# deliberate failures
|
72
|
-
specify { expect { throw :foo, 7 }.to_not throw_symbol(:foo, 7) }
|
73
|
-
specify { expect { throw :foo, 8 }.to throw_symbol(:foo, 7) }
|
74
|
-
specify { expect { throw :bar, 7 }.to throw_symbol(:foo, 7) }
|
75
|
-
specify { expect { throw :foo }.to throw_symbol(:foo, 7) }
|
76
|
-
end
|
77
|
-
"""
|
78
|
-
When I run `rspec throw_symbol_argument_matcher_spec.rb`
|
79
|
-
Then the output should contain all of these:
|
80
|
-
| 8 examples, 4 failures |
|
81
|
-
| expected :foo with 7 not to be thrown, got :foo with 7 |
|
82
|
-
| expected :foo with 7 to be thrown, got :foo with 8 |
|
83
|
-
| expected :foo with 7 to be thrown, got :bar |
|
84
|
-
| expected :foo with 7 to be thrown, got :foo with no argument |
|
85
|
-
|
@@ -1,114 +0,0 @@
|
|
1
|
-
Feature: specify types of objects
|
2
|
-
|
3
|
-
rspec-expectations includes two matchers to specify types of objects:
|
4
|
-
|
5
|
-
* obj.should 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
|
-
* obj.should 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
|
-
* obj.should be_a_kind_of(type) # same as obj.should be_kind_of(type)
|
14
|
-
* obj.should be_a(type) # same as obj.should be_kind_of(type)
|
15
|
-
* obj.should be_an(type) # same as obj.should be_kind_of(type)
|
16
|
-
* obj.should be_an_instance_of(type) # same as obj.should be_instance_of(type)
|
17
|
-
|
18
|
-
Scenario: be_(a_)kind_of matcher
|
19
|
-
Given a file named "be_kind_of_matcher_spec.rb" with:
|
20
|
-
"""
|
21
|
-
module MyModule; end
|
22
|
-
|
23
|
-
class Fixnum
|
24
|
-
include MyModule
|
25
|
-
end
|
26
|
-
|
27
|
-
describe 17 do
|
28
|
-
# the actual class
|
29
|
-
it { should be_kind_of(Fixnum) }
|
30
|
-
it { should be_a_kind_of(Fixnum) }
|
31
|
-
it { should be_a(Fixnum) }
|
32
|
-
|
33
|
-
# the superclass
|
34
|
-
it { should be_kind_of(Integer) }
|
35
|
-
it { should be_a_kind_of(Integer) }
|
36
|
-
it { should be_an(Integer) }
|
37
|
-
|
38
|
-
# an included module
|
39
|
-
it { should be_kind_of(MyModule) }
|
40
|
-
it { should be_a_kind_of(MyModule) }
|
41
|
-
it { should be_a(MyModule) }
|
42
|
-
|
43
|
-
# negative passing case
|
44
|
-
it { should_not be_kind_of(String) }
|
45
|
-
it { should_not be_a_kind_of(String) }
|
46
|
-
it { should_not be_a(String) }
|
47
|
-
|
48
|
-
# deliberate failures
|
49
|
-
it { should_not be_kind_of(Fixnum) }
|
50
|
-
it { should_not be_a_kind_of(Fixnum) }
|
51
|
-
it { should_not be_a(Fixnum) }
|
52
|
-
it { should_not be_kind_of(Integer) }
|
53
|
-
it { should_not be_a_kind_of(Integer) }
|
54
|
-
it { should_not be_an(Integer) }
|
55
|
-
it { should_not be_kind_of(MyModule) }
|
56
|
-
it { should_not be_a_kind_of(MyModule) }
|
57
|
-
it { should_not be_a(MyModule) }
|
58
|
-
it { should be_kind_of(String) }
|
59
|
-
it { should be_a_kind_of(String) }
|
60
|
-
it { should be_a(String) }
|
61
|
-
end
|
62
|
-
"""
|
63
|
-
When I run `rspec be_kind_of_matcher_spec.rb`
|
64
|
-
Then the output should contain all of these:
|
65
|
-
| 24 examples, 12 failures |
|
66
|
-
| expected 17 not to be a kind of Fixnum |
|
67
|
-
| expected 17 not to be a kind of Integer |
|
68
|
-
| expected 17 not to be a kind of MyModule |
|
69
|
-
| expected 17 to be a kind of String |
|
70
|
-
|
71
|
-
Scenario: be_(an_)instance_of matcher
|
72
|
-
Given a file named "be_instance_of_matcher_spec.rb" with:
|
73
|
-
"""
|
74
|
-
module MyModule; end
|
75
|
-
|
76
|
-
class Fixnum
|
77
|
-
include MyModule
|
78
|
-
end
|
79
|
-
|
80
|
-
describe 17 do
|
81
|
-
# the actual class
|
82
|
-
it { should be_instance_of(Fixnum) }
|
83
|
-
it { should be_an_instance_of(Fixnum) }
|
84
|
-
|
85
|
-
# the superclass
|
86
|
-
it { should_not be_instance_of(Integer) }
|
87
|
-
it { should_not be_an_instance_of(Integer) }
|
88
|
-
|
89
|
-
# an included module
|
90
|
-
it { should_not be_instance_of(MyModule) }
|
91
|
-
it { should_not be_an_instance_of(MyModule) }
|
92
|
-
|
93
|
-
# another class with no relation to the subject's hierarchy
|
94
|
-
it { should_not be_instance_of(String) }
|
95
|
-
it { should_not be_an_instance_of(String) }
|
96
|
-
|
97
|
-
# deliberate failures
|
98
|
-
it { should_not be_instance_of(Fixnum) }
|
99
|
-
it { should_not be_an_instance_of(Fixnum) }
|
100
|
-
it { should be_instance_of(Integer) }
|
101
|
-
it { should be_an_instance_of(Integer) }
|
102
|
-
it { should be_instance_of(MyModule) }
|
103
|
-
it { should be_an_instance_of(MyModule) }
|
104
|
-
it { should be_instance_of(String) }
|
105
|
-
it { should be_an_instance_of(String) }
|
106
|
-
end
|
107
|
-
"""
|
108
|
-
When I run `rspec be_instance_of_matcher_spec.rb`
|
109
|
-
Then the output should contain all of these:
|
110
|
-
| 16 examples, 8 failures |
|
111
|
-
| expected 17 not to be an instance of Fixnum |
|
112
|
-
| expected 17 to be an instance of Integer |
|
113
|
-
| expected 17 to be an instance of MyModule |
|
114
|
-
| expected 17 to be an instance of String |
|
@@ -1,146 +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
|
-
"""
|
28
|
-
class MyClass
|
29
|
-
def self.yield_once_with(*args)
|
30
|
-
yield *args
|
31
|
-
end
|
32
|
-
|
33
|
-
def self.raw_yield
|
34
|
-
yield
|
35
|
-
end
|
36
|
-
|
37
|
-
def self.dont_yield
|
38
|
-
end
|
39
|
-
end
|
40
|
-
"""
|
41
|
-
|
42
|
-
Scenario: yield_control matcher
|
43
|
-
Given a file named "yield_control_spec.rb" with:
|
44
|
-
"""
|
45
|
-
require './my_class'
|
46
|
-
|
47
|
-
describe "yield_control matcher" do
|
48
|
-
specify { expect { |b| MyClass.yield_once_with(1, &b) }.to yield_control }
|
49
|
-
specify { expect { |b| MyClass.dont_yield(&b) }.not_to yield_control }
|
50
|
-
|
51
|
-
# deliberate failures
|
52
|
-
specify { expect { |b| MyClass.yield_once_with(1, &b) }.not_to yield_control }
|
53
|
-
specify { expect { |b| MyClass.dont_yield(&b) }.to yield_control }
|
54
|
-
end
|
55
|
-
"""
|
56
|
-
When I run `rspec yield_control_spec.rb`
|
57
|
-
Then the output should contain all of these:
|
58
|
-
| 4 examples, 2 failures |
|
59
|
-
| expected given block to yield control |
|
60
|
-
| expected given block not to yield control |
|
61
|
-
|
62
|
-
Scenario: yield_with_args matcher
|
63
|
-
Given a file named "yield_with_args_spec.rb" with:
|
64
|
-
"""
|
65
|
-
require './my_class'
|
66
|
-
|
67
|
-
describe "yield_with_args matcher" do
|
68
|
-
specify { expect { |b| MyClass.yield_once_with("foo", &b) }.to yield_with_args }
|
69
|
-
specify { expect { |b| MyClass.yield_once_with("foo", &b) }.to yield_with_args("foo") }
|
70
|
-
specify { expect { |b| MyClass.yield_once_with("foo", &b) }.to yield_with_args(String) }
|
71
|
-
specify { expect { |b| MyClass.yield_once_with("foo", &b) }.to yield_with_args(/oo/) }
|
72
|
-
|
73
|
-
specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.to yield_with_args("foo", "bar") }
|
74
|
-
specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.to yield_with_args(String, String) }
|
75
|
-
specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.to yield_with_args(/fo/, /ar/) }
|
76
|
-
|
77
|
-
specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.not_to yield_with_args(17, "baz") }
|
78
|
-
|
79
|
-
# deliberate failures
|
80
|
-
specify { expect { |b| MyClass.yield_once_with("foo", &b) }.not_to yield_with_args }
|
81
|
-
specify { expect { |b| MyClass.yield_once_with("foo", &b) }.not_to yield_with_args("foo") }
|
82
|
-
specify { expect { |b| MyClass.yield_once_with("foo", &b) }.not_to yield_with_args(String) }
|
83
|
-
specify { expect { |b| MyClass.yield_once_with("foo", &b) }.not_to yield_with_args(/oo/) }
|
84
|
-
specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.not_to yield_with_args("foo", "bar") }
|
85
|
-
specify { expect { |b| MyClass.yield_once_with("foo", "bar", &b) }.to yield_with_args(17, "baz") }
|
86
|
-
end
|
87
|
-
"""
|
88
|
-
When I run `rspec yield_with_args_spec.rb`
|
89
|
-
Then the output should contain all of these:
|
90
|
-
| 14 examples, 6 failures |
|
91
|
-
| expected given block not to yield with arguments, but did |
|
92
|
-
| expected given block not to yield with arguments, but yielded with expected arguments |
|
93
|
-
| expected given block to yield with arguments, but yielded with unexpected arguments |
|
94
|
-
|
95
|
-
Scenario: yield_with_no_args matcher
|
96
|
-
Given a file named "yield_with_no_args_spec.rb" with:
|
97
|
-
"""
|
98
|
-
require './my_class'
|
99
|
-
|
100
|
-
describe "yield_with_no_args matcher" do
|
101
|
-
specify { expect { |b| MyClass.raw_yield(&b) }.to yield_with_no_args }
|
102
|
-
specify { expect { |b| MyClass.dont_yield(&b) }.not_to yield_with_no_args }
|
103
|
-
specify { expect { |b| MyClass.yield_once_with("a", &b) }.not_to yield_with_no_args }
|
104
|
-
|
105
|
-
# deliberate failures
|
106
|
-
specify { expect { |b| MyClass.raw_yield(&b) }.not_to yield_with_no_args }
|
107
|
-
specify { expect { |b| MyClass.dont_yield(&b) }.to yield_with_no_args }
|
108
|
-
specify { expect { |b| MyClass.yield_once_with("a", &b) }.to yield_with_no_args }
|
109
|
-
end
|
110
|
-
"""
|
111
|
-
When I run `rspec yield_with_no_args_spec.rb`
|
112
|
-
Then the output should contain all of these:
|
113
|
-
| 6 examples, 3 failures |
|
114
|
-
| expected given block not to yield with no arguments, but did |
|
115
|
-
| expected given block to yield with no arguments, but did not yield |
|
116
|
-
| expected given block to yield with no arguments, but yielded with arguments: ["a"] |
|
117
|
-
|
118
|
-
Scenario: yield_successive_args matcher
|
119
|
-
Given a file named "yield_successive_args_spec.rb" with:
|
120
|
-
"""
|
121
|
-
def array
|
122
|
-
[1, 2, 3]
|
123
|
-
end
|
124
|
-
|
125
|
-
def array_of_tuples
|
126
|
-
[[:a, :b], [:c, :d]]
|
127
|
-
end
|
128
|
-
|
129
|
-
describe "yield_successive_args matcher" do
|
130
|
-
specify { expect { |b| array.each(&b) }.to yield_successive_args(1, 2, 3) }
|
131
|
-
specify { expect { |b| array_of_tuples.each(&b) }.to yield_successive_args([:a, :b], [:c, :d]) }
|
132
|
-
specify { expect { |b| array.each(&b) }.to yield_successive_args(Fixnum, Fixnum, Fixnum) }
|
133
|
-
specify { expect { |b| array.each(&b) }.not_to yield_successive_args(1, 2) }
|
134
|
-
|
135
|
-
# deliberate failures
|
136
|
-
specify { expect { |b| array.each(&b) }.not_to yield_successive_args(1, 2, 3) }
|
137
|
-
specify { expect { |b| array_of_tuples.each(&b) }.not_to yield_successive_args([:a, :b], [:c, :d]) }
|
138
|
-
specify { expect { |b| array.each(&b) }.not_to yield_successive_args(Fixnum, Fixnum, Fixnum) }
|
139
|
-
specify { expect { |b| array.each(&b) }.to yield_successive_args(1, 2) }
|
140
|
-
end
|
141
|
-
"""
|
142
|
-
When I run `rspec yield_successive_args_spec.rb`
|
143
|
-
Then the output should contain all of these:
|
144
|
-
| 8 examples, 4 failures |
|
145
|
-
| expected given block not to yield successively with arguments, but yielded with expected arguments |
|
146
|
-
| expected given block to yield successively with arguments, but yielded with unexpected arguments |
|
@@ -1,53 +0,0 @@
|
|
1
|
-
Feature: access running example
|
2
|
-
|
3
|
-
In order to take advantage of services that are available
|
4
|
-
in my examples when I'm writing matchers
|
5
|
-
As a spec author
|
6
|
-
I want to call methods on the running example
|
7
|
-
|
8
|
-
If the method exists in the context of the example, it gets
|
9
|
-
called. If not, a NoMethodError is raised on the Matcher itself
|
10
|
-
(not the example).
|
11
|
-
|
12
|
-
Scenario: call method defined on example from matcher
|
13
|
-
Given a file named "example_spec.rb" with:
|
14
|
-
"""
|
15
|
-
RSpec::Matchers.define :bar do
|
16
|
-
match do |_|
|
17
|
-
foo == "foo"
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
describe "something" do
|
22
|
-
def foo
|
23
|
-
"foo"
|
24
|
-
end
|
25
|
-
|
26
|
-
it "does something" do
|
27
|
-
"foo".should bar
|
28
|
-
end
|
29
|
-
end
|
30
|
-
"""
|
31
|
-
When I run `rspec ./example_spec.rb`
|
32
|
-
Then the output should contain "1 example, 0 failures"
|
33
|
-
|
34
|
-
Scenario: call method _not_ defined on example from matcher
|
35
|
-
Given a file named "example_spec.rb" with:
|
36
|
-
"""
|
37
|
-
RSpec::Matchers.define :bar do
|
38
|
-
match do |_|
|
39
|
-
foo == "foo"
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
describe "something" do
|
44
|
-
it "does something" do
|
45
|
-
"foo".should bar
|
46
|
-
end
|
47
|
-
end
|
48
|
-
"""
|
49
|
-
When I run `rspec ./example_spec.rb`
|
50
|
-
Then the output should contain "1 example, 1 failure"
|
51
|
-
And the output should contain "undefined local variable"
|
52
|
-
And the output should contain "RSpec::Matchers::DSL::Matcher"
|
53
|
-
And the output should not contain "ExampleGroup"
|