rspec-mocks 3.0.0.beta2 → 3.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/Changelog.md +104 -36
- data/README.md +35 -4
- data/lib/rspec/mocks.rb +52 -13
- data/lib/rspec/mocks/any_instance.rb +10 -0
- data/lib/rspec/mocks/any_instance/chain.rb +17 -0
- data/lib/rspec/mocks/any_instance/expect_chain_chain.rb +1 -1
- data/lib/rspec/mocks/any_instance/expectation_chain.rb +9 -6
- data/lib/rspec/mocks/any_instance/message_chains.rb +10 -1
- data/lib/rspec/mocks/any_instance/proxy.rb +112 -0
- data/lib/rspec/mocks/any_instance/recorder.rb +49 -18
- data/lib/rspec/mocks/any_instance/stub_chain.rb +5 -3
- data/lib/rspec/mocks/any_instance/stub_chain_chain.rb +1 -1
- data/lib/rspec/mocks/argument_list_matcher.rb +5 -3
- data/lib/rspec/mocks/argument_matchers.rb +28 -12
- data/lib/rspec/mocks/configuration.rb +29 -0
- data/lib/rspec/mocks/error_generator.rb +2 -5
- data/lib/rspec/mocks/example_methods.rb +122 -13
- data/lib/rspec/mocks/instance_method_stasher.rb +26 -2
- data/lib/rspec/mocks/marshal_extension.rb +41 -0
- data/lib/rspec/mocks/matchers/expectation_customization.rb +20 -0
- data/lib/rspec/mocks/matchers/receive.rb +6 -20
- data/lib/rspec/mocks/matchers/receive_message_chain.rb +7 -5
- data/lib/rspec/mocks/matchers/receive_messages.rb +1 -2
- data/lib/rspec/mocks/message_chain.rb +9 -9
- data/lib/rspec/mocks/message_expectation.rb +6 -5
- data/lib/rspec/mocks/method_double.rb +67 -10
- data/lib/rspec/mocks/method_reference.rb +1 -1
- data/lib/rspec/mocks/mutate_const.rb +10 -14
- data/lib/rspec/mocks/object_reference.rb +7 -4
- data/lib/rspec/mocks/proxy.rb +95 -8
- data/lib/rspec/mocks/space.rb +58 -9
- data/lib/rspec/mocks/syntax.rb +128 -210
- data/lib/rspec/mocks/targets.rb +6 -8
- data/lib/rspec/mocks/test_double.rb +16 -25
- data/lib/rspec/mocks/verifying_double.rb +15 -12
- data/lib/rspec/mocks/verifying_message_expecation.rb +3 -3
- data/lib/rspec/mocks/verifying_proxy.rb +23 -17
- data/lib/rspec/mocks/version.rb +3 -1
- metadata +29 -257
- metadata.gz.sig +0 -0
- data/features/README.md +0 -75
- data/features/Scope.md +0 -17
- data/features/Upgrade.md +0 -22
- data/features/argument_matchers/README.md +0 -27
- data/features/argument_matchers/explicit.feature +0 -59
- data/features/argument_matchers/general_matchers.feature +0 -85
- data/features/argument_matchers/type_matchers.feature +0 -26
- data/features/message_expectations/README.md +0 -75
- data/features/message_expectations/allow_any_instance_of.feature +0 -26
- data/features/message_expectations/any_instance.feature +0 -43
- data/features/message_expectations/block_local_expectations.feature.pending +0 -55
- data/features/message_expectations/call_original.feature +0 -23
- data/features/message_expectations/expect_message_using_expect.feature +0 -107
- data/features/message_expectations/expect_message_using_should_receive.feature +0 -118
- data/features/message_expectations/message_chains_using_expect.feature +0 -49
- data/features/message_expectations/receive_counts.feature +0 -209
- data/features/message_expectations/warn_when_expectation_is_set_on_nil.feature +0 -50
- data/features/method_stubs/README.md +0 -77
- data/features/method_stubs/allow_any_instance_of.feature +0 -136
- data/features/method_stubs/as_null_object.feature +0 -40
- data/features/method_stubs/receive_message_chain.feature +0 -51
- data/features/method_stubs/simple_return_value_with_allow.feature +0 -44
- data/features/method_stubs/simple_return_value_with_stub.feature +0 -64
- data/features/method_stubs/stub_implementation.feature +0 -48
- data/features/method_stubs/to_ary.feature +0 -51
- data/features/mutating_constants/README.md +0 -82
- data/features/mutating_constants/hiding_defined_constant.feature +0 -64
- data/features/mutating_constants/stub_defined_constant.feature +0 -78
- data/features/mutating_constants/stub_undefined_constant.feature +0 -50
- data/features/outside_rspec/standalone.feature +0 -33
- data/features/spies/spy_partial_mock_method.feature +0 -34
- data/features/spies/spy_pure_mock_method.feature +0 -76
- data/features/spies/spy_unstubbed_method.feature +0 -18
- data/features/step_definitions/additional_cli_steps.rb +0 -11
- data/features/support/env.rb +0 -22
- data/features/support/rubinius.rb +0 -6
- data/features/test_frameworks/test_unit.feature +0 -54
- data/features/verifying_doubles/README.md +0 -17
- data/features/verifying_doubles/class_doubles.feature +0 -73
- data/features/verifying_doubles/dynamic_classes.feature +0 -71
- data/features/verifying_doubles/instance_doubles.feature +0 -103
- data/features/verifying_doubles/object_doubles.feature +0 -65
- data/features/verifying_doubles/partial_doubles.feature +0 -34
- data/lib/rspec/mocks/extensions/marshal.rb +0 -17
- data/lib/rspec/mocks/framework.rb +0 -35
- data/lib/rspec/mocks/method_signature_verifier.rb +0 -207
- data/lib/rspec/mocks/ruby_features.rb +0 -24
- data/spec/rspec/mocks/and_call_original_spec.rb +0 -268
- data/spec/rspec/mocks/and_return_spec.rb +0 -23
- data/spec/rspec/mocks/and_yield_spec.rb +0 -126
- data/spec/rspec/mocks/any_instance/message_chains_spec.rb +0 -41
- data/spec/rspec/mocks/any_instance_spec.rb +0 -1045
- data/spec/rspec/mocks/array_including_matcher_spec.rb +0 -41
- data/spec/rspec/mocks/at_least_spec.rb +0 -123
- data/spec/rspec/mocks/at_most_spec.rb +0 -90
- data/spec/rspec/mocks/before_all_spec.rb +0 -132
- data/spec/rspec/mocks/block_return_value_spec.rb +0 -70
- data/spec/rspec/mocks/combining_implementation_instructions_spec.rb +0 -204
- data/spec/rspec/mocks/configuration_spec.rb +0 -242
- data/spec/rspec/mocks/double_spec.rb +0 -874
- data/spec/rspec/mocks/example_methods_spec.rb +0 -13
- data/spec/rspec/mocks/extensions/marshal_spec.rb +0 -54
- data/spec/rspec/mocks/failing_argument_matchers_spec.rb +0 -184
- data/spec/rspec/mocks/hash_excluding_matcher_spec.rb +0 -67
- data/spec/rspec/mocks/hash_including_matcher_spec.rb +0 -94
- data/spec/rspec/mocks/instance_method_stasher_spec.rb +0 -74
- data/spec/rspec/mocks/matchers/have_received_spec.rb +0 -347
- data/spec/rspec/mocks/matchers/receive_message_chain_spec.rb +0 -198
- data/spec/rspec/mocks/matchers/receive_messages_spec.rb +0 -140
- data/spec/rspec/mocks/matchers/receive_spec.rb +0 -418
- data/spec/rspec/mocks/method_signature_verifier_spec.rb +0 -272
- data/spec/rspec/mocks/methods_spec.rb +0 -26
- data/spec/rspec/mocks/mock_expectation_error_spec.rb +0 -22
- data/spec/rspec/mocks/mock_ordering_spec.rb +0 -114
- data/spec/rspec/mocks/multiple_return_value_spec.rb +0 -132
- data/spec/rspec/mocks/mutate_const_spec.rb +0 -542
- data/spec/rspec/mocks/nil_expectation_warning_spec.rb +0 -52
- data/spec/rspec/mocks/null_object_mock_spec.rb +0 -133
- data/spec/rspec/mocks/once_counts_spec.rb +0 -52
- data/spec/rspec/mocks/options_hash_spec.rb +0 -35
- data/spec/rspec/mocks/order_group_spec.rb +0 -27
- data/spec/rspec/mocks/partial_double_spec.rb +0 -308
- data/spec/rspec/mocks/partial_double_using_mocks_directly_spec.rb +0 -95
- data/spec/rspec/mocks/passing_argument_matchers_spec.rb +0 -145
- data/spec/rspec/mocks/precise_counts_spec.rb +0 -68
- data/spec/rspec/mocks/record_messages_spec.rb +0 -26
- data/spec/rspec/mocks/serialization_spec.rb +0 -90
- data/spec/rspec/mocks/space_spec.rb +0 -244
- data/spec/rspec/mocks/stash_spec.rb +0 -46
- data/spec/rspec/mocks/stub_chain_spec.rb +0 -168
- data/spec/rspec/mocks/stub_implementation_spec.rb +0 -81
- data/spec/rspec/mocks/stub_spec.rb +0 -327
- data/spec/rspec/mocks/stubbed_message_expectations_spec.rb +0 -58
- data/spec/rspec/mocks/syntax_agnostic_message_matchers_spec.rb +0 -101
- data/spec/rspec/mocks/syntax_spec.rb +0 -19
- data/spec/rspec/mocks/test_double_spec.rb +0 -49
- data/spec/rspec/mocks/to_ary_spec.rb +0 -54
- data/spec/rspec/mocks/twice_counts_spec.rb +0 -66
- data/spec/rspec/mocks/verifying_double_spec.rb +0 -590
- data/spec/rspec/mocks/verifying_message_expecation_spec.rb +0 -69
- data/spec/rspec/mocks_spec.rb +0 -192
- data/spec/spec_helper.rb +0 -121
@@ -1,140 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
shared_examples_for "complains when given blocks" do
|
6
|
-
it "complains if a { } block is given" do
|
7
|
-
expect {
|
8
|
-
target.to receive_messages(:a => 1) { "implementation" }
|
9
|
-
}.to raise_error "Implementation blocks aren't supported with `receive_messages`"
|
10
|
-
end
|
11
|
-
|
12
|
-
it "complains if a do; end; block is given" do
|
13
|
-
expect {
|
14
|
-
target.to receive_messages(:a => 1) do
|
15
|
-
"implementation"
|
16
|
-
end
|
17
|
-
}.to raise_error "Implementation blocks aren't supported with `receive_messages`"
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
shared_examples_for "handles partially mocked objects correctly" do
|
22
|
-
let(:obj) { Struct.new(:a).new('original') }
|
23
|
-
|
24
|
-
it "resets partially mocked objects correctly" do
|
25
|
-
target.to receive_messages(:a => 1, :b => 2)
|
26
|
-
|
27
|
-
expect {
|
28
|
-
reset obj
|
29
|
-
}.to change { obj.a }.from(1).to("original")
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
describe "allow(...).to receive_messages(:a => 1, :b => 2)" do
|
34
|
-
let(:obj) { double "Object" }
|
35
|
-
let(:target) { allow(obj) }
|
36
|
-
|
37
|
-
it "allows the object to respond to multiple messages" do
|
38
|
-
allow(obj).to receive_messages(:a => 1, :b => 2)
|
39
|
-
expect(obj.a).to eq 1
|
40
|
-
expect(obj.b).to eq 2
|
41
|
-
end
|
42
|
-
|
43
|
-
it_behaves_like "complains when given blocks"
|
44
|
-
it_behaves_like "handles partially mocked objects correctly"
|
45
|
-
end
|
46
|
-
|
47
|
-
describe "allow_any_instance_of(...).to receive_messages(:a => 1, :b => 2)" do
|
48
|
-
let(:obj) { Object.new }
|
49
|
-
let(:target) { allow_any_instance_of(Object) }
|
50
|
-
|
51
|
-
it "allows the object to respond to multiple messages" do
|
52
|
-
allow_any_instance_of(Object).to receive_messages(:a => 1, :b => 2)
|
53
|
-
expect(obj.a).to eq 1
|
54
|
-
expect(obj.b).to eq 2
|
55
|
-
end
|
56
|
-
|
57
|
-
it_behaves_like "complains when given blocks"
|
58
|
-
end
|
59
|
-
|
60
|
-
describe "expect(...).to receive_messages(:a => 1, :b => 2)" do
|
61
|
-
let(:obj) { double "Object" }
|
62
|
-
let(:target) { expect(obj) }
|
63
|
-
|
64
|
-
let(:expectation_error) do
|
65
|
-
failure = nil
|
66
|
-
begin
|
67
|
-
verify_all
|
68
|
-
rescue RSpec::Mocks::MockExpectationError => error
|
69
|
-
failure = error
|
70
|
-
end
|
71
|
-
failure
|
72
|
-
end
|
73
|
-
|
74
|
-
it "sets up multiple expectations" do
|
75
|
-
expect(obj).to receive_messages(:a => 1, :b => 2)
|
76
|
-
obj.a
|
77
|
-
expect { verify_all }.to raise_error RSpec::Mocks::MockExpectationError
|
78
|
-
end
|
79
|
-
|
80
|
-
it 'fails with a sensible message' do
|
81
|
-
expect(obj).to receive_messages(:a => 1, :b => 2)
|
82
|
-
obj.b
|
83
|
-
expect(expectation_error.to_s).to eq %Q{(Double "Object").a(no args)\n expected: 1 time with any arguments\n received: 0 times}
|
84
|
-
end
|
85
|
-
|
86
|
-
it 'fails with the correct location' do
|
87
|
-
expect(obj).to receive_messages(:a => 1, :b => 2); line = __LINE__
|
88
|
-
expect(expectation_error.backtrace[0]).to match(/#{__FILE__}:#{line}/)
|
89
|
-
end
|
90
|
-
|
91
|
-
it_behaves_like "complains when given blocks"
|
92
|
-
it_behaves_like "handles partially mocked objects correctly"
|
93
|
-
end
|
94
|
-
|
95
|
-
describe "expect_any_instance_of(...).to receive_messages(:a => 1, :b => 2)" do
|
96
|
-
let(:obj) { Object.new }
|
97
|
-
let(:target) { expect_any_instance_of(Object) }
|
98
|
-
|
99
|
-
it "sets up multiple expectations" do
|
100
|
-
expect_any_instance_of(Object).to receive_messages(:a => 1, :b => 2)
|
101
|
-
obj.a
|
102
|
-
expect { RSpec::Mocks.space.verify_all }.to raise_error RSpec::Mocks::MockExpectationError
|
103
|
-
end
|
104
|
-
|
105
|
-
it_behaves_like "complains when given blocks"
|
106
|
-
end
|
107
|
-
|
108
|
-
describe "negative expectation failure" do
|
109
|
-
let(:obj) { Object.new }
|
110
|
-
|
111
|
-
example "allow(...).to_not receive_messages(:a => 1, :b => 2)" do
|
112
|
-
expect { allow(obj).to_not receive_messages(:a => 1, :b => 2) }.to(
|
113
|
-
raise_error "`allow(...).to_not receive_messages` is not supported "+
|
114
|
-
"since it doesn't really make sense. What would it even mean?"
|
115
|
-
)
|
116
|
-
end
|
117
|
-
|
118
|
-
example "allow_any_instance_of(...).to_not receive_messages(:a => 1, :b => 2)" do
|
119
|
-
expect { allow_any_instance_of(obj).to_not receive_messages(:a => 1, :b => 2) }.to(
|
120
|
-
raise_error "`allow_any_instance_of(...).to_not receive_messages` is not supported "+
|
121
|
-
"since it doesn't really make sense. What would it even mean?"
|
122
|
-
)
|
123
|
-
end
|
124
|
-
|
125
|
-
example "expect(...).to_not receive_messages(:a => 1, :b => 2)" do
|
126
|
-
expect { expect(obj).to_not receive_messages(:a => 1, :b => 2) }.to(
|
127
|
-
raise_error "`expect(...).to_not receive_messages` is not supported "+
|
128
|
-
"since it doesn't really make sense. What would it even mean?"
|
129
|
-
)
|
130
|
-
end
|
131
|
-
|
132
|
-
example "expect_any_instance_of(...).to_not receive_messages(:a => 1, :b => 2)" do
|
133
|
-
expect { expect_any_instance_of(obj).to_not receive_messages(:a => 1, :b => 2) }.to(
|
134
|
-
raise_error "`expect_any_instance_of(...).to_not receive_messages` is not supported "+
|
135
|
-
"since it doesn't really make sense. What would it even mean?"
|
136
|
-
)
|
137
|
-
end
|
138
|
-
end
|
139
|
-
end
|
140
|
-
end
|
@@ -1,418 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
describe Matchers::Receive do
|
6
|
-
include_context "with syntax", :expect
|
7
|
-
|
8
|
-
describe "expectations/allowances on any instance recorders" do
|
9
|
-
include_context "with syntax", [:expect, :should]
|
10
|
-
|
11
|
-
it "warns about allow(Klass.any_instance).to receive..." do
|
12
|
-
expect(RSpec).to receive(:warning).with(/allow.*any_instance.*is probably not what you meant.*allow_any_instance_of.*instead/)
|
13
|
-
allow(Object.any_instance).to receive(:foo)
|
14
|
-
end
|
15
|
-
|
16
|
-
it "includes the correct call site in the allow warning" do
|
17
|
-
expect_warning_with_call_site(__FILE__, __LINE__ + 1)
|
18
|
-
allow(Object.any_instance).to receive(:foo)
|
19
|
-
end
|
20
|
-
|
21
|
-
it "warns about expect(Klass.any_instance).to receive..." do
|
22
|
-
expect(RSpec).to receive(:warning).with(/expect.*any_instance.*is probably not what you meant.*expect_any_instance_of.*instead/)
|
23
|
-
expect(Object.any_instance).to receive(:foo)
|
24
|
-
Object.any_instance.foo
|
25
|
-
end
|
26
|
-
|
27
|
-
it "includes the correct call site in the expect warning" do
|
28
|
-
expect_warning_with_call_site(__FILE__, __LINE__ + 1)
|
29
|
-
expect(Object.any_instance).to receive(:foo)
|
30
|
-
Object.any_instance.foo
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
shared_examples_for "a receive matcher" do |*options|
|
35
|
-
it 'allows the caller to configure how the subject responds' do
|
36
|
-
wrapped.to receive(:foo).and_return(5)
|
37
|
-
expect(receiver.foo).to eq(5)
|
38
|
-
end
|
39
|
-
|
40
|
-
it 'allows the caller to constrain the received arguments' do
|
41
|
-
wrapped.to receive(:foo).with(:a)
|
42
|
-
receiver.foo(:a)
|
43
|
-
|
44
|
-
expect {
|
45
|
-
receiver.foo(:b)
|
46
|
-
}.to raise_error(/received :foo with unexpected arguments/)
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'allows a `do...end` block implementation to be provided' do
|
50
|
-
wrapped.to receive(:foo) do
|
51
|
-
4
|
52
|
-
end
|
53
|
-
|
54
|
-
expect(receiver.foo).to eq(4)
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'allows chaining off a `do...end` block implementation to be provided' do
|
58
|
-
wrapped.to receive(:foo) do
|
59
|
-
4
|
60
|
-
end.and_return(6)
|
61
|
-
|
62
|
-
expect(receiver.foo).to eq(6)
|
63
|
-
end
|
64
|
-
|
65
|
-
it 'allows a `{ ... }` block implementation to be provided' do
|
66
|
-
wrapped.to receive(:foo) { 5 }
|
67
|
-
expect(receiver.foo).to eq(5)
|
68
|
-
end
|
69
|
-
|
70
|
-
it 'gives precedence to a `{ ... }` block when both forms are provided ' +
|
71
|
-
'since that form actually binds to `receive`' do
|
72
|
-
wrapped.to receive(:foo) { :curly } do
|
73
|
-
:do_end
|
74
|
-
end
|
75
|
-
|
76
|
-
expect(receiver.foo).to eq(:curly)
|
77
|
-
end
|
78
|
-
|
79
|
-
it 'does not support other matchers', :unless => options.include?(:allow_other_matchers) do
|
80
|
-
expect {
|
81
|
-
wrapped.to eq(3)
|
82
|
-
}.to raise_error(UnsupportedMatcherError)
|
83
|
-
end
|
84
|
-
|
85
|
-
it 'does not get confused by messages being passed as strings and symbols' do
|
86
|
-
wrapped.to receive(:foo).with(1) { :a }
|
87
|
-
wrapped.to receive("foo").with(2) { :b }
|
88
|
-
|
89
|
-
expect(receiver.foo(1)).to eq(:a)
|
90
|
-
expect(receiver.foo(2)).to eq(:b)
|
91
|
-
end
|
92
|
-
|
93
|
-
it 'allows do...end blocks to be passed to the fluent interface methods without getting a warning' do
|
94
|
-
expect(RSpec).not_to receive(:warning)
|
95
|
-
|
96
|
-
wrapped.to receive(:foo).with(1) do
|
97
|
-
:a
|
98
|
-
end
|
99
|
-
|
100
|
-
expect(receiver.foo(1)).to eq(:a)
|
101
|
-
end
|
102
|
-
|
103
|
-
it 'makes { } blocks trump do...end blocks when passed to a fluent interface method' do
|
104
|
-
wrapped.to receive(:foo).with(1) { :curly } do
|
105
|
-
:do_end
|
106
|
-
end
|
107
|
-
|
108
|
-
expect(receiver.foo(1)).to eq(:curly)
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
shared_examples_for "an expect syntax allowance" do |*options|
|
113
|
-
it_behaves_like "a receive matcher", *options
|
114
|
-
|
115
|
-
it 'does not expect the message to be received' do
|
116
|
-
wrapped.to receive(:foo)
|
117
|
-
expect { verify_all }.not_to raise_error
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
shared_examples_for "an expect syntax negative allowance" do
|
122
|
-
it 'is disabled since this expression is confusing' do
|
123
|
-
expect {
|
124
|
-
wrapped.not_to receive(:foo)
|
125
|
-
}.to raise_error(/not_to receive` is not supported/)
|
126
|
-
|
127
|
-
expect {
|
128
|
-
wrapped.to_not receive(:foo)
|
129
|
-
}.to raise_error(/to_not receive` is not supported/)
|
130
|
-
end
|
131
|
-
end
|
132
|
-
|
133
|
-
shared_examples_for "an expect syntax expectation" do |*options|
|
134
|
-
it_behaves_like "a receive matcher", *options
|
135
|
-
|
136
|
-
it 'sets up a message expectation that passes if the message is received' do
|
137
|
-
wrapped.to receive(:foo)
|
138
|
-
receiver.foo
|
139
|
-
verify_all
|
140
|
-
end
|
141
|
-
|
142
|
-
it 'sets up a message expectation that fails if the message is not received' do
|
143
|
-
wrapped.to receive(:foo)
|
144
|
-
|
145
|
-
expect {
|
146
|
-
verify_all
|
147
|
-
}.to raise_error(RSpec::Mocks::MockExpectationError)
|
148
|
-
end
|
149
|
-
|
150
|
-
it "reports the line number of expectation of unreceived message", :pending => options.include?(:does_not_report_line_num) do
|
151
|
-
expected_error_line = __LINE__; wrapped.to receive(:foo)
|
152
|
-
|
153
|
-
expect {
|
154
|
-
verify_all
|
155
|
-
}.to raise_error { |e|
|
156
|
-
expect(e.backtrace.first).to match(/#{File.basename(__FILE__)}:#{expected_error_line}/)
|
157
|
-
}
|
158
|
-
end
|
159
|
-
end
|
160
|
-
|
161
|
-
shared_examples_for "an expect syntax negative expectation" do
|
162
|
-
it 'sets up a negaive message expectation that passes if the message is not received' do
|
163
|
-
wrapped.not_to receive(:foo)
|
164
|
-
verify_all
|
165
|
-
end
|
166
|
-
|
167
|
-
it 'sets up a negative message expectation that fails if the message is received' do
|
168
|
-
wrapped.not_to receive(:foo)
|
169
|
-
|
170
|
-
expect {
|
171
|
-
receiver.foo
|
172
|
-
}.to raise_error(/expected: 0 times.*received: 1 time/m)
|
173
|
-
end
|
174
|
-
|
175
|
-
it 'supports `to_not` as an alias for `not_to`' do
|
176
|
-
wrapped.to_not receive(:foo)
|
177
|
-
|
178
|
-
expect {
|
179
|
-
receiver.foo
|
180
|
-
}.to raise_error(/expected: 0 times.*received: 1 time/m)
|
181
|
-
end
|
182
|
-
|
183
|
-
it 'allows the caller to constrain the received arguments' do
|
184
|
-
wrapped.not_to receive(:foo).with(:a)
|
185
|
-
def receiver.method_missing(*a); end # a poor man's stub...
|
186
|
-
|
187
|
-
expect {
|
188
|
-
receiver.foo(:b)
|
189
|
-
}.not_to raise_error
|
190
|
-
|
191
|
-
expect {
|
192
|
-
receiver.foo(:a)
|
193
|
-
}.to raise_error(/expected: 0 times.*received: 1 time/m)
|
194
|
-
end
|
195
|
-
|
196
|
-
it 'prevents confusing double-negative expressions involving `never`' do
|
197
|
-
expect {
|
198
|
-
wrapped.not_to receive(:foo).never
|
199
|
-
}.to raise_error(/trying to negate it again/)
|
200
|
-
|
201
|
-
expect {
|
202
|
-
wrapped.to_not receive(:foo).never
|
203
|
-
}.to raise_error(/trying to negate it again/)
|
204
|
-
end
|
205
|
-
end
|
206
|
-
|
207
|
-
shared_examples_for "resets partial mocks cleanly" do
|
208
|
-
let(:klass) { Struct.new(:foo) }
|
209
|
-
let(:object) { klass.new :bar }
|
210
|
-
|
211
|
-
it "removes the method double" do
|
212
|
-
target.to receive(:foo).and_return(:baz)
|
213
|
-
expect { reset object }.to change { object.foo }.from(:baz).to(:bar)
|
214
|
-
end
|
215
|
-
end
|
216
|
-
|
217
|
-
shared_examples_for "resets partial mocks of any instance cleanly" do
|
218
|
-
let(:klass) { Struct.new(:foo) }
|
219
|
-
let(:object) { klass.new :bar }
|
220
|
-
|
221
|
-
it "removes the method double" do
|
222
|
-
target.to receive(:foo).and_return(:baz)
|
223
|
-
expect {
|
224
|
-
verify_all
|
225
|
-
}.to change { object.foo }.from(:baz).to(:bar)
|
226
|
-
end
|
227
|
-
end
|
228
|
-
|
229
|
-
describe "allow(...).to receive" do
|
230
|
-
it_behaves_like "an expect syntax allowance" do
|
231
|
-
let(:receiver) { double }
|
232
|
-
let(:wrapped) { allow(receiver) }
|
233
|
-
end
|
234
|
-
it_behaves_like "resets partial mocks cleanly" do
|
235
|
-
let(:target) { allow(object) }
|
236
|
-
end
|
237
|
-
end
|
238
|
-
|
239
|
-
describe "allow(...).not_to receive" do
|
240
|
-
it_behaves_like "an expect syntax negative allowance" do
|
241
|
-
let(:wrapped) { allow(double) }
|
242
|
-
end
|
243
|
-
end
|
244
|
-
|
245
|
-
describe "allow_any_instance_of(...).to receive" do
|
246
|
-
it_behaves_like "an expect syntax allowance" do
|
247
|
-
let(:klass) { Class.new }
|
248
|
-
let(:wrapped) { allow_any_instance_of(klass) }
|
249
|
-
let(:receiver) { klass.new }
|
250
|
-
end
|
251
|
-
|
252
|
-
it_behaves_like "resets partial mocks of any instance cleanly" do
|
253
|
-
let(:target) { allow_any_instance_of(klass) }
|
254
|
-
end
|
255
|
-
end
|
256
|
-
|
257
|
-
describe "allow_any_instance_of(...).not_to receive" do
|
258
|
-
it_behaves_like "an expect syntax negative allowance" do
|
259
|
-
let(:wrapped) { allow_any_instance_of(Class.new) }
|
260
|
-
end
|
261
|
-
end
|
262
|
-
|
263
|
-
describe "expect(...).to receive" do
|
264
|
-
it_behaves_like "an expect syntax expectation", :allow_other_matchers do
|
265
|
-
let(:receiver) { double }
|
266
|
-
let(:wrapped) { expect(receiver) }
|
267
|
-
end
|
268
|
-
it_behaves_like "resets partial mocks cleanly" do
|
269
|
-
let(:target) { expect(object) }
|
270
|
-
end
|
271
|
-
end
|
272
|
-
|
273
|
-
describe "expect_any_instance_of(...).to receive" do
|
274
|
-
it_behaves_like "an expect syntax expectation", :does_not_report_line_num do
|
275
|
-
let(:klass) { Class.new }
|
276
|
-
let(:wrapped) { expect_any_instance_of(klass) }
|
277
|
-
let(:receiver) { klass.new }
|
278
|
-
end
|
279
|
-
it_behaves_like "resets partial mocks of any instance cleanly" do
|
280
|
-
let(:target) { expect_any_instance_of(klass) }
|
281
|
-
end
|
282
|
-
end
|
283
|
-
|
284
|
-
describe "expect(...).not_to receive" do
|
285
|
-
it_behaves_like "an expect syntax negative expectation" do
|
286
|
-
let(:receiver) { double }
|
287
|
-
let(:wrapped) { expect(receiver) }
|
288
|
-
end
|
289
|
-
end
|
290
|
-
|
291
|
-
describe "expect_any_instance_of(...).not_to receive" do
|
292
|
-
it_behaves_like "an expect syntax negative expectation" do
|
293
|
-
let(:klass) { Class.new }
|
294
|
-
let(:wrapped) { expect_any_instance_of(klass) }
|
295
|
-
let(:receiver) { klass.new }
|
296
|
-
end
|
297
|
-
end
|
298
|
-
|
299
|
-
shared_examples "using rspec-mocks in another test framework" do
|
300
|
-
it 'can use the `expect` syntax' do
|
301
|
-
dbl = double
|
302
|
-
|
303
|
-
framework.new.instance_exec do
|
304
|
-
expect(dbl).to receive(:foo).and_return(3)
|
305
|
-
end
|
306
|
-
|
307
|
-
expect(dbl.foo).to eq(3)
|
308
|
-
end
|
309
|
-
|
310
|
-
it 'expects the method to be called when `expect` is used' do
|
311
|
-
dbl = double
|
312
|
-
|
313
|
-
framework.new.instance_exec do
|
314
|
-
expect(dbl).to receive(:foo)
|
315
|
-
end
|
316
|
-
|
317
|
-
expect { verify dbl }.to raise_error(RSpec::Mocks::MockExpectationError)
|
318
|
-
end
|
319
|
-
|
320
|
-
it 'supports `expect(...).not_to receive`' do
|
321
|
-
dbl = double
|
322
|
-
|
323
|
-
framework.new.instance_exec do
|
324
|
-
expect(dbl).not_to receive(:foo)
|
325
|
-
end
|
326
|
-
|
327
|
-
expect { dbl.foo }.to raise_error(RSpec::Mocks::MockExpectationError)
|
328
|
-
end
|
329
|
-
|
330
|
-
it 'supports `expect(...).to_not receive`' do
|
331
|
-
dbl = double
|
332
|
-
|
333
|
-
framework.new.instance_exec do
|
334
|
-
expect(dbl).to_not receive(:foo)
|
335
|
-
end
|
336
|
-
|
337
|
-
expect { dbl.foo }.to raise_error(RSpec::Mocks::MockExpectationError)
|
338
|
-
end
|
339
|
-
end
|
340
|
-
|
341
|
-
context "when used in a test framework without rspec-expectations" do
|
342
|
-
let(:framework) do
|
343
|
-
Class.new do
|
344
|
-
include RSpec::Mocks::ExampleMethods
|
345
|
-
|
346
|
-
def eq(value)
|
347
|
-
double("MyMatcher")
|
348
|
-
end
|
349
|
-
end
|
350
|
-
end
|
351
|
-
|
352
|
-
it_behaves_like "using rspec-mocks in another test framework"
|
353
|
-
|
354
|
-
it 'cannot use `expect` with another matcher' do
|
355
|
-
expect {
|
356
|
-
framework.new.instance_exec do
|
357
|
-
expect(3).to eq(3)
|
358
|
-
end
|
359
|
-
}.to raise_error(/only the `receive` or `receive_messages` matchers are supported with `expect\(...\).to`/)
|
360
|
-
end
|
361
|
-
|
362
|
-
it 'can toggle the available syntax' do
|
363
|
-
expect(framework.new).to respond_to(:expect)
|
364
|
-
RSpec::Mocks.configuration.syntax = :should
|
365
|
-
expect(framework.new).not_to respond_to(:expect)
|
366
|
-
RSpec::Mocks.configuration.syntax = :expect
|
367
|
-
expect(framework.new).to respond_to(:expect)
|
368
|
-
end
|
369
|
-
|
370
|
-
after { RSpec::Mocks.configuration.syntax = :expect }
|
371
|
-
end
|
372
|
-
|
373
|
-
context "when rspec-expectations is included in the test framework first" do
|
374
|
-
before do
|
375
|
-
# the examples here assume `expect` is define in RSpec::Matchers...
|
376
|
-
expect(RSpec::Matchers.method_defined?(:expect)).to be_truthy
|
377
|
-
end
|
378
|
-
|
379
|
-
let(:framework) do
|
380
|
-
Class.new do
|
381
|
-
include RSpec::Matchers
|
382
|
-
include RSpec::Mocks::ExampleMethods
|
383
|
-
end
|
384
|
-
end
|
385
|
-
|
386
|
-
it_behaves_like "using rspec-mocks in another test framework"
|
387
|
-
|
388
|
-
it 'can use `expect` with any matcher' do
|
389
|
-
framework.new.instance_exec do
|
390
|
-
expect(3).to eq(3)
|
391
|
-
end
|
392
|
-
end
|
393
|
-
end
|
394
|
-
|
395
|
-
context "when rspec-expectations is included in the test framework last" do
|
396
|
-
before do
|
397
|
-
# the examples here assume `expect` is define in RSpec::Matchers...
|
398
|
-
expect(RSpec::Matchers.method_defined?(:expect)).to be_truthy
|
399
|
-
end
|
400
|
-
|
401
|
-
let(:framework) do
|
402
|
-
Class.new do
|
403
|
-
include RSpec::Mocks::ExampleMethods
|
404
|
-
include RSpec::Matchers
|
405
|
-
end
|
406
|
-
end
|
407
|
-
|
408
|
-
it_behaves_like "using rspec-mocks in another test framework"
|
409
|
-
|
410
|
-
it 'can use `expect` with any matcher' do
|
411
|
-
framework.new.instance_exec do
|
412
|
-
expect(3).to eq(3)
|
413
|
-
end
|
414
|
-
end
|
415
|
-
end
|
416
|
-
end
|
417
|
-
end
|
418
|
-
end
|