rspec-expectations 2.14.0 → 3.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data/.document +1 -1
- data/.yardopts +1 -1
- data/Changelog.md +976 -25
- data/{License.txt → LICENSE.md} +5 -3
- data/README.md +162 -26
- data/lib/rspec/expectations/block_snippet_extractor.rb +253 -0
- data/lib/rspec/expectations/configuration.rb +230 -0
- data/lib/rspec/expectations/expectation_target.rb +127 -51
- data/lib/rspec/expectations/fail_with.rb +17 -57
- data/lib/rspec/expectations/failure_aggregator.rb +229 -0
- data/lib/rspec/expectations/handler.rb +146 -32
- data/lib/rspec/expectations/minitest_integration.rb +58 -0
- data/lib/rspec/expectations/syntax.rb +68 -100
- data/lib/rspec/expectations/version.rb +1 -1
- data/lib/rspec/expectations.rb +58 -23
- data/lib/rspec/matchers/aliased_matcher.rb +116 -0
- data/lib/rspec/matchers/built_in/all.rb +86 -0
- data/lib/rspec/matchers/built_in/base_matcher.rb +191 -20
- data/lib/rspec/matchers/built_in/be.rb +114 -114
- data/lib/rspec/matchers/built_in/be_between.rb +77 -0
- data/lib/rspec/matchers/built_in/be_instance_of.rb +15 -4
- data/lib/rspec/matchers/built_in/be_kind_of.rb +10 -1
- data/lib/rspec/matchers/built_in/be_within.rb +35 -18
- data/lib/rspec/matchers/built_in/change.rb +389 -80
- data/lib/rspec/matchers/built_in/compound.rb +290 -0
- data/lib/rspec/matchers/built_in/contain_exactly.rb +310 -0
- data/lib/rspec/matchers/built_in/count_expectation.rb +169 -0
- data/lib/rspec/matchers/built_in/cover.rb +3 -0
- data/lib/rspec/matchers/built_in/eq.rb +30 -8
- data/lib/rspec/matchers/built_in/eql.rb +23 -8
- data/lib/rspec/matchers/built_in/equal.rb +55 -22
- data/lib/rspec/matchers/built_in/exist.rb +74 -10
- data/lib/rspec/matchers/built_in/has.rb +141 -22
- data/lib/rspec/matchers/built_in/have_attributes.rb +114 -0
- data/lib/rspec/matchers/built_in/include.rb +184 -32
- data/lib/rspec/matchers/built_in/match.rb +95 -1
- data/lib/rspec/matchers/built_in/operators.rb +128 -0
- data/lib/rspec/matchers/built_in/output.rb +207 -0
- data/lib/rspec/matchers/built_in/raise_error.rb +192 -44
- data/lib/rspec/matchers/built_in/respond_to.rb +154 -28
- data/lib/rspec/matchers/built_in/satisfy.rb +39 -9
- data/lib/rspec/matchers/built_in/start_or_end_with.rb +94 -0
- data/lib/rspec/matchers/built_in/throw_symbol.rb +58 -14
- data/lib/rspec/matchers/built_in/yield.rb +240 -161
- data/lib/rspec/matchers/built_in.rb +47 -33
- data/lib/rspec/matchers/composable.rb +171 -0
- data/lib/rspec/matchers/dsl.rb +531 -10
- data/lib/rspec/matchers/english_phrasing.rb +58 -0
- data/lib/rspec/matchers/fail_matchers.rb +42 -0
- data/lib/rspec/matchers/generated_descriptions.rb +14 -8
- data/lib/rspec/matchers/matcher_delegator.rb +61 -0
- data/lib/rspec/matchers/matcher_protocol.rb +105 -0
- data/lib/rspec/matchers/multi_matcher_diff.rb +82 -0
- data/lib/rspec/matchers.rb +520 -173
- data.tar.gz.sig +0 -0
- metadata +141 -242
- metadata.gz.sig +2 -0
- data/features/README.md +0 -48
- data/features/Upgrade.md +0 -53
- data/features/built_in_matchers/README.md +0 -90
- data/features/built_in_matchers/be.feature +0 -175
- data/features/built_in_matchers/be_within.feature +0 -48
- data/features/built_in_matchers/cover.feature +0 -47
- data/features/built_in_matchers/end_with.feature +0 -48
- data/features/built_in_matchers/equality.feature +0 -139
- data/features/built_in_matchers/exist.feature +0 -45
- data/features/built_in_matchers/expect_change.feature +0 -59
- data/features/built_in_matchers/expect_error.feature +0 -144
- data/features/built_in_matchers/have.feature +0 -109
- data/features/built_in_matchers/include.feature +0 -174
- data/features/built_in_matchers/match.feature +0 -52
- data/features/built_in_matchers/operators.feature +0 -227
- data/features/built_in_matchers/predicates.feature +0 -137
- data/features/built_in_matchers/respond_to.feature +0 -84
- data/features/built_in_matchers/satisfy.feature +0 -33
- data/features/built_in_matchers/start_with.feature +0 -48
- data/features/built_in_matchers/throw_symbol.feature +0 -91
- data/features/built_in_matchers/types.feature +0 -116
- data/features/built_in_matchers/yield.feature +0 -161
- data/features/custom_matchers/access_running_example.feature +0 -53
- data/features/custom_matchers/define_diffable_matcher.feature +0 -27
- data/features/custom_matchers/define_matcher.feature +0 -368
- data/features/custom_matchers/define_matcher_outside_rspec.feature +0 -38
- data/features/custom_matchers/define_matcher_with_fluent_interface.feature +0 -24
- data/features/customized_message.feature +0 -22
- data/features/diffing.feature +0 -85
- data/features/implicit_docstrings.feature +0 -52
- data/features/step_definitions/additional_cli_steps.rb +0 -22
- data/features/support/env.rb +0 -14
- data/features/syntax_configuration.feature +0 -71
- data/features/test_frameworks/test_unit.feature +0 -44
- data/lib/rspec/expectations/deprecation.rb +0 -17
- data/lib/rspec/expectations/differ.rb +0 -133
- data/lib/rspec/expectations/errors.rb +0 -9
- data/lib/rspec/expectations/extensions/array.rb +0 -9
- data/lib/rspec/expectations/extensions/object.rb +0 -29
- data/lib/rspec/expectations/extensions.rb +0 -2
- data/lib/rspec/matchers/be_close.rb +0 -9
- data/lib/rspec/matchers/built_in/have.rb +0 -124
- data/lib/rspec/matchers/built_in/match_array.rb +0 -51
- data/lib/rspec/matchers/built_in/start_and_end_with.rb +0 -48
- data/lib/rspec/matchers/compatibility.rb +0 -14
- data/lib/rspec/matchers/configuration.rb +0 -108
- data/lib/rspec/matchers/extensions/instance_eval_with_args.rb +0 -39
- data/lib/rspec/matchers/matcher.rb +0 -300
- data/lib/rspec/matchers/method_missing.rb +0 -12
- data/lib/rspec/matchers/operator_matcher.rb +0 -109
- data/lib/rspec/matchers/pretty.rb +0 -70
- data/lib/rspec/matchers/test_unit_integration.rb +0 -11
- data/lib/rspec-expectations.rb +0 -1
- data/spec/rspec/expectations/differ_spec.rb +0 -192
- data/spec/rspec/expectations/expectation_target_spec.rb +0 -82
- data/spec/rspec/expectations/extensions/kernel_spec.rb +0 -67
- data/spec/rspec/expectations/fail_with_spec.rb +0 -114
- data/spec/rspec/expectations/handler_spec.rb +0 -227
- data/spec/rspec/expectations/syntax_spec.rb +0 -139
- data/spec/rspec/matchers/base_matcher_spec.rb +0 -62
- data/spec/rspec/matchers/be_close_spec.rb +0 -22
- data/spec/rspec/matchers/be_instance_of_spec.rb +0 -63
- data/spec/rspec/matchers/be_kind_of_spec.rb +0 -41
- data/spec/rspec/matchers/be_spec.rb +0 -516
- data/spec/rspec/matchers/be_within_spec.rb +0 -137
- data/spec/rspec/matchers/change_spec.rb +0 -553
- data/spec/rspec/matchers/configuration_spec.rb +0 -206
- data/spec/rspec/matchers/cover_spec.rb +0 -69
- data/spec/rspec/matchers/description_generation_spec.rb +0 -190
- data/spec/rspec/matchers/dsl_spec.rb +0 -57
- data/spec/rspec/matchers/eq_spec.rb +0 -60
- data/spec/rspec/matchers/eql_spec.rb +0 -41
- data/spec/rspec/matchers/equal_spec.rb +0 -78
- data/spec/rspec/matchers/exist_spec.rb +0 -124
- data/spec/rspec/matchers/has_spec.rb +0 -122
- data/spec/rspec/matchers/have_spec.rb +0 -455
- data/spec/rspec/matchers/include_matcher_integration_spec.rb +0 -30
- data/spec/rspec/matchers/include_spec.rb +0 -531
- data/spec/rspec/matchers/match_array_spec.rb +0 -194
- data/spec/rspec/matchers/match_spec.rb +0 -61
- data/spec/rspec/matchers/matcher_spec.rb +0 -471
- data/spec/rspec/matchers/matchers_spec.rb +0 -37
- data/spec/rspec/matchers/method_missing_spec.rb +0 -28
- data/spec/rspec/matchers/operator_matcher_spec.rb +0 -223
- data/spec/rspec/matchers/raise_error_spec.rb +0 -485
- data/spec/rspec/matchers/respond_to_spec.rb +0 -292
- data/spec/rspec/matchers/satisfy_spec.rb +0 -44
- data/spec/rspec/matchers/start_with_end_with_spec.rb +0 -186
- data/spec/rspec/matchers/throw_symbol_spec.rb +0 -116
- data/spec/rspec/matchers/yield_spec.rb +0 -514
- data/spec/spec_helper.rb +0 -54
- data/spec/support/classes.rb +0 -56
- data/spec/support/in_sub_process.rb +0 -38
- data/spec/support/matchers.rb +0 -22
- data/spec/support/ruby_version.rb +0 -10
- data/spec/support/shared_examples.rb +0 -13
@@ -1,124 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'ostruct'
|
3
|
-
|
4
|
-
describe "exist matcher" do
|
5
|
-
it_behaves_like "an RSpec matcher", :valid_value => OpenStruct.new(:exist? => true),
|
6
|
-
:invalid_value => OpenStruct.new(:exist? => false) do
|
7
|
-
let(:matcher) { exist }
|
8
|
-
end
|
9
|
-
|
10
|
-
context "when the object does not respond to #exist? or #exists?" do
|
11
|
-
subject { double }
|
12
|
-
|
13
|
-
[:to, :not_to].each do |expect_method|
|
14
|
-
describe "expect(...).#{expect_method} exist" do
|
15
|
-
it "raises an error" do
|
16
|
-
expect {
|
17
|
-
expect(subject).send(expect_method, exist)
|
18
|
-
}.to raise_error(NoMethodError)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
[:exist?, :exists?].each do |predicate|
|
25
|
-
context "when the object responds to ##{predicate}" do
|
26
|
-
describe "expect(...).to exist" do
|
27
|
-
it "passes if #{predicate}" do
|
28
|
-
expect(double(predicate => true)).to exist
|
29
|
-
end
|
30
|
-
|
31
|
-
it "fails if not #{predicate}" do
|
32
|
-
expect {
|
33
|
-
expect(double(predicate => false)).to exist
|
34
|
-
}.to fail_with(/expected .* to exist/)
|
35
|
-
end
|
36
|
-
|
37
|
-
it 'works when the object overrides `send`' do
|
38
|
-
klass = Struct.new(:message) do
|
39
|
-
def send
|
40
|
-
:message_sent
|
41
|
-
end
|
42
|
-
|
43
|
-
define_method predicate do
|
44
|
-
true
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
expect(klass.new("msg")).to exist
|
49
|
-
end
|
50
|
-
end
|
51
|
-
|
52
|
-
describe "expect(...).not_to exist" do
|
53
|
-
it "passes if not #{predicate}" do
|
54
|
-
expect(double(predicate => false)).not_to exist
|
55
|
-
end
|
56
|
-
|
57
|
-
it "fails if #{predicate}" do
|
58
|
-
expect {
|
59
|
-
expect(double(predicate => true)).not_to exist
|
60
|
-
}.to fail_with(/expected .* not to exist/)
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
context "when the object responds to #exist? and #exists?" do
|
67
|
-
context "when they both return falsey values" do
|
68
|
-
subject { double(:exist? => false, :exists? => nil) }
|
69
|
-
|
70
|
-
describe "expect(...).not_to exist" do
|
71
|
-
it "passes" do
|
72
|
-
expect(subject).not_to exist
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
describe "expect(...).to exist" do
|
77
|
-
it "fails" do
|
78
|
-
expect {
|
79
|
-
expect(subject).to exist
|
80
|
-
}.to fail_with(/expected .* to exist/)
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
context "when they both return truthy values" do
|
86
|
-
subject { double(:exist? => true, :exists? => "something true") }
|
87
|
-
|
88
|
-
describe "expect(...).not_to exist" do
|
89
|
-
it "fails" do
|
90
|
-
expect {
|
91
|
-
expect(subject).not_to exist
|
92
|
-
}.to fail_with(/expected .* not to exist/)
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
describe "expect(...).to exist" do
|
97
|
-
it "passes" do
|
98
|
-
expect(subject).to exist
|
99
|
-
end
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
context "when they return values with different truthiness" do
|
104
|
-
subject { double(:exist? => true, :exists? => false) }
|
105
|
-
|
106
|
-
[:to, :not_to].each do |expect_method|
|
107
|
-
describe "expect(...).#{expect_method} exist" do
|
108
|
-
it "raises an error" do
|
109
|
-
expect {
|
110
|
-
expect(subject).send(expect_method, exist)
|
111
|
-
}.to raise_error(/#exist\? and #exists\? returned different values/)
|
112
|
-
end
|
113
|
-
end
|
114
|
-
end
|
115
|
-
end
|
116
|
-
end
|
117
|
-
|
118
|
-
it 'passes any provided arguments to the call to #exist?' do
|
119
|
-
object = double
|
120
|
-
object.should_receive(:exist?).with(:foo, :bar) { true }
|
121
|
-
|
122
|
-
expect(object).to exist(:foo, :bar)
|
123
|
-
end
|
124
|
-
end
|
@@ -1,122 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "expect(...).to have_sym(*args)" do
|
4
|
-
it_behaves_like "an RSpec matcher", :valid_value => { :a => 1 },
|
5
|
-
:invalid_value => {} do
|
6
|
-
let(:matcher) { have_key(:a) }
|
7
|
-
end
|
8
|
-
|
9
|
-
it "passes if #has_sym?(*args) returns true" do
|
10
|
-
expect({:a => "A"}).to have_key(:a)
|
11
|
-
end
|
12
|
-
|
13
|
-
it "fails if #has_sym?(*args) returns false" do
|
14
|
-
expect {
|
15
|
-
expect({:b => "B"}).to have_key(:a)
|
16
|
-
}.to fail_with("expected #has_key?(:a) to return true, got false")
|
17
|
-
end
|
18
|
-
|
19
|
-
it 'does not include any args in the failure message if no args were given to the matcher' do
|
20
|
-
o = Object.new
|
21
|
-
def o.has_some_stuff?; false; end
|
22
|
-
expect {
|
23
|
-
expect(o).to have_some_stuff
|
24
|
-
}.to fail_with("expected #has_some_stuff? to return true, got false")
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'includes multiple args in the failure message if multiple args were given to the matcher' do
|
28
|
-
o = Object.new
|
29
|
-
def o.has_some_stuff?(*_); false; end
|
30
|
-
expect {
|
31
|
-
expect(o).to have_some_stuff(:a, 7, "foo")
|
32
|
-
}.to fail_with('expected #has_some_stuff?(:a, 7, "foo") to return true, got false')
|
33
|
-
end
|
34
|
-
|
35
|
-
it "fails if #has_sym?(*args) returns nil" do
|
36
|
-
klass = Class.new do
|
37
|
-
def has_foo?
|
38
|
-
end
|
39
|
-
end
|
40
|
-
expect {
|
41
|
-
expect(klass.new).to have_foo
|
42
|
-
}.to fail_with(/expected #has_foo.* to return true, got false/)
|
43
|
-
end
|
44
|
-
|
45
|
-
it "fails if target does not respond to #has_sym?" do
|
46
|
-
expect {
|
47
|
-
expect(Object.new).to have_key(:a)
|
48
|
-
}.to raise_error(NoMethodError)
|
49
|
-
end
|
50
|
-
|
51
|
-
it "reraises an exception thrown in #has_sym?(*args)" do
|
52
|
-
o = Object.new
|
53
|
-
def o.has_sym?(*args)
|
54
|
-
raise "Funky exception"
|
55
|
-
end
|
56
|
-
expect {
|
57
|
-
expect(o).to have_sym(:foo)
|
58
|
-
}.to raise_error("Funky exception")
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
describe "expect(...).not_to have_sym(*args)" do
|
63
|
-
it "passes if #has_sym?(*args) returns false" do
|
64
|
-
expect({:a => "A"}).not_to have_key(:b)
|
65
|
-
end
|
66
|
-
|
67
|
-
it "passes if #has_sym?(*args) returns nil" do
|
68
|
-
klass = Class.new do
|
69
|
-
def has_foo?
|
70
|
-
end
|
71
|
-
end
|
72
|
-
expect(klass.new).not_to have_foo
|
73
|
-
end
|
74
|
-
|
75
|
-
it "fails if #has_sym?(*args) returns true" do
|
76
|
-
expect {
|
77
|
-
expect({:a => "A"}).not_to have_key(:a)
|
78
|
-
}.to fail_with("expected #has_key?(:a) to return false, got true")
|
79
|
-
end
|
80
|
-
|
81
|
-
it "fails if target does not respond to #has_sym?" do
|
82
|
-
expect {
|
83
|
-
expect(Object.new).to have_key(:a)
|
84
|
-
}.to raise_error(NoMethodError)
|
85
|
-
end
|
86
|
-
|
87
|
-
it "reraises an exception thrown in #has_sym?(*args)" do
|
88
|
-
o = Object.new
|
89
|
-
def o.has_sym?(*args)
|
90
|
-
raise "Funky exception"
|
91
|
-
end
|
92
|
-
expect {
|
93
|
-
expect(o).not_to have_sym(:foo)
|
94
|
-
}.to raise_error("Funky exception")
|
95
|
-
end
|
96
|
-
|
97
|
-
it 'does not include any args in the failure message if no args were given to the matcher' do
|
98
|
-
o = Object.new
|
99
|
-
def o.has_some_stuff?; true; end
|
100
|
-
expect {
|
101
|
-
expect(o).not_to have_some_stuff
|
102
|
-
}.to fail_with("expected #has_some_stuff? to return false, got true")
|
103
|
-
end
|
104
|
-
|
105
|
-
it 'includes multiple args in the failure message if multiple args were given to the matcher' do
|
106
|
-
o = Object.new
|
107
|
-
def o.has_some_stuff?(*_); true; end
|
108
|
-
expect {
|
109
|
-
expect(o).not_to have_some_stuff(:a, 7, "foo")
|
110
|
-
}.to fail_with('expected #has_some_stuff?(:a, 7, "foo") to return false, got true')
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
describe "has" do
|
115
|
-
it "works when the target implements #send" do
|
116
|
-
o = {:a => "A"}
|
117
|
-
def o.send(*args); raise "DOH! Library developers shouldn't use #send!" end
|
118
|
-
expect {
|
119
|
-
expect(o).to have_key(:a)
|
120
|
-
}.not_to raise_error
|
121
|
-
end
|
122
|
-
end
|
@@ -1,455 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'stringio'
|
3
|
-
|
4
|
-
describe "have matcher" do
|
5
|
-
let(:inflector) do
|
6
|
-
Class.new do
|
7
|
-
def self.pluralize(string)
|
8
|
-
string.to_s + 's'
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
before(:each) { stub_const("ActiveSupport::Inflector", inflector) }
|
14
|
-
|
15
|
-
def create_collection_owner_with(n)
|
16
|
-
owner = RSpec::Expectations::Helper::CollectionOwner.new
|
17
|
-
(1..n).each do |number|
|
18
|
-
owner.add_to_collection_with_length_method(number)
|
19
|
-
owner.add_to_collection_with_size_method(number)
|
20
|
-
owner.add_to_collection_with_count_method(number)
|
21
|
-
end
|
22
|
-
owner
|
23
|
-
end
|
24
|
-
|
25
|
-
describe "expect(...).to have(n).items" do
|
26
|
-
it_behaves_like "an RSpec matcher", :valid_value => [1, 2], :invalid_value => [1] do
|
27
|
-
let(:matcher) { have(2).items }
|
28
|
-
end
|
29
|
-
|
30
|
-
it "passes if target has a collection of items with n members" do
|
31
|
-
owner = create_collection_owner_with(3)
|
32
|
-
expect(owner).to have(3).items_in_collection_with_length_method
|
33
|
-
expect(owner).to have(3).items_in_collection_with_size_method
|
34
|
-
expect(owner).to have(3).items_in_collection_with_count_method
|
35
|
-
end
|
36
|
-
|
37
|
-
it "converts :no to 0" do
|
38
|
-
owner = create_collection_owner_with(0)
|
39
|
-
expect(owner).to have(:no).items_in_collection_with_length_method
|
40
|
-
expect(owner).to have(:no).items_in_collection_with_size_method
|
41
|
-
expect(owner).to have(:no).items_in_collection_with_count_method
|
42
|
-
end
|
43
|
-
|
44
|
-
it "converts a String argument to Integer" do
|
45
|
-
owner = create_collection_owner_with(3)
|
46
|
-
expect(owner).to have('3').items_in_collection_with_length_method
|
47
|
-
expect(owner).to have('3').items_in_collection_with_size_method
|
48
|
-
expect(owner).to have('3').items_in_collection_with_count_method
|
49
|
-
end
|
50
|
-
|
51
|
-
it "fails if target has a collection of items with < n members" do
|
52
|
-
owner = create_collection_owner_with(3)
|
53
|
-
expect {
|
54
|
-
expect(owner).to have(4).items_in_collection_with_length_method
|
55
|
-
}.to fail_with("expected 4 items_in_collection_with_length_method, got 3")
|
56
|
-
expect {
|
57
|
-
expect(owner).to have(4).items_in_collection_with_size_method
|
58
|
-
}.to fail_with("expected 4 items_in_collection_with_size_method, got 3")
|
59
|
-
expect {
|
60
|
-
expect(owner).to have(4).items_in_collection_with_count_method
|
61
|
-
}.to fail_with("expected 4 items_in_collection_with_count_method, got 3")
|
62
|
-
end
|
63
|
-
|
64
|
-
it "fails if target has a collection of items with > n members" do
|
65
|
-
owner = create_collection_owner_with(3)
|
66
|
-
expect {
|
67
|
-
expect(owner).to have(2).items_in_collection_with_length_method
|
68
|
-
}.to fail_with("expected 2 items_in_collection_with_length_method, got 3")
|
69
|
-
expect {
|
70
|
-
expect(owner).to have(2).items_in_collection_with_size_method
|
71
|
-
}.to fail_with("expected 2 items_in_collection_with_size_method, got 3")
|
72
|
-
expect {
|
73
|
-
expect(owner).to have(2).items_in_collection_with_count_method
|
74
|
-
}.to fail_with("expected 2 items_in_collection_with_count_method, got 3")
|
75
|
-
end
|
76
|
-
end
|
77
|
-
|
78
|
-
describe 'expect(...).to have(1).item when ActiveSupport::Inflector is defined' do
|
79
|
-
|
80
|
-
it 'pluralizes the collection name' do
|
81
|
-
owner = create_collection_owner_with(1)
|
82
|
-
expect(owner).to have(1).item
|
83
|
-
end
|
84
|
-
|
85
|
-
context "when ActiveSupport::Inflector is partially loaded without its inflectors" do
|
86
|
-
|
87
|
-
it "does not pluralize the collection name" do
|
88
|
-
stub_const("ActiveSupport::Inflector", Module.new)
|
89
|
-
owner = create_collection_owner_with(1)
|
90
|
-
expect {
|
91
|
-
expect(owner).to have(1).item
|
92
|
-
}.to raise_error(NoMethodError)
|
93
|
-
end
|
94
|
-
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
describe 'expect(...).to have(1).item when Inflector is defined' do
|
99
|
-
before { stub_const("Inflector", inflector) }
|
100
|
-
|
101
|
-
it 'pluralizes the collection name' do
|
102
|
-
owner = create_collection_owner_with(1)
|
103
|
-
expect(owner).to have(1).item
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
describe "expect(...).to have(n).items where result responds to items but returns something other than a collection" do
|
108
|
-
it "provides a meaningful error" do
|
109
|
-
owner = Class.new do
|
110
|
-
def items
|
111
|
-
Object.new
|
112
|
-
end
|
113
|
-
end.new
|
114
|
-
expect do
|
115
|
-
expect(owner).to have(3).items
|
116
|
-
end.to raise_error("expected items to be a collection but it does not respond to #length, #size or #count")
|
117
|
-
end
|
118
|
-
end
|
119
|
-
|
120
|
-
describe "expect(...).not_to have(n).items" do
|
121
|
-
|
122
|
-
it "passes if target has a collection of items with < n members" do
|
123
|
-
owner = create_collection_owner_with(3)
|
124
|
-
expect(owner).not_to have(4).items_in_collection_with_length_method
|
125
|
-
expect(owner).not_to have(4).items_in_collection_with_size_method
|
126
|
-
expect(owner).not_to have(4).items_in_collection_with_count_method
|
127
|
-
end
|
128
|
-
|
129
|
-
it "passes if target has a collection of items with > n members" do
|
130
|
-
owner = create_collection_owner_with(3)
|
131
|
-
expect(owner).not_to have(2).items_in_collection_with_length_method
|
132
|
-
expect(owner).not_to have(2).items_in_collection_with_size_method
|
133
|
-
expect(owner).not_to have(2).items_in_collection_with_count_method
|
134
|
-
end
|
135
|
-
|
136
|
-
it "fails if target has a collection of items with n members" do
|
137
|
-
owner = create_collection_owner_with(3)
|
138
|
-
expect {
|
139
|
-
expect(owner).not_to have(3).items_in_collection_with_length_method
|
140
|
-
}.to fail_with("expected target not to have 3 items_in_collection_with_length_method, got 3")
|
141
|
-
expect {
|
142
|
-
expect(owner).not_to have(3).items_in_collection_with_size_method
|
143
|
-
}.to fail_with("expected target not to have 3 items_in_collection_with_size_method, got 3")
|
144
|
-
expect {
|
145
|
-
expect(owner).not_to have(3).items_in_collection_with_count_method
|
146
|
-
}.to fail_with("expected target not to have 3 items_in_collection_with_count_method, got 3")
|
147
|
-
end
|
148
|
-
end
|
149
|
-
|
150
|
-
describe "expect(...).to have_exactly(n).items" do
|
151
|
-
|
152
|
-
it "passes if target has a collection of items with n members" do
|
153
|
-
owner = create_collection_owner_with(3)
|
154
|
-
expect(owner).to have_exactly(3).items_in_collection_with_length_method
|
155
|
-
expect(owner).to have_exactly(3).items_in_collection_with_size_method
|
156
|
-
expect(owner).to have_exactly(3).items_in_collection_with_count_method
|
157
|
-
end
|
158
|
-
|
159
|
-
it "converts :no to 0" do
|
160
|
-
owner = create_collection_owner_with(0)
|
161
|
-
expect(owner).to have_exactly(:no).items_in_collection_with_length_method
|
162
|
-
expect(owner).to have_exactly(:no).items_in_collection_with_size_method
|
163
|
-
expect(owner).to have_exactly(:no).items_in_collection_with_count_method
|
164
|
-
end
|
165
|
-
|
166
|
-
it "fails if target has a collection of items with < n members" do
|
167
|
-
owner = create_collection_owner_with(3)
|
168
|
-
expect {
|
169
|
-
expect(owner).to have_exactly(4).items_in_collection_with_length_method
|
170
|
-
}.to fail_with("expected 4 items_in_collection_with_length_method, got 3")
|
171
|
-
expect {
|
172
|
-
expect(owner).to have_exactly(4).items_in_collection_with_size_method
|
173
|
-
}.to fail_with("expected 4 items_in_collection_with_size_method, got 3")
|
174
|
-
expect {
|
175
|
-
expect(owner).to have_exactly(4).items_in_collection_with_count_method
|
176
|
-
}.to fail_with("expected 4 items_in_collection_with_count_method, got 3")
|
177
|
-
end
|
178
|
-
|
179
|
-
it "fails if target has a collection of items with > n members" do
|
180
|
-
owner = create_collection_owner_with(3)
|
181
|
-
expect {
|
182
|
-
expect(owner).to have_exactly(2).items_in_collection_with_length_method
|
183
|
-
}.to fail_with("expected 2 items_in_collection_with_length_method, got 3")
|
184
|
-
expect {
|
185
|
-
expect(owner).to have_exactly(2).items_in_collection_with_size_method
|
186
|
-
}.to fail_with("expected 2 items_in_collection_with_size_method, got 3")
|
187
|
-
expect {
|
188
|
-
expect(owner).to have_exactly(2).items_in_collection_with_count_method
|
189
|
-
}.to fail_with("expected 2 items_in_collection_with_count_method, got 3")
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
describe "expect(...).to have_at_least(n).items" do
|
194
|
-
|
195
|
-
it "passes if target has a collection of items with n members" do
|
196
|
-
owner = create_collection_owner_with(3)
|
197
|
-
expect(owner).to have_at_least(3).items_in_collection_with_length_method
|
198
|
-
expect(owner).to have_at_least(3).items_in_collection_with_size_method
|
199
|
-
expect(owner).to have_at_least(3).items_in_collection_with_count_method
|
200
|
-
end
|
201
|
-
|
202
|
-
it "passes if target has a collection of items with > n members" do
|
203
|
-
owner = create_collection_owner_with(3)
|
204
|
-
expect(owner).to have_at_least(2).items_in_collection_with_length_method
|
205
|
-
expect(owner).to have_at_least(2).items_in_collection_with_size_method
|
206
|
-
expect(owner).to have_at_least(2).items_in_collection_with_count_method
|
207
|
-
end
|
208
|
-
|
209
|
-
it "fails if target has a collection of items with < n members" do
|
210
|
-
owner = create_collection_owner_with(3)
|
211
|
-
expect {
|
212
|
-
expect(owner).to have_at_least(4).items_in_collection_with_length_method
|
213
|
-
}.to fail_with("expected at least 4 items_in_collection_with_length_method, got 3")
|
214
|
-
expect {
|
215
|
-
expect(owner).to have_at_least(4).items_in_collection_with_size_method
|
216
|
-
}.to fail_with("expected at least 4 items_in_collection_with_size_method, got 3")
|
217
|
-
expect {
|
218
|
-
expect(owner).to have_at_least(4).items_in_collection_with_count_method
|
219
|
-
}.to fail_with("expected at least 4 items_in_collection_with_count_method, got 3")
|
220
|
-
end
|
221
|
-
|
222
|
-
it "provides educational negative failure messages" do
|
223
|
-
#given
|
224
|
-
owner = create_collection_owner_with(3)
|
225
|
-
length_matcher = have_at_least(3).items_in_collection_with_length_method
|
226
|
-
size_matcher = have_at_least(3).items_in_collection_with_size_method
|
227
|
-
count_matcher = have_at_least(3).items_in_collection_with_count_method
|
228
|
-
|
229
|
-
#when
|
230
|
-
length_matcher.matches?(owner)
|
231
|
-
size_matcher.matches?(owner)
|
232
|
-
count_matcher.matches?(owner)
|
233
|
-
|
234
|
-
#then
|
235
|
-
expect(length_matcher.failure_message_for_should_not).to eq <<-EOF
|
236
|
-
Isn't life confusing enough?
|
237
|
-
Instead of having to figure out the meaning of this:
|
238
|
-
expect(actual).not_to have_at_least(3).items_in_collection_with_length_method
|
239
|
-
We recommend that you use this instead:
|
240
|
-
expect(actual).to have_at_most(2).items_in_collection_with_length_method
|
241
|
-
EOF
|
242
|
-
|
243
|
-
expect(size_matcher.failure_message_for_should_not).to eq <<-EOF
|
244
|
-
Isn't life confusing enough?
|
245
|
-
Instead of having to figure out the meaning of this:
|
246
|
-
expect(actual).not_to have_at_least(3).items_in_collection_with_size_method
|
247
|
-
We recommend that you use this instead:
|
248
|
-
expect(actual).to have_at_most(2).items_in_collection_with_size_method
|
249
|
-
EOF
|
250
|
-
expect(count_matcher.failure_message_for_should_not).to eq <<-EOF
|
251
|
-
Isn't life confusing enough?
|
252
|
-
Instead of having to figure out the meaning of this:
|
253
|
-
expect(actual).not_to have_at_least(3).items_in_collection_with_count_method
|
254
|
-
We recommend that you use this instead:
|
255
|
-
expect(actual).to have_at_most(2).items_in_collection_with_count_method
|
256
|
-
EOF
|
257
|
-
end
|
258
|
-
end
|
259
|
-
|
260
|
-
describe "expect(...).to have_at_most(n).items" do
|
261
|
-
it "passes if target has a collection of items with n members" do
|
262
|
-
owner = create_collection_owner_with(3)
|
263
|
-
expect(owner).to have_at_most(3).items_in_collection_with_length_method
|
264
|
-
expect(owner).to have_at_most(3).items_in_collection_with_size_method
|
265
|
-
expect(owner).to have_at_most(3).items_in_collection_with_count_method
|
266
|
-
end
|
267
|
-
|
268
|
-
it "fails if target has a collection of items with > n members" do
|
269
|
-
owner = create_collection_owner_with(3)
|
270
|
-
expect {
|
271
|
-
expect(owner).to have_at_most(2).items_in_collection_with_length_method
|
272
|
-
}.to fail_with("expected at most 2 items_in_collection_with_length_method, got 3")
|
273
|
-
expect {
|
274
|
-
expect(owner).to have_at_most(2).items_in_collection_with_size_method
|
275
|
-
}.to fail_with("expected at most 2 items_in_collection_with_size_method, got 3")
|
276
|
-
expect {
|
277
|
-
expect(owner).to have_at_most(2).items_in_collection_with_count_method
|
278
|
-
}.to fail_with("expected at most 2 items_in_collection_with_count_method, got 3")
|
279
|
-
end
|
280
|
-
|
281
|
-
it "passes if target has a collection of items with < n members" do
|
282
|
-
owner = create_collection_owner_with(3)
|
283
|
-
expect(owner).to have_at_most(4).items_in_collection_with_length_method
|
284
|
-
expect(owner).to have_at_most(4).items_in_collection_with_size_method
|
285
|
-
expect(owner).to have_at_most(4).items_in_collection_with_count_method
|
286
|
-
end
|
287
|
-
|
288
|
-
it "provides educational negative failure messages" do
|
289
|
-
#given
|
290
|
-
owner = create_collection_owner_with(3)
|
291
|
-
length_matcher = have_at_most(3).items_in_collection_with_length_method
|
292
|
-
size_matcher = have_at_most(3).items_in_collection_with_size_method
|
293
|
-
count_matcher = have_at_most(3).items_in_collection_with_count_method
|
294
|
-
|
295
|
-
#when
|
296
|
-
length_matcher.matches?(owner)
|
297
|
-
size_matcher.matches?(owner)
|
298
|
-
count_matcher.matches?(owner)
|
299
|
-
|
300
|
-
#then
|
301
|
-
expect(length_matcher.failure_message_for_should_not).to eq <<-EOF
|
302
|
-
Isn't life confusing enough?
|
303
|
-
Instead of having to figure out the meaning of this:
|
304
|
-
expect(actual).not_to have_at_most(3).items_in_collection_with_length_method
|
305
|
-
We recommend that you use this instead:
|
306
|
-
expect(actual).to have_at_least(4).items_in_collection_with_length_method
|
307
|
-
EOF
|
308
|
-
|
309
|
-
expect(size_matcher.failure_message_for_should_not).to eq <<-EOF
|
310
|
-
Isn't life confusing enough?
|
311
|
-
Instead of having to figure out the meaning of this:
|
312
|
-
expect(actual).not_to have_at_most(3).items_in_collection_with_size_method
|
313
|
-
We recommend that you use this instead:
|
314
|
-
expect(actual).to have_at_least(4).items_in_collection_with_size_method
|
315
|
-
EOF
|
316
|
-
|
317
|
-
expect(count_matcher.failure_message_for_should_not).to eq <<-EOF
|
318
|
-
Isn't life confusing enough?
|
319
|
-
Instead of having to figure out the meaning of this:
|
320
|
-
expect(actual).not_to have_at_most(3).items_in_collection_with_count_method
|
321
|
-
We recommend that you use this instead:
|
322
|
-
expect(actual).to have_at_least(4).items_in_collection_with_count_method
|
323
|
-
EOF
|
324
|
-
end
|
325
|
-
end
|
326
|
-
|
327
|
-
describe "have(n).items(args, block)" do
|
328
|
-
it "passes args to target" do
|
329
|
-
target = double("target")
|
330
|
-
target.should_receive(:items).with("arg1","arg2").and_return([1,2,3])
|
331
|
-
expect(target).to have(3).items("arg1","arg2")
|
332
|
-
end
|
333
|
-
|
334
|
-
it "passes block to target" do
|
335
|
-
target = double("target")
|
336
|
-
block = lambda { 5 }
|
337
|
-
target.should_receive(:items).with("arg1","arg2", block).and_return([1,2,3])
|
338
|
-
expect(target).to have(3).items("arg1","arg2", block)
|
339
|
-
end
|
340
|
-
end
|
341
|
-
|
342
|
-
describe "have(n).items where target IS a collection" do
|
343
|
-
it "references the number of items IN the collection" do
|
344
|
-
expect([1,2,3]).to have(3).items
|
345
|
-
end
|
346
|
-
|
347
|
-
it "fails when the number of items IN the collection is not as expected" do
|
348
|
-
expect {
|
349
|
-
expect([1,2,3]).to have(7).items
|
350
|
-
}.to fail_with("expected 7 items, got 3")
|
351
|
-
end
|
352
|
-
end
|
353
|
-
|
354
|
-
describe "have(n).characters where target IS a String" do
|
355
|
-
it "passes if the length is correct" do
|
356
|
-
expect("this string").to have(11).characters
|
357
|
-
end
|
358
|
-
|
359
|
-
it "fails if the length is incorrect" do
|
360
|
-
expect {
|
361
|
-
expect("this string").to have(12).characters
|
362
|
-
}.to fail_with("expected 12 characters, got 11")
|
363
|
-
end
|
364
|
-
end
|
365
|
-
|
366
|
-
describe "have(n).things on an object which is not a collection nor contains one" do
|
367
|
-
it "fails" do
|
368
|
-
expect {
|
369
|
-
expect(Object.new).to have(2).things
|
370
|
-
}.to raise_error(NoMethodError) { |e|
|
371
|
-
expect(e.name).to eq :things
|
372
|
-
}
|
373
|
-
end
|
374
|
-
end
|
375
|
-
|
376
|
-
describe RSpec::Matchers::BuiltIn::Have, "for a collection owner that implements #send" do
|
377
|
-
before(:each) do
|
378
|
-
@collection = Object.new
|
379
|
-
def @collection.floozles; [1,2] end
|
380
|
-
def @collection.send; :sent; end
|
381
|
-
end
|
382
|
-
|
383
|
-
it "works in the straightforward case" do
|
384
|
-
expect(@collection).to have(2).floozles
|
385
|
-
end
|
386
|
-
|
387
|
-
it "works when doing automatic pluralization" do
|
388
|
-
expect(@collection).to have_at_least(1).floozle
|
389
|
-
end
|
390
|
-
|
391
|
-
it "blows up when the owner doesn't respond to that method" do
|
392
|
-
expect {
|
393
|
-
expect(@collection).to have(99).problems
|
394
|
-
}.to raise_error(NoMethodError, /problems/)
|
395
|
-
end
|
396
|
-
|
397
|
-
it 'works when #send is defined directly on an array' do
|
398
|
-
array = [1, 2]
|
399
|
-
def array.send; :sent; end
|
400
|
-
|
401
|
-
expect(array).to have(2).items
|
402
|
-
end
|
403
|
-
end
|
404
|
-
|
405
|
-
if RUBY_VERSION >= '2.0'
|
406
|
-
describe RSpec::Matchers::BuiltIn::Have, "for an Enumerator whose size is nil but count is supplied" do
|
407
|
-
let(:enumerator) { %w[a b c d].to_enum(:each) }
|
408
|
-
|
409
|
-
it 'works fine' do
|
410
|
-
expect(enumerator).to have(4).items
|
411
|
-
end
|
412
|
-
end
|
413
|
-
end
|
414
|
-
|
415
|
-
describe RSpec::Matchers::BuiltIn::Have do
|
416
|
-
it "has method_missing as private" do
|
417
|
-
expect(described_class.private_instance_methods).to include_method(:method_missing)
|
418
|
-
end
|
419
|
-
|
420
|
-
it "does not respond_to? method_missing (because it's private)" do
|
421
|
-
formatter = described_class.new(0, StringIO.new)
|
422
|
-
expect(formatter).not_to respond_to(:method_missing)
|
423
|
-
end
|
424
|
-
|
425
|
-
describe "respond_to?" do
|
426
|
-
before :each do
|
427
|
-
@have = described_class.new(:foo)
|
428
|
-
@a_method_which_have_defines = described_class.instance_methods.first
|
429
|
-
@a_method_which_object_defines = Object.instance_methods.first
|
430
|
-
end
|
431
|
-
|
432
|
-
it "is true for a method which Have defines" do
|
433
|
-
expect(@have).to respond_to(@a_method_which_have_defines)
|
434
|
-
end
|
435
|
-
|
436
|
-
it "is true for a method that it's superclass (Object) defines" do
|
437
|
-
expect(@have).to respond_to(@a_method_which_object_defines)
|
438
|
-
end
|
439
|
-
|
440
|
-
it "is false for a method which neither Object nor nor Have defines" do
|
441
|
-
expect(@have).not_to respond_to(:foo_bar_baz)
|
442
|
-
end
|
443
|
-
|
444
|
-
it "is false if the owner doesn't respond to the method" do
|
445
|
-
have = described_class.new(99)
|
446
|
-
expect(have).not_to respond_to(:problems)
|
447
|
-
end
|
448
|
-
|
449
|
-
it "is true if the owner responds to the method" do
|
450
|
-
have = described_class.new(:a_symbol)
|
451
|
-
expect(have).to respond_to(:to_sym)
|
452
|
-
end
|
453
|
-
end
|
454
|
-
end
|
455
|
-
end
|