rspec-expectations 2.0.0.beta.22 → 2.6.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.
- data/.gitignore +1 -1
- data/.travis.yml +7 -0
- data/Gemfile +35 -13
- data/Guardfile +5 -0
- data/License.txt +1 -1
- data/README.md +43 -0
- data/Rakefile +51 -9
- data/cucumber.yml +9 -2
- data/features/.nav +29 -0
- data/features/Changelog.md +101 -0
- data/features/README.markdown +45 -8
- data/features/built_in_matchers/README.md +71 -0
- data/features/built_in_matchers/be.feature +135 -0
- data/features/built_in_matchers/be_within.feature +43 -0
- data/features/built_in_matchers/cover.feature +45 -0
- data/features/{matchers → built_in_matchers}/equality.feature +16 -13
- data/features/built_in_matchers/exist.feature +43 -0
- data/features/built_in_matchers/expect_change.feature +59 -0
- data/features/built_in_matchers/expect_error.feature +105 -0
- data/features/built_in_matchers/have.feature +103 -0
- data/features/built_in_matchers/include.feature +121 -0
- data/features/built_in_matchers/match.feature +50 -0
- data/features/built_in_matchers/operators.feature +221 -0
- data/features/built_in_matchers/predicates.feature +128 -0
- data/features/built_in_matchers/respond_to.feature +78 -0
- data/features/built_in_matchers/satisfy.feature +31 -0
- data/features/built_in_matchers/throw_symbol.feature +85 -0
- data/features/built_in_matchers/types.feature +114 -0
- data/features/{matchers → custom_matchers}/access_running_example.feature +2 -2
- data/features/{matchers → custom_matchers}/define_diffable_matcher.feature +1 -1
- data/features/{matchers → custom_matchers}/define_matcher.feature +91 -9
- data/features/{matchers → custom_matchers}/define_matcher_outside_rspec.feature +1 -1
- data/features/{matchers → custom_matchers}/define_matcher_with_fluent_interface.feature +8 -11
- data/features/customized_message.feature +22 -0
- data/features/{expectations/diffing.feature → diffing.feature} +16 -16
- data/features/{expectations/implicit_docstrings.feature → implicit_docstrings.feature} +4 -4
- data/features/step_definitions/additional_cli_steps.rb +22 -0
- data/features/support/env.rb +5 -1
- data/features/test_frameworks/test_unit.feature +46 -0
- data/lib/rspec/expectations/backward_compatibility.rb +22 -1
- data/lib/rspec/expectations/deprecation.rb +36 -0
- data/lib/rspec/expectations/extensions.rb +0 -1
- data/lib/rspec/expectations/version.rb +1 -1
- data/lib/rspec/expectations.rb +2 -1
- data/lib/rspec/matchers/be.rb +4 -4
- data/lib/rspec/matchers/be_close.rb +2 -17
- data/lib/rspec/matchers/be_within.rb +40 -0
- data/lib/rspec/matchers/block_aliases.rb +19 -0
- data/lib/rspec/matchers/change.rb +74 -49
- data/lib/rspec/matchers/cover.rb +35 -0
- data/lib/rspec/matchers/exist.rb +13 -3
- data/lib/rspec/matchers/has.rb +15 -11
- data/lib/rspec/matchers/have.rb +5 -5
- data/lib/rspec/matchers/include.rb +14 -5
- data/lib/rspec/matchers/matcher.rb +39 -13
- data/lib/rspec/matchers/method_missing.rb +6 -3
- data/lib/rspec/matchers/operator_matcher.rb +12 -3
- data/lib/rspec/matchers/respond_to.rb +23 -9
- data/lib/rspec/matchers/satisfy.rb +4 -0
- data/lib/rspec/matchers/throw_symbol.rb +43 -26
- data/lib/rspec/matchers.rb +15 -3
- data/rspec-expectations.gemspec +2 -6
- data/spec/rspec/matchers/be_close_spec.rb +11 -39
- data/spec/rspec/matchers/be_spec.rb +8 -8
- data/spec/rspec/matchers/be_within_spec.rb +64 -0
- data/spec/rspec/matchers/change_spec.rb +88 -9
- data/spec/rspec/matchers/cover_spec.rb +65 -0
- data/spec/rspec/matchers/description_generation_spec.rb +41 -25
- data/spec/rspec/matchers/exist_spec.rb +90 -51
- data/spec/rspec/matchers/has_spec.rb +2 -2
- data/spec/rspec/matchers/have_spec.rb +291 -291
- data/spec/rspec/matchers/include_spec.rb +318 -65
- data/spec/rspec/matchers/matcher_spec.rb +91 -0
- data/spec/rspec/matchers/matchers_spec.rb +29 -0
- data/spec/rspec/matchers/method_missing_spec.rb +23 -0
- data/spec/rspec/matchers/operator_matcher_spec.rb +36 -10
- data/spec/rspec/matchers/respond_to_spec.rb +177 -1
- data/spec/rspec/matchers/satisfy_spec.rb +4 -0
- data/spec/rspec/matchers/throw_symbol_spec.rb +27 -10
- data/spec/spec_helper.rb +10 -35
- data/spec/support/classes.rb +1 -1
- data/spec/support/matchers.rb +22 -0
- data/spec/support/ruby_version.rb +10 -0
- metadata +95 -114
- data/History.md +0 -16
- data/README.markdown +0 -23
- data/features/expectations/attribute_of_subject.feature +0 -19
- data/features/expectations/customized_message.feature +0 -54
- data/features/matchers/expect_change.feature +0 -65
- data/features/matchers/expect_error.feature +0 -44
- data/lib/rspec/expectations/extensions/rspec/core/example_group.rb +0 -19
- data/spec/suite.rb +0 -1
- /data/{Upgrade.markdown → features/Upgrade.md} +0 -0
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
(1..10).should cover(5)
|
|
9
|
+
(1..10).should cover(4, 6)
|
|
10
|
+
(1..10).should_not cover(11)
|
|
11
|
+
|
|
12
|
+
Scenario: range usage
|
|
13
|
+
Given a file named "range_cover_matcher_spec.rb" with:
|
|
14
|
+
"""
|
|
15
|
+
describe (1..10) do
|
|
16
|
+
it { should cover(4) }
|
|
17
|
+
it { should cover(6) }
|
|
18
|
+
it { should cover(8) }
|
|
19
|
+
it { should cover(4, 6) }
|
|
20
|
+
it { should cover(4, 6, 8) }
|
|
21
|
+
it { should_not cover(11) }
|
|
22
|
+
it { should_not cover(11, 12) }
|
|
23
|
+
|
|
24
|
+
# deliberate failures
|
|
25
|
+
it { should cover(11) }
|
|
26
|
+
it { should_not cover(4) }
|
|
27
|
+
it { should_not cover(6) }
|
|
28
|
+
it { should_not cover(8) }
|
|
29
|
+
it { should_not cover(4, 6, 8) }
|
|
30
|
+
|
|
31
|
+
# both of these should fail since it covers 1 but not 9
|
|
32
|
+
it { should cover(5, 11) }
|
|
33
|
+
it { should_not cover(5, 11) }
|
|
34
|
+
end
|
|
35
|
+
"""
|
|
36
|
+
When I run `rspec range_cover_matcher_spec.rb`
|
|
37
|
+
Then the output should contain all of these:
|
|
38
|
+
| 14 examples, 7 failures |
|
|
39
|
+
| expected 1..10 to cover 11 |
|
|
40
|
+
| expected 1..10 not to cover 4 |
|
|
41
|
+
| expected 1..10 not to cover 6 |
|
|
42
|
+
| expected 1..10 not to cover 8 |
|
|
43
|
+
| expected 1..10 not to cover 4, 6, and 8 |
|
|
44
|
+
| expected 1..10 to cover 5 and 11 |
|
|
45
|
+
| expected 1..10 not to cover 5 and 11 |
|
|
@@ -2,9 +2,9 @@ Feature: equality matchers
|
|
|
2
2
|
|
|
3
3
|
Ruby exposes several different methods for handling equality:
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
8
|
|
|
9
9
|
Note that these descriptions are guidelines but are not forced by the
|
|
10
10
|
language. Any object can implement any of these methods with its own
|
|
@@ -12,14 +12,17 @@ Feature: equality matchers
|
|
|
12
12
|
|
|
13
13
|
rspec-expectations ships with matchers that align with each of these methods:
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
a.should equal(b) # passes if a.equal?(b)
|
|
16
|
+
a.should eql(b) # passes if a.eql?(b)
|
|
17
|
+
a.should == b # passes if a == b
|
|
18
18
|
|
|
19
19
|
It also ships with two matchers that have more of a DSL feel to them:
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
|
|
21
|
+
a.should be(b) # passes if a.equal?(b)
|
|
22
|
+
a.should eq(b) # passes if a == b
|
|
23
|
+
|
|
24
|
+
These are a useful pair if you wish to avoid the warning that Ruby emits on
|
|
25
|
+
`a.should == b`
|
|
23
26
|
|
|
24
27
|
Scenario: compare using eq (==)
|
|
25
28
|
Given a file named "compare_using_eq.rb" with:
|
|
@@ -42,7 +45,7 @@ Feature: equality matchers
|
|
|
42
45
|
end
|
|
43
46
|
end
|
|
44
47
|
"""
|
|
45
|
-
When I run
|
|
48
|
+
When I run `rspec compare_using_eq.rb`
|
|
46
49
|
Then the output should contain "3 examples, 0 failures"
|
|
47
50
|
|
|
48
51
|
Scenario: compare using ==
|
|
@@ -66,7 +69,7 @@ Feature: equality matchers
|
|
|
66
69
|
end
|
|
67
70
|
end
|
|
68
71
|
"""
|
|
69
|
-
When I run
|
|
72
|
+
When I run `rspec compare_using_==.rb`
|
|
70
73
|
Then the output should contain "3 examples, 0 failures"
|
|
71
74
|
|
|
72
75
|
Scenario: compare using eql (eql?)
|
|
@@ -89,7 +92,7 @@ Feature: equality matchers
|
|
|
89
92
|
|
|
90
93
|
end
|
|
91
94
|
"""
|
|
92
|
-
When I run
|
|
95
|
+
When I run `rspec compare_using_eql.rb`
|
|
93
96
|
Then the output should contain "3 examples, 0 failures"
|
|
94
97
|
|
|
95
98
|
Scenario: compare using equal (equal?)
|
|
@@ -113,7 +116,7 @@ Feature: equality matchers
|
|
|
113
116
|
|
|
114
117
|
end
|
|
115
118
|
"""
|
|
116
|
-
When I run
|
|
119
|
+
When I run `rspec compare_using_equal.rb`
|
|
117
120
|
Then the output should contain "3 examples, 0 failures"
|
|
118
121
|
|
|
119
122
|
Scenario: compare using be (equal?)
|
|
@@ -137,6 +140,6 @@ Feature: equality matchers
|
|
|
137
140
|
|
|
138
141
|
end
|
|
139
142
|
"""
|
|
140
|
-
When I run
|
|
143
|
+
When I run `rspec compare_using_be.rb`
|
|
141
144
|
Then the output should contain "3 examples, 0 failures"
|
|
142
145
|
|
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
obj.should exist # passes if obj.exist? or obj.exists?
|
|
7
|
+
|
|
8
|
+
Scenario: basic usage
|
|
9
|
+
Given a file named "exist_matcher_spec.rb" with:
|
|
10
|
+
"""
|
|
11
|
+
class Planet
|
|
12
|
+
attr_reader :name
|
|
13
|
+
|
|
14
|
+
def initialize(name)
|
|
15
|
+
@name = name
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def inspect
|
|
19
|
+
"<Planet: #{name}>"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def exist? # also works with exists?
|
|
23
|
+
%w[Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune].include?(name)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
describe "Earth" do
|
|
28
|
+
let(:earth) { Planet.new("Earth") }
|
|
29
|
+
specify { earth.should exist }
|
|
30
|
+
specify { earth.should_not exist } # deliberate failure
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
describe "Tatooine" do
|
|
34
|
+
let(:tatooine) { Planet.new("Tatooine") }
|
|
35
|
+
it { tatooine.should exist } # deliberate failure
|
|
36
|
+
it { tatooine.should_not exist }
|
|
37
|
+
end
|
|
38
|
+
"""
|
|
39
|
+
When I run `rspec exist_matcher_spec.rb`
|
|
40
|
+
Then the output should contain all of these:
|
|
41
|
+
| 4 examples, 2 failures |
|
|
42
|
+
| expected <Planet: Earth> not to exist |
|
|
43
|
+
| expected <Planet: Tatooine> to exist |
|
|
@@ -0,0 +1,59 @@
|
|
|
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
|
+
"""
|
|
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
|
+
"""
|
|
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
|
+
"""
|
|
45
|
+
require "counter"
|
|
46
|
+
|
|
47
|
+
describe Counter, "#increment" do
|
|
48
|
+
it "should not increment the count by 1 (using to_not)" do
|
|
49
|
+
expect { Counter.increment }.to_not change{Counter.count}
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it "should not increment the count by 1 (using not_to)" do
|
|
53
|
+
expect { Counter.increment }.not_to 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"
|
|
@@ -0,0 +1,105 @@
|
|
|
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
|
+
expect { raise StandardError }.to raise_error
|
|
7
|
+
|
|
8
|
+
You can use `raise_exception` instead if you prefer that wording:
|
|
9
|
+
|
|
10
|
+
expect { 3 / 0 }.to raise_exception
|
|
11
|
+
|
|
12
|
+
`raise_error` and `raise_exception` are functionally interchangeable, so use
|
|
13
|
+
the one that makes the most sense to you in any given context.
|
|
14
|
+
|
|
15
|
+
In addition to the basic form, above, there are a number of ways to specify
|
|
16
|
+
details of an error/exception:
|
|
17
|
+
|
|
18
|
+
Scenario: expect any error
|
|
19
|
+
Given a file named "expect_error_spec.rb" with:
|
|
20
|
+
"""
|
|
21
|
+
describe "calling a method that does not exist" do
|
|
22
|
+
it "raises" do
|
|
23
|
+
expect { Object.new.foo }.to raise_error
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
"""
|
|
27
|
+
When I run `rspec expect_error_spec.rb`
|
|
28
|
+
Then the example should pass
|
|
29
|
+
|
|
30
|
+
Scenario: expect specific error
|
|
31
|
+
Given a file named "expect_error_spec.rb" with:
|
|
32
|
+
"""
|
|
33
|
+
describe "calling a method that does not exist" do
|
|
34
|
+
it "raises" do
|
|
35
|
+
expect { Object.new.foo }.to raise_error(NameError)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
"""
|
|
39
|
+
When I run `rspec expect_error_spec.rb`
|
|
40
|
+
Then the example should pass
|
|
41
|
+
|
|
42
|
+
Scenario: expect specific error message using a string
|
|
43
|
+
Given a file named "expect_error_with_message.rb" with:
|
|
44
|
+
"""
|
|
45
|
+
describe "matching error message with string" do
|
|
46
|
+
it "matches the error message" do
|
|
47
|
+
expect { raise StandardError, 'this message exactly'}.
|
|
48
|
+
to raise_error(StandardError, 'this message exactly')
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
"""
|
|
52
|
+
When I run `rspec expect_error_with_message.rb`
|
|
53
|
+
Then the example should pass
|
|
54
|
+
|
|
55
|
+
Scenario: expect specific error message using a regular expression
|
|
56
|
+
Given a file named "expect_error_with_regex.rb" with:
|
|
57
|
+
"""
|
|
58
|
+
describe "matching error message with regex" do
|
|
59
|
+
it "matches the error message" do
|
|
60
|
+
expect { raise StandardError, "my message" }.
|
|
61
|
+
to raise_error(StandardError, /my mess/)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
"""
|
|
65
|
+
When I run `rspec expect_error_with_regex.rb`
|
|
66
|
+
Then the example should pass
|
|
67
|
+
|
|
68
|
+
Scenario: set expectations on error object passed to block
|
|
69
|
+
Given a file named "expect_error_with_block_spec.rb" with:
|
|
70
|
+
"""
|
|
71
|
+
describe "#foo" do
|
|
72
|
+
it "raises NameError" do
|
|
73
|
+
expect { Object.new.foo }.to raise_error { |error|
|
|
74
|
+
error.should be_a(NameError)
|
|
75
|
+
}
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
"""
|
|
79
|
+
When I run `rspec expect_error_with_block_spec.rb`
|
|
80
|
+
Then the example should pass
|
|
81
|
+
|
|
82
|
+
Scenario: expect no error at all
|
|
83
|
+
Given a file named "expect_no_error_spec.rb" with:
|
|
84
|
+
"""
|
|
85
|
+
describe "#to_s" do
|
|
86
|
+
it "does not raise" do
|
|
87
|
+
expect { Object.new.to_s }.to_not raise_error
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
"""
|
|
91
|
+
When I run `rspec expect_no_error_spec.rb`
|
|
92
|
+
Then the example should pass
|
|
93
|
+
|
|
94
|
+
Scenario: expect no occurence of a specific error
|
|
95
|
+
Given a file named "expect_no_error_spec.rb" with:
|
|
96
|
+
"""
|
|
97
|
+
describe Object, "#public_instance_methods" do
|
|
98
|
+
it "does not raise" do
|
|
99
|
+
expect { Object.public_instance_methods }.
|
|
100
|
+
to_not raise_error(NameError)
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
"""
|
|
104
|
+
When I run `rspec expect_no_error_spec.rb`
|
|
105
|
+
Then the example should pass
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
Feature: have(n).items matcher
|
|
2
|
+
|
|
3
|
+
RSpec provides several matchers that make it easy to set expectations about the
|
|
4
|
+
size of a collection. There are three basic forms:
|
|
5
|
+
|
|
6
|
+
* collection.should have(x).items
|
|
7
|
+
* collection.should have_at_least(x).items
|
|
8
|
+
* collection.should have_at_most(x).items
|
|
9
|
+
|
|
10
|
+
In addition, #have_exactly is provided as an alias to #have.
|
|
11
|
+
|
|
12
|
+
These work on any collection-like object--the object just needs to respond to #size
|
|
13
|
+
or #length (or both). When the matcher is called directly on a collection object,
|
|
14
|
+
the #items call is pure syntactic sugar. You can use anything you want here. These
|
|
15
|
+
are equivalent:
|
|
16
|
+
|
|
17
|
+
* collection.should have(x).items
|
|
18
|
+
* collection.should have(x).things
|
|
19
|
+
|
|
20
|
+
You can also use this matcher on a non-collection object that returns a collection
|
|
21
|
+
from one of its methods. For example, Dir#entries returns an array, so you could
|
|
22
|
+
set an expectation using the following:
|
|
23
|
+
|
|
24
|
+
Dir.new("my/directory").should have(7).entries
|
|
25
|
+
|
|
26
|
+
Scenario: have(x).items on a collection
|
|
27
|
+
Given a file named "have_items_spec.rb" with:
|
|
28
|
+
"""
|
|
29
|
+
describe [1, 2, 3] do
|
|
30
|
+
it { should have(3).items }
|
|
31
|
+
it { should_not have(2).items }
|
|
32
|
+
it { should_not have(4).items }
|
|
33
|
+
|
|
34
|
+
it { should have_exactly(3).items }
|
|
35
|
+
it { should_not have_exactly(2).items }
|
|
36
|
+
it { should_not have_exactly(4).items }
|
|
37
|
+
|
|
38
|
+
it { should have_at_least(2).items }
|
|
39
|
+
it { should have_at_most(4).items }
|
|
40
|
+
|
|
41
|
+
# deliberate failures
|
|
42
|
+
it { should_not have(3).items }
|
|
43
|
+
it { should have(2).items }
|
|
44
|
+
it { should have(4).items }
|
|
45
|
+
|
|
46
|
+
it { should_not have_exactly(3).items }
|
|
47
|
+
it { should have_exactly(2).items }
|
|
48
|
+
it { should have_exactly(4).items }
|
|
49
|
+
|
|
50
|
+
it { should have_at_least(4).items }
|
|
51
|
+
it { should have_at_most(2).items }
|
|
52
|
+
end
|
|
53
|
+
"""
|
|
54
|
+
When I run `rspec have_items_spec.rb`
|
|
55
|
+
Then the output should contain "16 examples, 8 failures"
|
|
56
|
+
And the output should contain "expected target not to have 3 items, got 3"
|
|
57
|
+
And the output should contain "expected 2 items, got 3"
|
|
58
|
+
And the output should contain "expected 4 items, got 3"
|
|
59
|
+
And the output should contain "expected at least 4 items, got 3"
|
|
60
|
+
And the output should contain "expected at most 2 items, got 3"
|
|
61
|
+
|
|
62
|
+
Scenario: have(x).words on a String when String#words is defined
|
|
63
|
+
Given a file named "have_words_spec.rb" with:
|
|
64
|
+
"""
|
|
65
|
+
class String
|
|
66
|
+
def words
|
|
67
|
+
split(' ')
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
describe "a sentence with some words" do
|
|
72
|
+
it { should have(5).words }
|
|
73
|
+
it { should_not have(4).words }
|
|
74
|
+
it { should_not have(6).words }
|
|
75
|
+
|
|
76
|
+
it { should have_exactly(5).words }
|
|
77
|
+
it { should_not have_exactly(4).words }
|
|
78
|
+
it { should_not have_exactly(6).words }
|
|
79
|
+
|
|
80
|
+
it { should have_at_least(4).words }
|
|
81
|
+
it { should have_at_most(6).words }
|
|
82
|
+
|
|
83
|
+
# deliberate failures
|
|
84
|
+
it { should_not have(5).words }
|
|
85
|
+
it { should have(4).words }
|
|
86
|
+
it { should have(6).words }
|
|
87
|
+
|
|
88
|
+
it { should_not have_exactly(5).words }
|
|
89
|
+
it { should have_exactly(4).words }
|
|
90
|
+
it { should have_exactly(6).words }
|
|
91
|
+
|
|
92
|
+
it { should have_at_least(6).words }
|
|
93
|
+
it { should have_at_most(4).words }
|
|
94
|
+
end
|
|
95
|
+
"""
|
|
96
|
+
When I run `rspec have_words_spec.rb`
|
|
97
|
+
Then the output should contain "16 examples, 8 failures"
|
|
98
|
+
And the output should contain "expected target not to have 5 words, got 5"
|
|
99
|
+
And the output should contain "expected 4 words, got 5"
|
|
100
|
+
And the output should contain "expected 6 words, got 5"
|
|
101
|
+
And the output should contain "expected at least 6 words, got 5"
|
|
102
|
+
And the output should contain "expected at most 4 words, got 5"
|
|
103
|
+
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
Feature: include matcher
|
|
2
|
+
|
|
3
|
+
Use the include matcher to specify that a collection includes one or more
|
|
4
|
+
expected objects. This works on any object that responds to #include? (such
|
|
5
|
+
as a string or array):
|
|
6
|
+
|
|
7
|
+
"a string".should include("a")
|
|
8
|
+
"a string".should include("str")
|
|
9
|
+
"a string".should include("str", "g")
|
|
10
|
+
"a string".should_not include("foo")
|
|
11
|
+
|
|
12
|
+
[1, 2].should include(1)
|
|
13
|
+
[1, 2].should include(1, 2)
|
|
14
|
+
[1, 2].should_not include(17)
|
|
15
|
+
|
|
16
|
+
The matcher also provides flexible handling for hashes:
|
|
17
|
+
|
|
18
|
+
{:a => 1, :b => 2}.should include(:a)
|
|
19
|
+
{:a => 1, :b => 2}.should include(:a, :b)
|
|
20
|
+
{:a => 1, :b => 2}.should include(:a => 1)
|
|
21
|
+
{:a => 1, :b => 2}.should include(:b => 2, :a => 1)
|
|
22
|
+
{:a => 1, :b => 2}.should_not include(:c)
|
|
23
|
+
{:a => 1, :b => 2}.should_not include(:a => 2)
|
|
24
|
+
{:a => 1, :b => 2}.should_not include(:c => 3)
|
|
25
|
+
|
|
26
|
+
Scenario: array usage
|
|
27
|
+
Given a file named "array_include_matcher_spec.rb" with:
|
|
28
|
+
"""
|
|
29
|
+
describe [1, 3, 7] do
|
|
30
|
+
it { should include(1) }
|
|
31
|
+
it { should include(3) }
|
|
32
|
+
it { should include(7) }
|
|
33
|
+
it { should include(1, 7) }
|
|
34
|
+
it { should include(1, 3, 7) }
|
|
35
|
+
it { should_not include(17) }
|
|
36
|
+
it { should_not include(43, 100) }
|
|
37
|
+
|
|
38
|
+
# deliberate failures
|
|
39
|
+
it { should include(4) }
|
|
40
|
+
it { should_not include(1) }
|
|
41
|
+
it { should_not include(3) }
|
|
42
|
+
it { should_not include(7) }
|
|
43
|
+
it { should_not include(1, 3, 7) }
|
|
44
|
+
|
|
45
|
+
# both of these should fail since it includes 1 but not 9
|
|
46
|
+
it { should include(1, 9) }
|
|
47
|
+
it { should_not include(1, 9) }
|
|
48
|
+
end
|
|
49
|
+
"""
|
|
50
|
+
When I run `rspec array_include_matcher_spec.rb`
|
|
51
|
+
Then the output should contain all of these:
|
|
52
|
+
| 14 examples, 7 failures |
|
|
53
|
+
| expected [1, 3, 7] to include 4 |
|
|
54
|
+
| expected [1, 3, 7] not to include 1 |
|
|
55
|
+
| expected [1, 3, 7] not to include 3 |
|
|
56
|
+
| expected [1, 3, 7] not to include 7 |
|
|
57
|
+
| expected [1, 3, 7] not to include 1, 3, and 7 |
|
|
58
|
+
| expected [1, 3, 7] to include 1 and 9 |
|
|
59
|
+
| expected [1, 3, 7] not to include 1 and 9 |
|
|
60
|
+
|
|
61
|
+
Scenario: string usage
|
|
62
|
+
Given a file named "string_include_matcher_spec.rb" with:
|
|
63
|
+
"""
|
|
64
|
+
describe "a string" do
|
|
65
|
+
it { should include("str") }
|
|
66
|
+
it { should include("a", "str", "ng") }
|
|
67
|
+
it { should_not include("foo") }
|
|
68
|
+
it { should_not include("foo", "bar") }
|
|
69
|
+
|
|
70
|
+
# deliberate failures
|
|
71
|
+
it { should include("foo") }
|
|
72
|
+
it { should_not include("str") }
|
|
73
|
+
it { should include("str", "foo") }
|
|
74
|
+
it { should_not include("str", "foo") }
|
|
75
|
+
end
|
|
76
|
+
"""
|
|
77
|
+
When I run `rspec string_include_matcher_spec.rb`
|
|
78
|
+
Then the output should contain all of these:
|
|
79
|
+
| 8 examples, 4 failures |
|
|
80
|
+
| expected "a string" to include "foo" |
|
|
81
|
+
| expected "a string" not to include "str" |
|
|
82
|
+
| expected "a string" to include "str" and "foo" |
|
|
83
|
+
| expected "a string" not to include "str" and "foo" |
|
|
84
|
+
|
|
85
|
+
Scenario: hash usage
|
|
86
|
+
Given a file named "hash_include_matcher_spec.rb" with:
|
|
87
|
+
"""
|
|
88
|
+
describe Hash do
|
|
89
|
+
subject { { :a => 7, :b => 5 } }
|
|
90
|
+
|
|
91
|
+
it { should include(:a) }
|
|
92
|
+
it { should include(:b, :a) }
|
|
93
|
+
it { should include(:a => 7) }
|
|
94
|
+
it { should include(:b => 5, :a => 7) }
|
|
95
|
+
it { should_not include(:c) }
|
|
96
|
+
it { should_not include(:c, :d) }
|
|
97
|
+
it { should_not include(:d => 2) }
|
|
98
|
+
it { should_not include(:a => 5) }
|
|
99
|
+
it { should_not include(:b => 7, :a => 5) }
|
|
100
|
+
|
|
101
|
+
# deliberate failures
|
|
102
|
+
it { should_not include(:a) }
|
|
103
|
+
it { should_not include(:b, :a) }
|
|
104
|
+
it { should_not include(:a => 7) }
|
|
105
|
+
it { should_not include(:a => 7, :b => 5) }
|
|
106
|
+
it { should include(:c) }
|
|
107
|
+
it { should include(:c, :d) }
|
|
108
|
+
it { should include(:d => 2) }
|
|
109
|
+
it { should include(:a => 5) }
|
|
110
|
+
it { should include(:a => 5, :b => 7) }
|
|
111
|
+
|
|
112
|
+
# Mixed cases--the hash includes one but not the other.
|
|
113
|
+
# All 4 of these cases should fail.
|
|
114
|
+
it { should include(:a, :d) }
|
|
115
|
+
it { should_not include(:a, :d) }
|
|
116
|
+
it { should include(:a => 7, :d => 3) }
|
|
117
|
+
it { should_not include(:a => 7, :d => 3) }
|
|
118
|
+
end
|
|
119
|
+
"""
|
|
120
|
+
When I run `rspec hash_include_matcher_spec.rb`
|
|
121
|
+
Then the output should contain "13 failure"
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
Feature: match matcher
|
|
2
|
+
|
|
3
|
+
The match matcher calls #match on the object, passing if #match returns a
|
|
4
|
+
truthy (not false or nil) value. Regexp and String both provide a #match
|
|
5
|
+
method.
|
|
6
|
+
|
|
7
|
+
"a string".should match(/str/) # passes
|
|
8
|
+
"a string".should match(/foo/) # fails
|
|
9
|
+
/foo/.should match("food") # passes
|
|
10
|
+
/foo/.should match("drinks") # fails
|
|
11
|
+
|
|
12
|
+
This is equivalent to using the =~ matcher (see the operator matchers
|
|
13
|
+
feature for more details).
|
|
14
|
+
|
|
15
|
+
Scenario: string usage
|
|
16
|
+
Given a file named "string_match_spec.rb" with:
|
|
17
|
+
"""
|
|
18
|
+
describe "a string" do
|
|
19
|
+
it { should match(/str/) }
|
|
20
|
+
it { should_not match(/foo/) }
|
|
21
|
+
|
|
22
|
+
# deliberate failures
|
|
23
|
+
it { should_not match(/str/) }
|
|
24
|
+
it { should match(/foo/) }
|
|
25
|
+
end
|
|
26
|
+
"""
|
|
27
|
+
When I run `rspec string_match_spec.rb`
|
|
28
|
+
Then the output should contain all of these:
|
|
29
|
+
| 4 examples, 2 failures |
|
|
30
|
+
| expected "a string" not to match /str/ |
|
|
31
|
+
| expected "a string" to match /foo/ |
|
|
32
|
+
|
|
33
|
+
Scenario: regular expression usage
|
|
34
|
+
Given a file named "regexp_match_spec.rb" with:
|
|
35
|
+
"""
|
|
36
|
+
describe /foo/ do
|
|
37
|
+
it { should match("food") }
|
|
38
|
+
it { should_not match("drinks") }
|
|
39
|
+
|
|
40
|
+
# deliberate failures
|
|
41
|
+
it { should_not match("food") }
|
|
42
|
+
it { should match("drinks") }
|
|
43
|
+
end
|
|
44
|
+
"""
|
|
45
|
+
When I run `rspec regexp_match_spec.rb`
|
|
46
|
+
Then the output should contain all of these:
|
|
47
|
+
| 4 examples, 2 failures |
|
|
48
|
+
| expected /foo/ not to match "food" |
|
|
49
|
+
| expected /foo/ to match "drinks" |
|
|
50
|
+
|