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,204 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
describe "Combining implementation instructions" do
|
6
|
-
it 'can combine and_yield and and_return' do
|
7
|
-
dbl = double
|
8
|
-
dbl.stub(:foo).and_yield(5).and_return(3)
|
9
|
-
|
10
|
-
expect { |b|
|
11
|
-
expect(dbl.foo(&b)).to eq(3)
|
12
|
-
}.to yield_with_args(5)
|
13
|
-
end
|
14
|
-
|
15
|
-
describe "combining and_yield, a block implementation and and_return" do
|
16
|
-
def verify_combined_implementation
|
17
|
-
dbl = double
|
18
|
-
(yield dbl).and_yield(5).and_return(3)
|
19
|
-
|
20
|
-
expect { |b|
|
21
|
-
expect(dbl.foo(:arg, &b)).to eq(3)
|
22
|
-
}.to yield_with_args(5)
|
23
|
-
|
24
|
-
expect(@block_called).to be_truthy
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'works when passing a block to `stub`' do
|
28
|
-
verify_combined_implementation do |dbl|
|
29
|
-
dbl.stub(:foo) { @block_called = true }
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'works when passing a block to `with`' do
|
34
|
-
verify_combined_implementation do |dbl|
|
35
|
-
dbl.stub(:foo).with(:arg) { @block_called = true }
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'works when passing a block to `exactly`' do
|
40
|
-
verify_combined_implementation do |dbl|
|
41
|
-
dbl.should_receive(:foo).exactly(:once) { @block_called = true }
|
42
|
-
end
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'works when passing a block to `at_least`' do
|
46
|
-
verify_combined_implementation do |dbl|
|
47
|
-
dbl.should_receive(:foo).at_least(:once) { @block_called = true }
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
it 'works when passing a block to `at_most`' do
|
52
|
-
verify_combined_implementation do |dbl|
|
53
|
-
dbl.should_receive(:foo).at_most(:once) { @block_called = true }
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
it 'works when passing a block to `times`' do
|
58
|
-
verify_combined_implementation do |dbl|
|
59
|
-
dbl.should_receive(:foo).exactly(1).times { @block_called = true }
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
it 'works when passing a block to `once`' do
|
64
|
-
verify_combined_implementation do |dbl|
|
65
|
-
dbl.should_receive(:foo).once { @block_called = true }
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
it 'works when passing a block to `twice`' do
|
70
|
-
the_double = nil
|
71
|
-
|
72
|
-
verify_combined_implementation do |dbl|
|
73
|
-
the_double = dbl
|
74
|
-
dbl.should_receive(:foo).twice { @block_called = true }
|
75
|
-
end
|
76
|
-
|
77
|
-
the_double.foo { |a| } # to ensure it is called twice
|
78
|
-
end
|
79
|
-
|
80
|
-
it 'works when passing a block to `ordered`' do
|
81
|
-
verify_combined_implementation do |dbl|
|
82
|
-
dbl.should_receive(:foo).ordered { @block_called = true }
|
83
|
-
end
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
it 'can combine and_yield and and_raise' do
|
88
|
-
dbl = double
|
89
|
-
dbl.stub(:foo).and_yield(5).and_raise("boom")
|
90
|
-
|
91
|
-
expect { |b|
|
92
|
-
expect { dbl.foo(&b) }.to raise_error("boom")
|
93
|
-
}.to yield_with_args(5)
|
94
|
-
end
|
95
|
-
|
96
|
-
it 'can combine and_yield, a block implementation and and_raise' do
|
97
|
-
dbl = double
|
98
|
-
block_called = false
|
99
|
-
dbl.stub(:foo) { block_called = true }.and_yield(5).and_raise("boom")
|
100
|
-
|
101
|
-
expect { |b|
|
102
|
-
expect { dbl.foo(&b) }.to raise_error("boom")
|
103
|
-
}.to yield_with_args(5)
|
104
|
-
|
105
|
-
expect(block_called).to be_truthy
|
106
|
-
end
|
107
|
-
|
108
|
-
it 'can combine and_yield and and_throw' do
|
109
|
-
dbl = double
|
110
|
-
dbl.stub(:foo).and_yield(5).and_throw(:bar)
|
111
|
-
|
112
|
-
expect { |b|
|
113
|
-
expect { dbl.foo(&b) }.to throw_symbol(:bar)
|
114
|
-
}.to yield_with_args(5)
|
115
|
-
end
|
116
|
-
|
117
|
-
it 'can combine and_yield, a block implementation and and_throw' do
|
118
|
-
dbl = double
|
119
|
-
block_called = false
|
120
|
-
dbl.stub(:foo) { block_called = true }.and_yield(5).and_throw(:bar)
|
121
|
-
|
122
|
-
expect { |b|
|
123
|
-
expect { dbl.foo(&b) }.to throw_symbol(:bar)
|
124
|
-
}.to yield_with_args(5)
|
125
|
-
|
126
|
-
expect(block_called).to be_truthy
|
127
|
-
end
|
128
|
-
|
129
|
-
it 'returns `nil` from all terminal actions to discourage further configuration' do
|
130
|
-
expect(double.stub(:foo).and_return(1)).to be_nil
|
131
|
-
expect(double.stub(:foo).and_raise("boom")).to be_nil
|
132
|
-
expect(double.stub(:foo).and_throw(:foo)).to be_nil
|
133
|
-
end
|
134
|
-
|
135
|
-
it 'allows the terminal action to be overriden' do
|
136
|
-
dbl = double
|
137
|
-
stubbed_double = dbl.stub(:foo)
|
138
|
-
|
139
|
-
stubbed_double.and_return(1)
|
140
|
-
expect(dbl.foo).to eq(1)
|
141
|
-
|
142
|
-
stubbed_double.and_return(3)
|
143
|
-
expect(dbl.foo).to eq(3)
|
144
|
-
|
145
|
-
stubbed_double.and_raise("boom")
|
146
|
-
expect { dbl.foo }.to raise_error("boom")
|
147
|
-
|
148
|
-
stubbed_double.and_throw(:bar)
|
149
|
-
expect { dbl.foo }.to throw_symbol(:bar)
|
150
|
-
end
|
151
|
-
|
152
|
-
it 'allows the inner implementation block to be overriden' do
|
153
|
-
allow(RSpec).to receive(:warning)
|
154
|
-
dbl = double
|
155
|
-
stubbed_double = dbl.stub(:foo)
|
156
|
-
|
157
|
-
stubbed_double.with(:arg) { :with_block }
|
158
|
-
expect(dbl.foo(:arg)).to eq(:with_block)
|
159
|
-
|
160
|
-
stubbed_double.at_least(:once) { :at_least_block }
|
161
|
-
expect(dbl.foo(:arg)).to eq(:at_least_block)
|
162
|
-
end
|
163
|
-
|
164
|
-
it 'warns when the inner implementation block is overriden' do
|
165
|
-
expect(RSpec).to receive(:warning).with(/overriding a previous stub implementation of `foo`.*#{__FILE__}:#{__LINE__ + 1}/)
|
166
|
-
double.stub(:foo).with(:arg) { :with_block }.at_least(:once) { :at_least_block }
|
167
|
-
end
|
168
|
-
|
169
|
-
it "does not warn about overriding the stub when `:with` is chained off the block" do
|
170
|
-
expect(RSpec).not_to receive(:warning)
|
171
|
-
|
172
|
-
obj = Object.new
|
173
|
-
stub = obj.stub(:foo) { }
|
174
|
-
stub.with(1)
|
175
|
-
end
|
176
|
-
|
177
|
-
it 'can combine and_call_original, with, and_return' do
|
178
|
-
obj = Struct.new(:value).new('original')
|
179
|
-
obj.stub(:value).and_call_original
|
180
|
-
obj.stub(:value).with(:arg).and_return('value')
|
181
|
-
expect(obj.value).to eq 'original'
|
182
|
-
expect(obj.value(:arg)).to eq 'value'
|
183
|
-
end
|
184
|
-
|
185
|
-
it 'raises an error if `and_call_original` is followed by any other instructions' do
|
186
|
-
allow(RSpec).to receive(:warning)
|
187
|
-
dbl = [1, 2, 3]
|
188
|
-
stubbed = dbl.stub(:size)
|
189
|
-
stubbed.and_call_original
|
190
|
-
|
191
|
-
msg_fragment = /cannot be modified further/
|
192
|
-
|
193
|
-
expect { stubbed.and_yield }.to raise_error(msg_fragment)
|
194
|
-
expect { stubbed.and_return(1) }.to raise_error(msg_fragment)
|
195
|
-
expect { stubbed.and_raise("a") }.to raise_error(msg_fragment)
|
196
|
-
expect { stubbed.and_throw(:bar) }.to raise_error(msg_fragment)
|
197
|
-
|
198
|
-
expect { stubbed.once { } }.to raise_error(msg_fragment)
|
199
|
-
|
200
|
-
expect(dbl.size).to eq(3)
|
201
|
-
end
|
202
|
-
end
|
203
|
-
end
|
204
|
-
end
|
@@ -1,242 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
describe Configuration do
|
6
|
-
let(:config) { Configuration.new }
|
7
|
-
let(:mod_1) { Module.new }
|
8
|
-
let(:mod_2) { Module.new }
|
9
|
-
|
10
|
-
def instance_methods_of(mod)
|
11
|
-
mod_1.instance_methods.map(&:to_sym)
|
12
|
-
end
|
13
|
-
|
14
|
-
it 'adds stub and should_receive to the given modules' do
|
15
|
-
expect(instance_methods_of(mod_1)).not_to include(:stub, :should_receive)
|
16
|
-
expect(instance_methods_of(mod_2)).not_to include(:stub, :should_receive)
|
17
|
-
|
18
|
-
config.add_stub_and_should_receive_to(mod_1, mod_2)
|
19
|
-
|
20
|
-
expect(instance_methods_of(mod_1)).to include(:stub, :should_receive)
|
21
|
-
expect(instance_methods_of(mod_2)).to include(:stub, :should_receive)
|
22
|
-
end
|
23
|
-
|
24
|
-
shared_examples_for "configuring the syntax" do
|
25
|
-
def sandboxed
|
26
|
-
orig_syntax = RSpec::Mocks.configuration.syntax
|
27
|
-
yield
|
28
|
-
ensure
|
29
|
-
configure_syntax(orig_syntax)
|
30
|
-
end
|
31
|
-
|
32
|
-
around(:each) { |ex| sandboxed(&ex) }
|
33
|
-
let(:dbl) { double }
|
34
|
-
let(:should_methods) { [:should_receive, :stub, :should_not_receive] }
|
35
|
-
let(:should_class_methods) { [:any_instance] }
|
36
|
-
let(:expect_methods) { [:receive, :allow, :expect_any_instance_of, :allow_any_instance_of] }
|
37
|
-
|
38
|
-
it 'defaults to enabling both the :should and :expect syntaxes' do
|
39
|
-
expect(dbl).to respond_to(*should_methods)
|
40
|
-
expect(self).to respond_to(*expect_methods)
|
41
|
-
end
|
42
|
-
|
43
|
-
context 'when configured to :expect' do
|
44
|
-
before { configure_syntax :expect }
|
45
|
-
|
46
|
-
it 'removes the should methods from every object' do
|
47
|
-
expect(dbl).not_to respond_to(*should_methods)
|
48
|
-
end
|
49
|
-
|
50
|
-
it 'removes `any_instance` from every class' do
|
51
|
-
expect(Class.new).not_to respond_to(*should_class_methods)
|
52
|
-
end
|
53
|
-
|
54
|
-
it 'adds the expect methods to the example group context' do
|
55
|
-
expect(self).to respond_to(*expect_methods)
|
56
|
-
end
|
57
|
-
|
58
|
-
it 'reports that the syntax is :expect' do
|
59
|
-
expect(configured_syntax).to eq([:expect])
|
60
|
-
end
|
61
|
-
|
62
|
-
it 'is a no-op when configured a second time' do
|
63
|
-
expect(Syntax.default_should_syntax_host).not_to receive(:method_undefined)
|
64
|
-
expect(::RSpec::Mocks::ExampleMethods).not_to receive(:method_added)
|
65
|
-
configure_syntax :expect
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
context 'when configured to :should' do
|
70
|
-
before { configure_syntax :should }
|
71
|
-
|
72
|
-
it 'adds the should methods to every object' do
|
73
|
-
expect(dbl).to respond_to(*should_methods)
|
74
|
-
end
|
75
|
-
|
76
|
-
it 'adds `any_instance` to every class' do
|
77
|
-
expect(Class.new).to respond_to(*should_class_methods)
|
78
|
-
end
|
79
|
-
|
80
|
-
it 'removes the expect methods from the example group context' do
|
81
|
-
expect(self).not_to respond_to(*expect_methods)
|
82
|
-
end
|
83
|
-
|
84
|
-
it 'reports that the syntax is :should' do
|
85
|
-
expect(configured_syntax).to eq([:should])
|
86
|
-
end
|
87
|
-
|
88
|
-
it "does not warn about the should syntax" do
|
89
|
-
RSpec.should_not_receive(:deprecate)
|
90
|
-
Object.new.should_not_receive(:bees)
|
91
|
-
end
|
92
|
-
|
93
|
-
it 'is a no-op when configured a second time' do
|
94
|
-
Syntax.default_should_syntax_host.should_not_receive(:method_added)
|
95
|
-
::RSpec::Mocks::ExampleMethods.should_not_receive(:method_undefined)
|
96
|
-
configure_syntax :should
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
context 'when configured to [:should, :expect]' do
|
101
|
-
before { configure_syntax [:should, :expect] }
|
102
|
-
|
103
|
-
it 'adds the should methods to every object' do
|
104
|
-
expect(dbl).to respond_to(*should_methods)
|
105
|
-
end
|
106
|
-
|
107
|
-
it 'adds `any_instance` to every class' do
|
108
|
-
expect(Class.new).to respond_to(*should_class_methods)
|
109
|
-
end
|
110
|
-
|
111
|
-
it 'adds the expect methods to the example group context' do
|
112
|
-
expect(self).to respond_to(*expect_methods)
|
113
|
-
end
|
114
|
-
|
115
|
-
it 'reports that both syntaxes are enabled' do
|
116
|
-
expect(configured_syntax).to eq([:should, :expect])
|
117
|
-
end
|
118
|
-
|
119
|
-
it "does not warn about the should syntax" do
|
120
|
-
RSpec.should_not_receive(:deprecate)
|
121
|
-
Object.new.should_not_receive(:bees)
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
|
-
context "by default" do
|
126
|
-
before do
|
127
|
-
configure_default_syntax
|
128
|
-
end
|
129
|
-
|
130
|
-
let(:expected_arguments) {
|
131
|
-
[
|
132
|
-
/Using.*without explicitly enabling/,
|
133
|
-
{:replacement=>"the new `:expect` syntax or explicitly enable `:should`"}
|
134
|
-
]
|
135
|
-
}
|
136
|
-
|
137
|
-
it "it warns about should once, regardless of how many times it is called" do
|
138
|
-
expect(RSpec).to receive(:deprecate).with(*expected_arguments)
|
139
|
-
o = Object.new
|
140
|
-
o2 = Object.new
|
141
|
-
o.should_receive(:bees)
|
142
|
-
o2.should_receive(:bees)
|
143
|
-
|
144
|
-
o.bees
|
145
|
-
o2.bees
|
146
|
-
end
|
147
|
-
|
148
|
-
it "warns about should not once, regardless of how many times it is called" do
|
149
|
-
expect(RSpec).to receive(:deprecate).with(*expected_arguments)
|
150
|
-
o = Object.new
|
151
|
-
o2 = Object.new
|
152
|
-
o.should_not_receive(:bees)
|
153
|
-
o2.should_not_receive(:bees)
|
154
|
-
end
|
155
|
-
|
156
|
-
it "warns about stubbing once, regardless of how many times it is called" do
|
157
|
-
expect(RSpec).to receive(:deprecate).with(*expected_arguments)
|
158
|
-
o = Object.new
|
159
|
-
o2 = Object.new
|
160
|
-
|
161
|
-
o.stub(:faces)
|
162
|
-
o2.stub(:faces)
|
163
|
-
end
|
164
|
-
|
165
|
-
it "warns about unstubbing once, regardless of how many times it is called" do
|
166
|
-
expect(RSpec).to receive(:deprecate).with(/Using.*without explicitly enabling/,
|
167
|
-
{:replacement => "`allow(...).to_receive(...).and_call_original` or explicitly enable `:should`"})
|
168
|
-
o = Object.new
|
169
|
-
o2 = Object.new
|
170
|
-
|
171
|
-
allow(o).to receive(:faces)
|
172
|
-
allow(o2).to receive(:faces)
|
173
|
-
|
174
|
-
o.unstub(:faces)
|
175
|
-
o2.unstub(:faces)
|
176
|
-
end
|
177
|
-
|
178
|
-
|
179
|
-
it "doesn't warn about stubbing after a reset and setting should" do
|
180
|
-
expect(RSpec).not_to receive(:deprecate)
|
181
|
-
RSpec::Mocks.configuration.reset_syntaxes_to_default
|
182
|
-
RSpec::Mocks.configuration.syntax = :should
|
183
|
-
o = Object.new
|
184
|
-
o2 = Object.new
|
185
|
-
o.stub(:faces)
|
186
|
-
o2.stub(:faces)
|
187
|
-
end
|
188
|
-
|
189
|
-
it "includes the call site in the deprecation warning" do
|
190
|
-
obj = Object.new
|
191
|
-
expect_deprecation_with_call_site(__FILE__, __LINE__ + 1)
|
192
|
-
obj.stub(:faces)
|
193
|
-
end
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
describe "configuring rspec-mocks directly" do
|
198
|
-
it_behaves_like "configuring the syntax" do
|
199
|
-
def configure_syntax(syntax)
|
200
|
-
RSpec::Mocks.configuration.syntax = syntax
|
201
|
-
end
|
202
|
-
|
203
|
-
def configured_syntax
|
204
|
-
RSpec::Mocks.configuration.syntax
|
205
|
-
end
|
206
|
-
|
207
|
-
def configure_default_syntax
|
208
|
-
RSpec::Mocks.configuration.reset_syntaxes_to_default
|
209
|
-
end
|
210
|
-
end
|
211
|
-
end
|
212
|
-
|
213
|
-
describe "configuring using the rspec-core config API" do
|
214
|
-
it_behaves_like "configuring the syntax" do
|
215
|
-
def configure_syntax(syntax)
|
216
|
-
RSpec.configure do |rspec|
|
217
|
-
rspec.mock_with :rspec do |c|
|
218
|
-
c.syntax = syntax
|
219
|
-
end
|
220
|
-
end
|
221
|
-
end
|
222
|
-
|
223
|
-
def configured_syntax
|
224
|
-
RSpec.configure do |rspec|
|
225
|
-
rspec.mock_with :rspec do |c|
|
226
|
-
return c.syntax
|
227
|
-
end
|
228
|
-
end
|
229
|
-
end
|
230
|
-
|
231
|
-
def configure_default_syntax
|
232
|
-
RSpec.configure do |rspec|
|
233
|
-
rspec.mock_with :rspec do |c|
|
234
|
-
c.reset_syntaxes_to_default
|
235
|
-
end
|
236
|
-
end
|
237
|
-
end
|
238
|
-
end
|
239
|
-
end
|
240
|
-
end
|
241
|
-
end
|
242
|
-
end
|
@@ -1,874 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
describe Double do
|
6
|
-
before(:each) { @double = double("test double") }
|
7
|
-
after(:each) { reset @double }
|
8
|
-
|
9
|
-
it "has method_missing as private" do
|
10
|
-
expect(RSpec::Mocks::Double.private_instance_methods).to include_method(:method_missing)
|
11
|
-
end
|
12
|
-
|
13
|
-
it "does not respond_to? method_missing (because it's private)" do
|
14
|
-
expect(RSpec::Mocks::Double.new).not_to respond_to(:method_missing)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "uses 'Double' in failure messages" do
|
18
|
-
double = double('name')
|
19
|
-
expect { double.foo }.to raise_error(/Double "name" received/)
|
20
|
-
end
|
21
|
-
|
22
|
-
it "hides internals in its inspect representation" do
|
23
|
-
m = double('cup')
|
24
|
-
expect(m.inspect).to match(/#<RSpec::Mocks::Double:0x[a-f0-9.]+ @name="cup">/)
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'does not blow up when resetting standard object methods' do
|
28
|
-
dbl = double(:tainted? => true)
|
29
|
-
expect(dbl.tainted?).to eq(true)
|
30
|
-
expect { reset dbl }.not_to raise_error
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'does not get string vs symbol messages confused' do
|
34
|
-
dbl = double("foo" => 1)
|
35
|
-
allow(dbl).to receive(:foo).and_return(2)
|
36
|
-
expect(dbl.foo).to eq(2)
|
37
|
-
expect { reset dbl }.not_to raise_error
|
38
|
-
end
|
39
|
-
|
40
|
-
it 'allows string representation of methods in constructor' do
|
41
|
-
dbl = double('foo' => 1)
|
42
|
-
expect(dbl.foo).to eq(1)
|
43
|
-
end
|
44
|
-
|
45
|
-
it 'allows setter methods to be stubbed' do
|
46
|
-
dbl = double('foo=' => 1)
|
47
|
-
|
48
|
-
# Note the specified return value is thrown away. This is a Ruby semantics
|
49
|
-
# thing. You cannot change the return value of assignment.
|
50
|
-
expect(dbl.foo = "bar").to eq("bar")
|
51
|
-
end
|
52
|
-
|
53
|
-
it 'allows `send` to be stubbed' do
|
54
|
-
dbl = double
|
55
|
-
allow(dbl).to receive(:send).and_return("received")
|
56
|
-
expect(dbl.send(:some_msg)).to eq("received")
|
57
|
-
end
|
58
|
-
|
59
|
-
context "after it has been torn down" do
|
60
|
-
let(:dbl) { double }
|
61
|
-
|
62
|
-
before do
|
63
|
-
expect(dbl).to receive(:foo).at_least(:once)
|
64
|
-
allow(dbl).to receive(:bar)
|
65
|
-
dbl.foo
|
66
|
-
|
67
|
-
RSpec::Mocks.verify
|
68
|
-
RSpec::Mocks.teardown
|
69
|
-
RSpec::Mocks.setup
|
70
|
-
end
|
71
|
-
|
72
|
-
it 'disallows previously mocked methods' do
|
73
|
-
expect { dbl.foo }.to raise_error(ExpiredTestDoubleError)
|
74
|
-
end
|
75
|
-
|
76
|
-
it 'disallows previously stubbed methods' do
|
77
|
-
expect { dbl.bar }.to raise_error(ExpiredTestDoubleError)
|
78
|
-
end
|
79
|
-
|
80
|
-
it 'disallows stubbing new methods (with receive)' do
|
81
|
-
expect {
|
82
|
-
allow(dbl).to receive(:bazz)
|
83
|
-
}.to raise_error(ExpiredTestDoubleError)
|
84
|
-
end
|
85
|
-
|
86
|
-
it 'disallows stubbing new methods (with receive_messages)' do
|
87
|
-
expect {
|
88
|
-
allow(dbl).to receive_messages(:bazz => 3)
|
89
|
-
}.to raise_error(ExpiredTestDoubleError)
|
90
|
-
end
|
91
|
-
|
92
|
-
it 'disallows stubbing new message chains' do
|
93
|
-
expect {
|
94
|
-
allow(dbl).to receive_message_chain(:bazz, :bam, :goo)
|
95
|
-
}.to raise_error(ExpiredTestDoubleError)
|
96
|
-
end
|
97
|
-
|
98
|
-
it 'disallows mocking new methods' do
|
99
|
-
expect {
|
100
|
-
expect(dbl).to receive(:bazz)
|
101
|
-
}.to raise_error(ExpiredTestDoubleError)
|
102
|
-
end
|
103
|
-
|
104
|
-
it 'disallows being turned into a null object' do
|
105
|
-
expect { dbl.as_null_object }.to raise_error(ExpiredTestDoubleError)
|
106
|
-
end
|
107
|
-
|
108
|
-
it 'disallows being checked for nullness' do
|
109
|
-
expect { dbl.null_object? }.to raise_error(ExpiredTestDoubleError)
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
it "reports line number of expectation of unreceived message" do
|
114
|
-
expected_error_line = __LINE__; @double.should_receive(:wont_happen).with("x", 3)
|
115
|
-
begin
|
116
|
-
verify @double
|
117
|
-
violated
|
118
|
-
rescue RSpec::Mocks::MockExpectationError => e
|
119
|
-
# NOTE - this regexp ended w/ $, but jruby adds extra info at the end of the line
|
120
|
-
expect(e.backtrace[0]).to match(/#{File.basename(__FILE__)}:#{expected_error_line}/)
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
it "reports line number of expectation of unreceived message after #should_receive after similar stub" do
|
125
|
-
@double.stub(:wont_happen)
|
126
|
-
expected_error_line = __LINE__; @double.should_receive(:wont_happen).with("x", 3)
|
127
|
-
begin
|
128
|
-
verify @double
|
129
|
-
violated
|
130
|
-
rescue RSpec::Mocks::MockExpectationError => e
|
131
|
-
# NOTE - this regexp ended w/ $, but jruby adds extra info at the end of the line
|
132
|
-
expect(e.backtrace[0]).to match(/#{File.basename(__FILE__)}:#{expected_error_line}/)
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
it "passes when not receiving message specified as not to be received" do
|
137
|
-
@double.should_not_receive(:not_expected)
|
138
|
-
verify @double
|
139
|
-
end
|
140
|
-
|
141
|
-
it "prevents confusing double-negative expressions involving `never`" do
|
142
|
-
expect {
|
143
|
-
@double.should_not_receive(:not_expected).never
|
144
|
-
}.to raise_error(/trying to negate it again/)
|
145
|
-
end
|
146
|
-
|
147
|
-
it "warns when `and_return` is called on a negative expectation" do
|
148
|
-
expect {
|
149
|
-
@double.should_not_receive(:do_something).and_return(1)
|
150
|
-
}.to raise_error(/not supported/)
|
151
|
-
|
152
|
-
expect {
|
153
|
-
expect(@double).not_to receive(:do_something).and_return(1)
|
154
|
-
}.to raise_error(/not supported/)
|
155
|
-
|
156
|
-
expect {
|
157
|
-
expect(@double).to receive(:do_something).never.and_return(1)
|
158
|
-
}.to raise_error(/not supported/)
|
159
|
-
end
|
160
|
-
|
161
|
-
it "passes when receiving message specified as not to be received with different args" do
|
162
|
-
@double.should_not_receive(:message).with("unwanted text")
|
163
|
-
@double.should_receive(:message).with("other text")
|
164
|
-
@double.message "other text"
|
165
|
-
verify @double
|
166
|
-
end
|
167
|
-
|
168
|
-
it "fails when receiving message specified as not to be received" do
|
169
|
-
@double.should_not_receive(:not_expected)
|
170
|
-
expect {
|
171
|
-
@double.not_expected
|
172
|
-
violated
|
173
|
-
}.to raise_error(
|
174
|
-
RSpec::Mocks::MockExpectationError,
|
175
|
-
%Q|(Double "test double").not_expected(no args)\n expected: 0 times with any arguments\n received: 1 time|
|
176
|
-
)
|
177
|
-
end
|
178
|
-
|
179
|
-
it "fails when receiving message specified as not to be received with args" do
|
180
|
-
@double.should_not_receive(:not_expected).with("unexpected text")
|
181
|
-
expect {
|
182
|
-
@double.not_expected("unexpected text")
|
183
|
-
violated
|
184
|
-
}.to raise_error(
|
185
|
-
RSpec::Mocks::MockExpectationError,
|
186
|
-
%Q|(Double "test double").not_expected("unexpected text")\n expected: 0 times with arguments: ("unexpected text")\n received: 1 time with arguments: ("unexpected text")|
|
187
|
-
)
|
188
|
-
end
|
189
|
-
|
190
|
-
it "fails when array arguments do not match" do
|
191
|
-
@double.should_not_receive(:not_expected).with(["do not want"])
|
192
|
-
expect {
|
193
|
-
@double.not_expected(["do not want"])
|
194
|
-
violated
|
195
|
-
}.to raise_error(
|
196
|
-
RSpec::Mocks::MockExpectationError,
|
197
|
-
%Q|(Double "test double").not_expected(["do not want"])\n expected: 0 times with arguments: (["do not want"])\n received: 1 time with arguments: (["do not want"])|
|
198
|
-
)
|
199
|
-
end
|
200
|
-
|
201
|
-
context "when specifying a message should not be received with specific args" do
|
202
|
-
context "using `should_not_receive`" do
|
203
|
-
it 'passes when receiving the message with different args' do
|
204
|
-
@double.should_not_receive(:not_expected).with("unexpected text")
|
205
|
-
@double.not_expected "really unexpected text"
|
206
|
-
verify @double
|
207
|
-
end
|
208
|
-
end
|
209
|
-
|
210
|
-
context "using `should_receive().never`" do
|
211
|
-
it 'passes when receiving the message with different args' do
|
212
|
-
@double.should_receive(:not_expected).with("unexpected text").never
|
213
|
-
@double.not_expected "really unexpected text"
|
214
|
-
verify @double
|
215
|
-
end
|
216
|
-
end
|
217
|
-
end
|
218
|
-
|
219
|
-
it 'does not get confused when `should_not_received` is used with a string and symbol message' do
|
220
|
-
@double.stub(:foo) { 3 }
|
221
|
-
@double.should_not_receive(:foo).with(1)
|
222
|
-
@double.should_not_receive("foo").with(2)
|
223
|
-
|
224
|
-
expect(@double.foo).to eq(3)
|
225
|
-
verify @double
|
226
|
-
end
|
227
|
-
|
228
|
-
it 'does not get confused when `should_received` is used with a string and symbol message' do
|
229
|
-
@double.should_receive(:foo).with(1)
|
230
|
-
@double.should_receive("foo").with(2)
|
231
|
-
|
232
|
-
@double.foo(1)
|
233
|
-
@double.foo(2)
|
234
|
-
|
235
|
-
verify @double
|
236
|
-
end
|
237
|
-
|
238
|
-
it "allows parameter as return value" do
|
239
|
-
@double.should_receive(:something).with("a","b","c").and_return("booh")
|
240
|
-
expect(@double.something("a","b","c")).to eq "booh"
|
241
|
-
verify @double
|
242
|
-
end
|
243
|
-
|
244
|
-
it "returns the previously stubbed value if no return value is set" do
|
245
|
-
@double.stub(:something).with("a","b","c").and_return(:stubbed_value)
|
246
|
-
@double.should_receive(:something).with("a","b","c")
|
247
|
-
expect(@double.something("a","b","c")).to eq :stubbed_value
|
248
|
-
verify @double
|
249
|
-
end
|
250
|
-
|
251
|
-
it "returns nil if no return value is set and there is no previously stubbed value" do
|
252
|
-
@double.should_receive(:something).with("a","b","c")
|
253
|
-
expect(@double.something("a","b","c")).to be_nil
|
254
|
-
verify @double
|
255
|
-
end
|
256
|
-
|
257
|
-
it "raises exception if args don't match when method called" do
|
258
|
-
@double.should_receive(:something).with("a","b","c").and_return("booh")
|
259
|
-
expect {
|
260
|
-
@double.something("a","d","c")
|
261
|
-
violated
|
262
|
-
}.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (\"a\", \"b\", \"c\")\n got: (\"a\", \"d\", \"c\")")
|
263
|
-
end
|
264
|
-
|
265
|
-
describe "even when a similar expectation with different arguments exist" do
|
266
|
-
it "raises exception if args don't match when method called, correctly reporting the offending arguments" do
|
267
|
-
@double.should_receive(:something).with("a","b","c").once
|
268
|
-
@double.should_receive(:something).with("z","x","c").once
|
269
|
-
expect {
|
270
|
-
@double.something("a","b","c")
|
271
|
-
@double.something("z","x","g")
|
272
|
-
}.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (\"z\", \"x\", \"c\")\n got: (\"z\", \"x\", \"g\")")
|
273
|
-
end
|
274
|
-
end
|
275
|
-
|
276
|
-
it "raises exception if args don't match when method called even when the method is stubbed" do
|
277
|
-
@double.stub(:something)
|
278
|
-
@double.should_receive(:something).with("a","b","c")
|
279
|
-
expect {
|
280
|
-
@double.something("a","d","c")
|
281
|
-
verify @double
|
282
|
-
}.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (\"a\", \"b\", \"c\")\n got: (\"a\", \"d\", \"c\")")
|
283
|
-
end
|
284
|
-
|
285
|
-
it "raises exception if args don't match when method called even when using null_object" do
|
286
|
-
@double = double("test double").as_null_object
|
287
|
-
@double.should_receive(:something).with("a","b","c")
|
288
|
-
expect {
|
289
|
-
@double.something("a","d","c")
|
290
|
-
verify @double
|
291
|
-
}.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (\"a\", \"b\", \"c\")\n got: (\"a\", \"d\", \"c\")")
|
292
|
-
end
|
293
|
-
|
294
|
-
describe 'with a method that has a default argument' do
|
295
|
-
it "raises an exception if the arguments don't match when the method is called, correctly reporting the offending arguments" do
|
296
|
-
def @double.method_with_default_argument(arg={}); end
|
297
|
-
@double.should_receive(:method_with_default_argument).with({})
|
298
|
-
|
299
|
-
expect {
|
300
|
-
@double.method_with_default_argument(nil)
|
301
|
-
verify @double
|
302
|
-
}.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :method_with_default_argument with unexpected arguments\n expected: ({})\n got: (nil)")
|
303
|
-
end
|
304
|
-
end
|
305
|
-
|
306
|
-
it "fails if unexpected method called" do
|
307
|
-
expect {
|
308
|
-
@double.something("a","b","c")
|
309
|
-
violated
|
310
|
-
}.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received unexpected message :something with (\"a\", \"b\", \"c\")")
|
311
|
-
end
|
312
|
-
|
313
|
-
it "uses block for expectation if provided" do
|
314
|
-
@double.should_receive(:something) do | a, b |
|
315
|
-
expect(a).to eq "a"
|
316
|
-
expect(b).to eq "b"
|
317
|
-
"booh"
|
318
|
-
end
|
319
|
-
expect(@double.something("a", "b")).to eq "booh"
|
320
|
-
verify @double
|
321
|
-
end
|
322
|
-
|
323
|
-
it "fails if expectation block fails" do
|
324
|
-
@double.should_receive(:something) do |bool|
|
325
|
-
expect(bool).to be_truthy
|
326
|
-
end
|
327
|
-
|
328
|
-
expect {
|
329
|
-
@double.something false
|
330
|
-
}.to raise_error(RSpec::Expectations::ExpectationNotMetError)
|
331
|
-
end
|
332
|
-
|
333
|
-
it "is wrappable in an array" do
|
334
|
-
with_isolated_stderr do
|
335
|
-
expect( Array(@double) ).to eq([@double])
|
336
|
-
end
|
337
|
-
end
|
338
|
-
|
339
|
-
it "is wrappable in an array when a null object" do
|
340
|
-
with_isolated_stderr do
|
341
|
-
expect( Array(@double.as_null_object) ).to eq [@double]
|
342
|
-
end
|
343
|
-
end
|
344
|
-
|
345
|
-
it "responds to to_ary as a null object" do
|
346
|
-
expect(@double.as_null_object.to_ary).to eq nil
|
347
|
-
end
|
348
|
-
|
349
|
-
it "responds to to_a as a null object" do
|
350
|
-
if RUBY_VERSION.to_f > 1.8
|
351
|
-
expect(@double.as_null_object.to_a).to eq nil
|
352
|
-
else
|
353
|
-
with_isolated_stderr do
|
354
|
-
expect(@double.as_null_object.to_a).to eq [@double]
|
355
|
-
end
|
356
|
-
end
|
357
|
-
end
|
358
|
-
|
359
|
-
it "passes proc to expectation block without an argument" do
|
360
|
-
@double.should_receive(:foo) { |&block| expect(block.call).to eq(:bar) }
|
361
|
-
@double.foo { :bar }
|
362
|
-
end
|
363
|
-
|
364
|
-
it "passes proc to expectation block with an argument" do
|
365
|
-
@double.should_receive(:foo) { |arg, &block| expect(block.call).to eq(:bar) }
|
366
|
-
@double.foo(:arg) { :bar }
|
367
|
-
end
|
368
|
-
|
369
|
-
it "passes proc to stub block without an argurment" do
|
370
|
-
@double.stub(:foo) { |&block| expect(block.call).to eq(:bar) }
|
371
|
-
@double.foo { :bar }
|
372
|
-
end
|
373
|
-
|
374
|
-
it "passes proc to stub block with an argument" do
|
375
|
-
@double.stub(:foo) { |arg, &block| expect(block.call).to eq(:bar) }
|
376
|
-
@double.foo(:arg) { :bar }
|
377
|
-
end
|
378
|
-
|
379
|
-
it "fails right away when method defined as never is received" do
|
380
|
-
@double.should_receive(:not_expected).never
|
381
|
-
expect { @double.not_expected }.
|
382
|
-
to raise_error(RSpec::Mocks::MockExpectationError,
|
383
|
-
%Q|(Double "test double").not_expected(no args)\n expected: 0 times with any arguments\n received: 1 time|
|
384
|
-
)
|
385
|
-
end
|
386
|
-
|
387
|
-
it "raises RuntimeError by default" do
|
388
|
-
@double.should_receive(:something).and_raise
|
389
|
-
expect { @double.something }.to raise_error(RuntimeError)
|
390
|
-
end
|
391
|
-
|
392
|
-
it "raises RuntimeError with a message by default" do
|
393
|
-
@double.should_receive(:something).and_raise("error message")
|
394
|
-
expect { @double.something }.to raise_error(RuntimeError, "error message")
|
395
|
-
end
|
396
|
-
|
397
|
-
it "raises an exception of a given type without an error message" do
|
398
|
-
@double.should_receive(:something).and_raise(StandardError)
|
399
|
-
expect { @double.something }.to raise_error(StandardError)
|
400
|
-
end
|
401
|
-
|
402
|
-
it "raises an exception of a given type with a message" do
|
403
|
-
@double.should_receive(:something).and_raise(RuntimeError, "error message")
|
404
|
-
expect { @double.something }.to raise_error(RuntimeError, "error message")
|
405
|
-
end
|
406
|
-
|
407
|
-
it "raises a given instance of an exception" do
|
408
|
-
@double.should_receive(:something).and_raise(RuntimeError.new("error message"))
|
409
|
-
expect { @double.something }.to raise_error(RuntimeError, "error message")
|
410
|
-
end
|
411
|
-
|
412
|
-
class OutOfGas < StandardError
|
413
|
-
attr_reader :amount, :units
|
414
|
-
def initialize(amount, units)
|
415
|
-
@amount = amount
|
416
|
-
@units = units
|
417
|
-
end
|
418
|
-
end
|
419
|
-
|
420
|
-
it "raises a given instance of an exception with arguments other than the standard 'message'" do
|
421
|
-
@double.should_receive(:something).and_raise(OutOfGas.new(2, :oz))
|
422
|
-
|
423
|
-
begin
|
424
|
-
@double.something
|
425
|
-
fail "OutOfGas was not raised"
|
426
|
-
rescue OutOfGas => e
|
427
|
-
expect(e.amount).to eq 2
|
428
|
-
expect(e.units).to eq :oz
|
429
|
-
end
|
430
|
-
end
|
431
|
-
|
432
|
-
it "does not raise when told to if args dont match" do
|
433
|
-
@double.should_receive(:something).with(2).and_raise(RuntimeError)
|
434
|
-
expect {
|
435
|
-
@double.something 1
|
436
|
-
}.to raise_error(RSpec::Mocks::MockExpectationError)
|
437
|
-
end
|
438
|
-
|
439
|
-
it "throws when told to" do
|
440
|
-
@double.should_receive(:something).and_throw(:blech)
|
441
|
-
expect {
|
442
|
-
@double.something
|
443
|
-
}.to throw_symbol(:blech)
|
444
|
-
end
|
445
|
-
|
446
|
-
it "ignores args on any args" do
|
447
|
-
@double.should_receive(:something).at_least(:once).with(any_args)
|
448
|
-
@double.something
|
449
|
-
@double.something 1
|
450
|
-
@double.something "a", 2
|
451
|
-
@double.something [], {}, "joe", 7
|
452
|
-
verify @double
|
453
|
-
end
|
454
|
-
|
455
|
-
it "fails on no args if any args received" do
|
456
|
-
@double.should_receive(:something).with(no_args())
|
457
|
-
expect {
|
458
|
-
@double.something 1
|
459
|
-
}.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (no args)\n got: (1)")
|
460
|
-
end
|
461
|
-
|
462
|
-
it "fails when args are expected but none are received" do
|
463
|
-
@double.should_receive(:something).with(1)
|
464
|
-
expect {
|
465
|
-
@double.something
|
466
|
-
}.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" received :something with unexpected arguments\n expected: (1)\n got: (no args)")
|
467
|
-
end
|
468
|
-
|
469
|
-
it "returns value from block by default" do
|
470
|
-
@double.stub(:method_that_yields).and_yield
|
471
|
-
value = @double.method_that_yields { :returned_obj }
|
472
|
-
expect(value).to eq :returned_obj
|
473
|
-
verify @double
|
474
|
-
end
|
475
|
-
|
476
|
-
it "yields 0 args to blocks that take a variable number of arguments" do
|
477
|
-
@double.should_receive(:yield_back).with(no_args()).once.and_yield
|
478
|
-
a = nil
|
479
|
-
@double.yield_back {|*x| a = x}
|
480
|
-
expect(a).to eq []
|
481
|
-
verify @double
|
482
|
-
end
|
483
|
-
|
484
|
-
it "yields 0 args multiple times to blocks that take a variable number of arguments" do
|
485
|
-
@double.should_receive(:yield_back).once.with(no_args()).once.and_yield.
|
486
|
-
and_yield
|
487
|
-
b = []
|
488
|
-
@double.yield_back {|*a| b << a}
|
489
|
-
expect(b).to eq [ [], [] ]
|
490
|
-
verify @double
|
491
|
-
end
|
492
|
-
|
493
|
-
it "yields one arg to blocks that take a variable number of arguments" do
|
494
|
-
@double.should_receive(:yield_back).with(no_args()).once.and_yield(99)
|
495
|
-
a = nil
|
496
|
-
@double.yield_back {|*x| a = x}
|
497
|
-
expect(a).to eq [99]
|
498
|
-
verify @double
|
499
|
-
end
|
500
|
-
|
501
|
-
it "yields one arg 3 times consecutively to blocks that take a variable number of arguments" do
|
502
|
-
@double.should_receive(:yield_back).once.with(no_args()).once.and_yield(99).
|
503
|
-
and_yield(43).
|
504
|
-
and_yield("something fruity")
|
505
|
-
b = []
|
506
|
-
@double.yield_back {|*a| b << a}
|
507
|
-
expect(b).to eq [[99], [43], ["something fruity"]]
|
508
|
-
verify @double
|
509
|
-
end
|
510
|
-
|
511
|
-
it "yields many args to blocks that take a variable number of arguments" do
|
512
|
-
@double.should_receive(:yield_back).with(no_args()).once.and_yield(99, 27, "go")
|
513
|
-
a = nil
|
514
|
-
@double.yield_back {|*x| a = x}
|
515
|
-
expect(a).to eq [99, 27, "go"]
|
516
|
-
verify @double
|
517
|
-
end
|
518
|
-
|
519
|
-
it "yields many args 3 times consecutively to blocks that take a variable number of arguments" do
|
520
|
-
@double.should_receive(:yield_back).once.with(no_args()).once.and_yield(99, :green, "go").
|
521
|
-
and_yield("wait", :amber).
|
522
|
-
and_yield("stop", 12, :red)
|
523
|
-
b = []
|
524
|
-
@double.yield_back {|*a| b << a}
|
525
|
-
expect(b).to eq [[99, :green, "go"], ["wait", :amber], ["stop", 12, :red]]
|
526
|
-
verify @double
|
527
|
-
end
|
528
|
-
|
529
|
-
it "yields single value" do
|
530
|
-
@double.should_receive(:yield_back).with(no_args()).once.and_yield(99)
|
531
|
-
a = nil
|
532
|
-
@double.yield_back {|x| a = x}
|
533
|
-
expect(a).to eq 99
|
534
|
-
verify @double
|
535
|
-
end
|
536
|
-
|
537
|
-
it "yields single value 3 times consecutively" do
|
538
|
-
@double.should_receive(:yield_back).once.with(no_args()).once.and_yield(99).
|
539
|
-
and_yield(43).
|
540
|
-
and_yield("something fruity")
|
541
|
-
b = []
|
542
|
-
@double.yield_back {|a| b << a}
|
543
|
-
expect(b).to eq [99, 43, "something fruity"]
|
544
|
-
verify @double
|
545
|
-
end
|
546
|
-
|
547
|
-
it "yields two values" do
|
548
|
-
@double.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
|
549
|
-
a, b = nil
|
550
|
-
@double.yield_back {|x,y| a=x; b=y}
|
551
|
-
expect(a).to eq 'wha'
|
552
|
-
expect(b).to eq 'zup'
|
553
|
-
verify @double
|
554
|
-
end
|
555
|
-
|
556
|
-
it "yields two values 3 times consecutively" do
|
557
|
-
@double.should_receive(:yield_back).once.with(no_args()).once.and_yield('wha', 'zup').
|
558
|
-
and_yield('not', 'down').
|
559
|
-
and_yield(14, 65)
|
560
|
-
c = []
|
561
|
-
@double.yield_back {|a,b| c << [a, b]}
|
562
|
-
expect(c).to eq [['wha', 'zup'], ['not', 'down'], [14, 65]]
|
563
|
-
verify @double
|
564
|
-
end
|
565
|
-
|
566
|
-
it "fails when calling yielding method with wrong arity" do
|
567
|
-
@double.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
|
568
|
-
expect {
|
569
|
-
@double.yield_back {|a|}
|
570
|
-
}.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" yielded |\"wha\", \"zup\"| to block with arity of 1")
|
571
|
-
end
|
572
|
-
|
573
|
-
if kw_args_supported?
|
574
|
-
it 'fails when calling yielding method with invalid kw args' do
|
575
|
-
@double.should_receive(:yield_back).and_yield(:x => 1, :y => 2)
|
576
|
-
expect {
|
577
|
-
eval("@double.yield_back { |x: 1| }")
|
578
|
-
}.to raise_error(RSpec::Mocks::MockExpectationError,
|
579
|
-
'Double "test double" yielded |{:x=>1, :y=>2}| to block with optional keyword args (:x)')
|
580
|
-
end
|
581
|
-
end
|
582
|
-
|
583
|
-
it "fails when calling yielding method consecutively with wrong arity" do
|
584
|
-
@double.should_receive(:yield_back).once.with(no_args()).once.and_yield('wha', 'zup').
|
585
|
-
and_yield('down').
|
586
|
-
and_yield(14, 65)
|
587
|
-
expect {
|
588
|
-
c = []
|
589
|
-
@double.yield_back {|a,b| c << [a, b]}
|
590
|
-
}.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" yielded |\"down\"| to block with arity of 2")
|
591
|
-
end
|
592
|
-
|
593
|
-
it "fails when calling yielding method without block" do
|
594
|
-
@double.should_receive(:yield_back).with(no_args()).once.and_yield('wha', 'zup')
|
595
|
-
expect {
|
596
|
-
@double.yield_back
|
597
|
-
}.to raise_error(RSpec::Mocks::MockExpectationError, "Double \"test double\" asked to yield |[\"wha\", \"zup\"]| but no block was passed")
|
598
|
-
end
|
599
|
-
|
600
|
-
it "is able to double send" do
|
601
|
-
@double.should_receive(:send).with(any_args)
|
602
|
-
@double.send 'hi'
|
603
|
-
verify @double
|
604
|
-
end
|
605
|
-
|
606
|
-
it "is able to raise from method calling yielding double" do
|
607
|
-
@double.should_receive(:yield_me).and_yield 44
|
608
|
-
|
609
|
-
expect {
|
610
|
-
@double.yield_me do |x|
|
611
|
-
raise "Bang"
|
612
|
-
end
|
613
|
-
}.to raise_error(StandardError, "Bang")
|
614
|
-
|
615
|
-
verify @double
|
616
|
-
end
|
617
|
-
|
618
|
-
it "clears expectations after verify" do
|
619
|
-
@double.should_receive(:foobar)
|
620
|
-
@double.foobar
|
621
|
-
verify @double
|
622
|
-
expect {
|
623
|
-
@double.foobar
|
624
|
-
}.to raise_error(RSpec::Mocks::MockExpectationError, %q|Double "test double" received unexpected message :foobar with (no args)|)
|
625
|
-
end
|
626
|
-
|
627
|
-
it "restores objects to their original state on rspec_reset" do
|
628
|
-
double = double("this is a double")
|
629
|
-
double.should_receive(:blah)
|
630
|
-
reset double
|
631
|
-
verify double #should throw if reset didn't work
|
632
|
-
end
|
633
|
-
|
634
|
-
it "temporarily replaces a method stub on a double" do
|
635
|
-
@double.stub(:msg).and_return(:stub_value)
|
636
|
-
@double.should_receive(:msg).with(:arg).and_return(:double_value)
|
637
|
-
expect(@double.msg(:arg)).to equal(:double_value)
|
638
|
-
expect(@double.msg).to equal(:stub_value)
|
639
|
-
expect(@double.msg).to equal(:stub_value)
|
640
|
-
verify @double
|
641
|
-
end
|
642
|
-
|
643
|
-
it "does not require a different signature to replace a method stub" do
|
644
|
-
@double.stub(:msg).and_return(:stub_value)
|
645
|
-
@double.should_receive(:msg).and_return(:double_value)
|
646
|
-
expect(@double.msg(:arg)).to equal(:double_value)
|
647
|
-
expect(@double.msg).to equal(:stub_value)
|
648
|
-
expect(@double.msg).to equal(:stub_value)
|
649
|
-
verify @double
|
650
|
-
end
|
651
|
-
|
652
|
-
it "raises an error when a previously stubbed method has a negative expectation" do
|
653
|
-
@double.stub(:msg).and_return(:stub_value)
|
654
|
-
@double.should_not_receive(:msg)
|
655
|
-
expect { @double.msg(:arg) }.to raise_error(RSpec::Mocks::MockExpectationError)
|
656
|
-
end
|
657
|
-
|
658
|
-
it "temporarily replaces a method stub on a non-double" do
|
659
|
-
non_double = Object.new
|
660
|
-
non_double.stub(:msg).and_return(:stub_value)
|
661
|
-
non_double.should_receive(:msg).with(:arg).and_return(:double_value)
|
662
|
-
expect(non_double.msg(:arg)).to equal(:double_value)
|
663
|
-
expect(non_double.msg).to equal(:stub_value)
|
664
|
-
expect(non_double.msg).to equal(:stub_value)
|
665
|
-
verify non_double
|
666
|
-
end
|
667
|
-
|
668
|
-
it "returns the stubbed value when no new value specified" do
|
669
|
-
@double.stub(:msg).and_return(:stub_value)
|
670
|
-
@double.should_receive(:msg)
|
671
|
-
expect(@double.msg).to equal(:stub_value)
|
672
|
-
verify @double
|
673
|
-
end
|
674
|
-
|
675
|
-
it "returns the stubbed value when stubbed with args and no new value specified" do
|
676
|
-
@double.stub(:msg).with(:arg).and_return(:stub_value)
|
677
|
-
@double.should_receive(:msg).with(:arg)
|
678
|
-
expect(@double.msg(:arg)).to equal(:stub_value)
|
679
|
-
verify @double
|
680
|
-
end
|
681
|
-
|
682
|
-
it "does not mess with the stub's yielded values when also doubleed" do
|
683
|
-
@double.stub(:yield_back).and_yield(:stub_value)
|
684
|
-
@double.should_receive(:yield_back).and_yield(:double_value)
|
685
|
-
@double.yield_back{|v| expect(v).to eq :double_value }
|
686
|
-
@double.yield_back{|v| expect(v).to eq :stub_value }
|
687
|
-
verify @double
|
688
|
-
end
|
689
|
-
|
690
|
-
it "can yield multiple times when told to do so" do
|
691
|
-
@double.stub(:foo).and_yield(:stub_value)
|
692
|
-
@double.should_receive(:foo).and_yield(:first_yield).and_yield(:second_yield)
|
693
|
-
|
694
|
-
expect { |b| @double.foo(&b) }.to yield_successive_args(:first_yield, :second_yield)
|
695
|
-
expect { |b| @double.foo(&b) }.to yield_with_args(:stub_value)
|
696
|
-
|
697
|
-
verify @double
|
698
|
-
end
|
699
|
-
|
700
|
-
it "assigns stub return values" do
|
701
|
-
double = RSpec::Mocks::Double.new('name', :message => :response)
|
702
|
-
expect(double.message).to eq :response
|
703
|
-
end
|
704
|
-
|
705
|
-
describe "a double message receiving a block" do
|
706
|
-
before(:each) do
|
707
|
-
@double = double("double")
|
708
|
-
@calls = 0
|
709
|
-
end
|
710
|
-
|
711
|
-
def add_call
|
712
|
-
@calls = @calls + 1
|
713
|
-
end
|
714
|
-
|
715
|
-
it "calls the block after #should_receive" do
|
716
|
-
@double.should_receive(:foo) { add_call }
|
717
|
-
|
718
|
-
@double.foo
|
719
|
-
|
720
|
-
expect(@calls).to eq 1
|
721
|
-
end
|
722
|
-
|
723
|
-
it "calls the block after #should_receive after a similar stub" do
|
724
|
-
@double.stub(:foo).and_return(:bar)
|
725
|
-
@double.should_receive(:foo) { add_call }
|
726
|
-
|
727
|
-
@double.foo
|
728
|
-
|
729
|
-
expect(@calls).to eq 1
|
730
|
-
end
|
731
|
-
|
732
|
-
it "calls the block after #once" do
|
733
|
-
@double.should_receive(:foo).once { add_call }
|
734
|
-
|
735
|
-
@double.foo
|
736
|
-
|
737
|
-
expect(@calls).to eq 1
|
738
|
-
end
|
739
|
-
|
740
|
-
it "calls the block after #twice" do
|
741
|
-
@double.should_receive(:foo).twice { add_call }
|
742
|
-
|
743
|
-
@double.foo
|
744
|
-
@double.foo
|
745
|
-
|
746
|
-
expect(@calls).to eq 2
|
747
|
-
end
|
748
|
-
|
749
|
-
it "calls the block after #times" do
|
750
|
-
@double.should_receive(:foo).exactly(10).times { add_call }
|
751
|
-
|
752
|
-
(1..10).each { @double.foo }
|
753
|
-
|
754
|
-
expect(@calls).to eq 10
|
755
|
-
end
|
756
|
-
|
757
|
-
it "calls the block after #ordered" do
|
758
|
-
@double.should_receive(:foo).ordered { add_call }
|
759
|
-
@double.should_receive(:bar).ordered { add_call }
|
760
|
-
|
761
|
-
@double.foo
|
762
|
-
@double.bar
|
763
|
-
|
764
|
-
expect(@calls).to eq 2
|
765
|
-
end
|
766
|
-
end
|
767
|
-
|
768
|
-
describe 'string representation generated by #to_s' do
|
769
|
-
it 'does not contain < because that might lead to invalid HTML in some situations' do
|
770
|
-
double = double("Dog")
|
771
|
-
valid_html_str = "#{double}"
|
772
|
-
expect(valid_html_str).not_to include('<')
|
773
|
-
end
|
774
|
-
end
|
775
|
-
|
776
|
-
describe "#to_str", :unless => RUBY_VERSION == '1.9.2' do
|
777
|
-
it "should not respond to #to_str to avoid being coerced to strings by the runtime" do
|
778
|
-
double = double("Foo")
|
779
|
-
expect { double.to_str }.to raise_error(
|
780
|
-
RSpec::Mocks::MockExpectationError,
|
781
|
-
'Double "Foo" received unexpected message :to_str with (no args)')
|
782
|
-
end
|
783
|
-
end
|
784
|
-
|
785
|
-
describe "double created with no name" do
|
786
|
-
it "does not use a name in a failure message" do
|
787
|
-
double = double()
|
788
|
-
expect {double.foo}.to raise_error(/Double received/)
|
789
|
-
end
|
790
|
-
|
791
|
-
it "does respond to initially stubbed methods" do
|
792
|
-
double = double(:foo => "woo", :bar => "car")
|
793
|
-
expect(double.foo).to eq "woo"
|
794
|
-
expect(double.bar).to eq "car"
|
795
|
-
end
|
796
|
-
end
|
797
|
-
|
798
|
-
describe "==" do
|
799
|
-
it "sends '== self' to the comparison object" do
|
800
|
-
first = double('first')
|
801
|
-
second = double('second')
|
802
|
-
|
803
|
-
first.should_receive(:==).with(second)
|
804
|
-
second == first
|
805
|
-
end
|
806
|
-
end
|
807
|
-
|
808
|
-
describe "with" do
|
809
|
-
before { @double = double('double') }
|
810
|
-
context "with args" do
|
811
|
-
context "with matching args" do
|
812
|
-
it "passes" do
|
813
|
-
@double.should_receive(:foo).with('bar')
|
814
|
-
@double.foo('bar')
|
815
|
-
end
|
816
|
-
end
|
817
|
-
|
818
|
-
context "with non-matching args" do
|
819
|
-
it "fails" do
|
820
|
-
@double.should_receive(:foo).with('bar')
|
821
|
-
expect do
|
822
|
-
@double.foo('baz')
|
823
|
-
end.to raise_error
|
824
|
-
reset @double
|
825
|
-
end
|
826
|
-
end
|
827
|
-
|
828
|
-
context "with non-matching doubles" do
|
829
|
-
it "fails" do
|
830
|
-
d1 = double('1')
|
831
|
-
d2 = double('2')
|
832
|
-
@double.should_receive(:foo).with(d1)
|
833
|
-
expect do
|
834
|
-
@double.foo(d2)
|
835
|
-
end.to raise_error
|
836
|
-
reset @double
|
837
|
-
end
|
838
|
-
end
|
839
|
-
|
840
|
-
context "with non-matching doubles as_null_object" do
|
841
|
-
it "fails" do
|
842
|
-
d1 = double('1').as_null_object
|
843
|
-
d2 = double('2').as_null_object
|
844
|
-
@double.should_receive(:foo).with(d1)
|
845
|
-
expect do
|
846
|
-
@double.foo(d2)
|
847
|
-
end.to raise_error
|
848
|
-
reset @double
|
849
|
-
end
|
850
|
-
end
|
851
|
-
end
|
852
|
-
|
853
|
-
context "with a block" do
|
854
|
-
context "with matching args" do
|
855
|
-
it "returns the result of the block" do
|
856
|
-
@double.should_receive(:foo).with('bar') { 'baz' }
|
857
|
-
expect(@double.foo('bar')).to eq('baz')
|
858
|
-
end
|
859
|
-
end
|
860
|
-
|
861
|
-
context "with non-matching args" do
|
862
|
-
it "fails" do
|
863
|
-
@double.should_receive(:foo).with('bar') { 'baz' }
|
864
|
-
expect do
|
865
|
-
expect(@double.foo('wrong')).to eq('baz')
|
866
|
-
end.to raise_error(/received :foo with unexpected arguments/)
|
867
|
-
reset @double
|
868
|
-
end
|
869
|
-
end
|
870
|
-
end
|
871
|
-
end
|
872
|
-
end
|
873
|
-
end
|
874
|
-
end
|