rspec-mocks 2.11.3 → 3.11.1
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/.document +1 -1
- data/.yardopts +1 -1
- data/Changelog.md +989 -21
- data/{License.txt → LICENSE.md} +5 -3
- data/README.md +260 -73
- data/lib/rspec/mocks/any_instance/chain.rb +58 -24
- data/lib/rspec/mocks/any_instance/error_generator.rb +31 -0
- data/lib/rspec/mocks/any_instance/expect_chain_chain.rb +31 -0
- data/lib/rspec/mocks/any_instance/expectation_chain.rb +23 -30
- data/lib/rspec/mocks/any_instance/message_chains.rb +38 -15
- data/lib/rspec/mocks/any_instance/proxy.rb +116 -0
- data/lib/rspec/mocks/any_instance/recorder.rb +155 -59
- data/lib/rspec/mocks/any_instance/stub_chain.rb +33 -19
- data/lib/rspec/mocks/any_instance/stub_chain_chain.rb +10 -12
- data/lib/rspec/mocks/any_instance.rb +11 -81
- data/lib/rspec/mocks/argument_list_matcher.rb +59 -37
- data/lib/rspec/mocks/argument_matchers.rb +233 -149
- data/lib/rspec/mocks/configuration.rb +212 -0
- data/lib/rspec/mocks/error_generator.rb +304 -49
- data/lib/rspec/mocks/example_methods.rb +361 -22
- data/lib/rspec/mocks/instance_method_stasher.rb +146 -0
- data/lib/rspec/mocks/marshal_extension.rb +41 -0
- data/lib/rspec/mocks/matchers/expectation_customization.rb +20 -0
- data/lib/rspec/mocks/matchers/have_received.rb +134 -0
- data/lib/rspec/mocks/matchers/receive.rb +133 -0
- data/lib/rspec/mocks/matchers/receive_message_chain.rb +82 -0
- data/lib/rspec/mocks/matchers/receive_messages.rb +77 -0
- data/lib/rspec/mocks/message_chain.rb +87 -0
- data/lib/rspec/mocks/message_expectation.rb +648 -314
- data/lib/rspec/mocks/method_double.rb +185 -58
- data/lib/rspec/mocks/method_reference.rb +202 -0
- data/lib/rspec/mocks/minitest_integration.rb +68 -0
- data/lib/rspec/mocks/mutate_const.rb +339 -0
- data/lib/rspec/mocks/object_reference.rb +149 -0
- data/lib/rspec/mocks/order_group.rb +48 -7
- data/lib/rspec/mocks/proxy.rb +405 -74
- data/lib/rspec/mocks/space.rb +219 -15
- data/lib/rspec/mocks/standalone.rb +2 -2
- data/lib/rspec/mocks/syntax.rb +325 -0
- data/lib/rspec/mocks/targets.rb +124 -0
- data/lib/rspec/mocks/test_double.rb +125 -57
- data/lib/rspec/mocks/verifying_double.rb +121 -0
- data/lib/rspec/mocks/verifying_message_expectation.rb +54 -0
- data/lib/rspec/mocks/verifying_proxy.rb +220 -0
- data/lib/rspec/mocks/version.rb +3 -1
- data/lib/rspec/mocks.rb +121 -27
- data.tar.gz.sig +0 -0
- metadata +178 -253
- metadata.gz.sig +3 -0
- data/features/README.md +0 -67
- 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 -60
- data/features/argument_matchers/general_matchers.feature +0 -85
- data/features/argument_matchers/type_matchers.feature +0 -27
- data/features/message_expectations/README.md +0 -69
- data/features/message_expectations/any_instance.feature +0 -21
- data/features/message_expectations/block_local_expectations.feature.pending +0 -55
- data/features/message_expectations/expect_message.feature +0 -94
- 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 -47
- data/features/method_stubs/any_instance.feature +0 -133
- data/features/method_stubs/as_null_object.feature +0 -35
- data/features/method_stubs/simple_return_value.feature +0 -64
- data/features/method_stubs/stub_chain.feature +0 -51
- data/features/method_stubs/stub_implementation.feature +0 -26
- data/features/method_stubs/to_ary.feature +0 -47
- data/features/outside_rspec/configuration.feature +0 -82
- data/features/outside_rspec/standalone.feature +0 -32
- data/features/step_definitions/additional_cli_steps.rb +0 -4
- data/features/stubbing_constants/README.md +0 -62
- data/features/stubbing_constants/stub_defined_constant.feature +0 -79
- data/features/stubbing_constants/stub_undefined_constant.feature +0 -50
- data/features/support/env.rb +0 -6
- data/lib/rspec/mocks/errors.rb +0 -12
- data/lib/rspec/mocks/extensions/instance_exec.rb +0 -34
- data/lib/rspec/mocks/extensions/marshal.rb +0 -23
- data/lib/rspec/mocks/extensions/psych.rb +0 -23
- data/lib/rspec/mocks/framework.rb +0 -21
- data/lib/rspec/mocks/methods.rb +0 -155
- data/lib/rspec/mocks/mock.rb +0 -7
- data/lib/rspec/mocks/serialization.rb +0 -34
- data/lib/rspec/mocks/stashed_instance_method.rb +0 -60
- data/lib/rspec/mocks/stub_const.rb +0 -332
- data/lib/spec/mocks.rb +0 -2
- data/spec/rspec/mocks/and_yield_spec.rb +0 -114
- data/spec/rspec/mocks/any_instance/message_chains_spec.rb +0 -40
- data/spec/rspec/mocks/any_instance_spec.rb +0 -877
- data/spec/rspec/mocks/any_number_of_times_spec.rb +0 -30
- data/spec/rspec/mocks/argument_expectation_spec.rb +0 -34
- data/spec/rspec/mocks/at_least_spec.rb +0 -142
- data/spec/rspec/mocks/at_most_spec.rb +0 -90
- data/spec/rspec/mocks/block_return_value_spec.rb +0 -53
- data/spec/rspec/mocks/bug_report_10260_spec.rb +0 -8
- data/spec/rspec/mocks/bug_report_10263_spec.rb +0 -25
- data/spec/rspec/mocks/bug_report_11545_spec.rb +0 -32
- data/spec/rspec/mocks/bug_report_496_spec.rb +0 -17
- data/spec/rspec/mocks/bug_report_600_spec.rb +0 -22
- data/spec/rspec/mocks/bug_report_7611_spec.rb +0 -16
- data/spec/rspec/mocks/bug_report_8165_spec.rb +0 -31
- data/spec/rspec/mocks/bug_report_830_spec.rb +0 -21
- data/spec/rspec/mocks/bug_report_957_spec.rb +0 -22
- data/spec/rspec/mocks/double_spec.rb +0 -12
- data/spec/rspec/mocks/failing_argument_matchers_spec.rb +0 -95
- data/spec/rspec/mocks/hash_excluding_matcher_spec.rb +0 -67
- data/spec/rspec/mocks/hash_including_matcher_spec.rb +0 -90
- data/spec/rspec/mocks/mock_ordering_spec.rb +0 -103
- data/spec/rspec/mocks/mock_space_spec.rb +0 -58
- data/spec/rspec/mocks/mock_spec.rb +0 -730
- data/spec/rspec/mocks/multiple_return_value_spec.rb +0 -119
- data/spec/rspec/mocks/nil_expectation_warning_spec.rb +0 -62
- data/spec/rspec/mocks/null_object_mock_spec.rb +0 -106
- data/spec/rspec/mocks/once_counts_spec.rb +0 -52
- data/spec/rspec/mocks/options_hash_spec.rb +0 -35
- data/spec/rspec/mocks/partial_mock_spec.rb +0 -171
- data/spec/rspec/mocks/partial_mock_using_mocks_directly_spec.rb +0 -95
- data/spec/rspec/mocks/passing_argument_matchers_spec.rb +0 -142
- 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 -111
- data/spec/rspec/mocks/stash_spec.rb +0 -27
- data/spec/rspec/mocks/stashed_instance_method_spec.rb +0 -53
- data/spec/rspec/mocks/stub_chain_spec.rb +0 -154
- data/spec/rspec/mocks/stub_const_spec.rb +0 -334
- data/spec/rspec/mocks/stub_implementation_spec.rb +0 -81
- data/spec/rspec/mocks/stub_spec.rb +0 -247
- data/spec/rspec/mocks/stubbed_message_expectations_spec.rb +0 -47
- data/spec/rspec/mocks/test_double_spec.rb +0 -57
- data/spec/rspec/mocks/to_ary_spec.rb +0 -40
- data/spec/rspec/mocks/twice_counts_spec.rb +0 -66
- data/spec/rspec/mocks_spec.rb +0 -51
- data/spec/spec_helper.rb +0 -21
@@ -1,142 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
describe Methods do
|
6
|
-
before(:each) do
|
7
|
-
@double = double('double')
|
8
|
-
Kernel.stub(:warn)
|
9
|
-
end
|
10
|
-
|
11
|
-
after(:each) do
|
12
|
-
@double.rspec_verify
|
13
|
-
end
|
14
|
-
|
15
|
-
context "handling argument matchers" do
|
16
|
-
it "accepts true as boolean()" do
|
17
|
-
@double.should_receive(:random_call).with(boolean())
|
18
|
-
@double.random_call(true)
|
19
|
-
end
|
20
|
-
|
21
|
-
it "accepts false as boolean()" do
|
22
|
-
@double.should_receive(:random_call).with(boolean())
|
23
|
-
@double.random_call(false)
|
24
|
-
end
|
25
|
-
|
26
|
-
it "accepts fixnum as kind_of(Numeric)" do
|
27
|
-
@double.should_receive(:random_call).with(kind_of(Numeric))
|
28
|
-
@double.random_call(1)
|
29
|
-
end
|
30
|
-
|
31
|
-
it "accepts float as an_instance_of(Numeric)" do
|
32
|
-
@double.should_receive(:random_call).with(kind_of(Numeric))
|
33
|
-
@double.random_call(1.5)
|
34
|
-
end
|
35
|
-
|
36
|
-
it "accepts fixnum as instance_of(Fixnum)" do
|
37
|
-
@double.should_receive(:random_call).with(instance_of(Fixnum))
|
38
|
-
@double.random_call(1)
|
39
|
-
end
|
40
|
-
|
41
|
-
it "does NOT accept fixnum as instance_of(Numeric)" do
|
42
|
-
@double.should_not_receive(:random_call).with(instance_of(Numeric))
|
43
|
-
@double.random_call(1)
|
44
|
-
end
|
45
|
-
|
46
|
-
it "does NOT accept float as instance_of(Numeric)" do
|
47
|
-
@double.should_not_receive(:random_call).with(instance_of(Numeric))
|
48
|
-
@double.random_call(1.5)
|
49
|
-
end
|
50
|
-
|
51
|
-
it "accepts string as anything()" do
|
52
|
-
@double.should_receive(:random_call).with("a", anything(), "c")
|
53
|
-
@double.random_call("a", "whatever", "c")
|
54
|
-
end
|
55
|
-
|
56
|
-
it "matches duck type with one method" do
|
57
|
-
@double.should_receive(:random_call).with(duck_type(:length))
|
58
|
-
@double.random_call([])
|
59
|
-
end
|
60
|
-
|
61
|
-
it "matches duck type with two methods" do
|
62
|
-
@double.should_receive(:random_call).with(duck_type(:abs, :div))
|
63
|
-
@double.random_call(1)
|
64
|
-
end
|
65
|
-
|
66
|
-
it "matches no args against any_args()" do
|
67
|
-
@double.should_receive(:random_call).with(any_args)
|
68
|
-
@double.random_call()
|
69
|
-
end
|
70
|
-
|
71
|
-
it "matches one arg against any_args()" do
|
72
|
-
@double.should_receive(:random_call).with(any_args)
|
73
|
-
@double.random_call("a string")
|
74
|
-
end
|
75
|
-
|
76
|
-
it "matches no args against no_args()" do
|
77
|
-
@double.should_receive(:random_call).with(no_args)
|
78
|
-
@double.random_call()
|
79
|
-
end
|
80
|
-
|
81
|
-
it "matches hash with hash_including same hash" do
|
82
|
-
@double.should_receive(:random_call).with(hash_including(:a => 1))
|
83
|
-
@double.random_call(:a => 1)
|
84
|
-
end
|
85
|
-
end
|
86
|
-
|
87
|
-
context "handling block matchers" do
|
88
|
-
it "matches arguments against RSpec expectations" do
|
89
|
-
@double.should_receive(:random_call).with {|arg1, arg2, arr, *rest|
|
90
|
-
arg1.should eq 5
|
91
|
-
arg2.should have_at_least(3).characters
|
92
|
-
arg2.should have_at_most(10).characters
|
93
|
-
arr.map {|i| i * 2}.should eq [2,4,6]
|
94
|
-
rest.should eq [:fee, "fi", 4]
|
95
|
-
}
|
96
|
-
@double.random_call 5, "hello", [1,2,3], :fee, "fi", 4
|
97
|
-
end
|
98
|
-
|
99
|
-
it "does not eval the block as the return value" do
|
100
|
-
eval_count = 0
|
101
|
-
@double.should_receive(:msg).with {|a| eval_count += 1}
|
102
|
-
@double.msg(:ignore)
|
103
|
-
eval_count.should eq(1)
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
context "handling non-matcher arguments" do
|
108
|
-
it "matches non special symbol (can be removed when deprecated symbols are removed)" do
|
109
|
-
@double.should_receive(:random_call).with(:some_symbol)
|
110
|
-
@double.random_call(:some_symbol)
|
111
|
-
end
|
112
|
-
|
113
|
-
it "matches string against regexp" do
|
114
|
-
@double.should_receive(:random_call).with(/bcd/)
|
115
|
-
@double.random_call("abcde")
|
116
|
-
end
|
117
|
-
|
118
|
-
it "matches regexp against regexp" do
|
119
|
-
@double.should_receive(:random_call).with(/bcd/)
|
120
|
-
@double.random_call(/bcd/)
|
121
|
-
end
|
122
|
-
|
123
|
-
it "matches against a hash submitted and received by value" do
|
124
|
-
@double.should_receive(:random_call).with(:a => "a", :b => "b")
|
125
|
-
@double.random_call(:a => "a", :b => "b")
|
126
|
-
end
|
127
|
-
|
128
|
-
it "matches against a hash submitted by reference and received by value" do
|
129
|
-
opts = {:a => "a", :b => "b"}
|
130
|
-
@double.should_receive(:random_call).with(opts)
|
131
|
-
@double.random_call(:a => "a", :b => "b")
|
132
|
-
end
|
133
|
-
|
134
|
-
it "matches against a hash submitted by value and received by reference" do
|
135
|
-
opts = {:a => "a", :b => "b"}
|
136
|
-
@double.should_receive(:random_call).with(:a => "a", :b => "b")
|
137
|
-
@double.random_call(opts)
|
138
|
-
end
|
139
|
-
end
|
140
|
-
end
|
141
|
-
end
|
142
|
-
end
|
@@ -1,68 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
describe "PreciseCounts" do
|
6
|
-
before(:each) do
|
7
|
-
@double = double("test double")
|
8
|
-
end
|
9
|
-
|
10
|
-
it "fails when exactly n times method is called less than n times" do
|
11
|
-
@double.should_receive(:do_something).exactly(3).times
|
12
|
-
@double.do_something
|
13
|
-
@double.do_something
|
14
|
-
lambda do
|
15
|
-
@double.rspec_verify
|
16
|
-
end.should raise_error(RSpec::Mocks::MockExpectationError)
|
17
|
-
end
|
18
|
-
|
19
|
-
it "fails fast when exactly n times method is called more than n times" do
|
20
|
-
@double.should_receive(:do_something).exactly(3).times
|
21
|
-
@double.do_something
|
22
|
-
@double.do_something
|
23
|
-
@double.do_something
|
24
|
-
lambda do
|
25
|
-
@double.do_something
|
26
|
-
end.should raise_error(RSpec::Mocks::MockExpectationError)
|
27
|
-
end
|
28
|
-
|
29
|
-
it "fails when exactly n times method is never called" do
|
30
|
-
@double.should_receive(:do_something).exactly(3).times
|
31
|
-
lambda do
|
32
|
-
@double.rspec_verify
|
33
|
-
end.should raise_error(RSpec::Mocks::MockExpectationError)
|
34
|
-
end
|
35
|
-
|
36
|
-
it "passes if exactly n times method is called exactly n times" do
|
37
|
-
@double.should_receive(:do_something).exactly(3).times
|
38
|
-
@double.do_something
|
39
|
-
@double.do_something
|
40
|
-
@double.do_something
|
41
|
-
@double.rspec_verify
|
42
|
-
end
|
43
|
-
|
44
|
-
it "returns the value given by a block when the exactly once method is called" do
|
45
|
-
@double.should_receive(:to_s).exactly(:once) { "testing" }
|
46
|
-
@double.to_s.should eq "testing"
|
47
|
-
@double.rspec_verify
|
48
|
-
end
|
49
|
-
|
50
|
-
it "passes mutiple calls with different args" do
|
51
|
-
@double.should_receive(:do_something).once.with(1)
|
52
|
-
@double.should_receive(:do_something).once.with(2)
|
53
|
-
@double.do_something(1)
|
54
|
-
@double.do_something(2)
|
55
|
-
@double.rspec_verify
|
56
|
-
end
|
57
|
-
|
58
|
-
it "passes multiple calls with different args and counts" do
|
59
|
-
@double.should_receive(:do_something).twice.with(1)
|
60
|
-
@double.should_receive(:do_something).once.with(2)
|
61
|
-
@double.do_something(1)
|
62
|
-
@double.do_something(2)
|
63
|
-
@double.do_something(1)
|
64
|
-
@double.rspec_verify
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|
@@ -1,26 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
describe "a mock" do
|
6
|
-
before(:each) do
|
7
|
-
@mock = double("mock").as_null_object
|
8
|
-
end
|
9
|
-
it "answers false for received_message? when no messages received" do
|
10
|
-
@mock.received_message?(:message).should be_false
|
11
|
-
end
|
12
|
-
it "answers true for received_message? when message received" do
|
13
|
-
@mock.message
|
14
|
-
@mock.received_message?(:message).should be_true
|
15
|
-
end
|
16
|
-
it "answers true for received_message? when message received with correct args" do
|
17
|
-
@mock.message 1,2,3
|
18
|
-
@mock.received_message?(:message, 1,2,3).should be_true
|
19
|
-
end
|
20
|
-
it "answers false for received_message? when message received with incorrect args" do
|
21
|
-
@mock.message 1,2,3
|
22
|
-
@mock.received_message?(:message, 1,2).should be_false
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
@@ -1,111 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
describe Serialization do
|
6
|
-
|
7
|
-
class SerializableObject < Struct.new(:foo, :bar); end
|
8
|
-
|
9
|
-
class SerializableMockProxy
|
10
|
-
attr_reader :mock_proxy
|
11
|
-
|
12
|
-
def initialize(mock_proxy)
|
13
|
-
@mock_proxy = mock_proxy
|
14
|
-
end
|
15
|
-
|
16
|
-
def ==(other)
|
17
|
-
other.class == self.class && other.mock_proxy == mock_proxy
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def self.with_yaml_loaded(&block)
|
22
|
-
context 'with YAML loaded' do
|
23
|
-
module_eval(&block)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def self.without_yaml_loaded(&block)
|
28
|
-
context 'without YAML loaded' do
|
29
|
-
before do
|
30
|
-
# We can't really unload yaml, but we can fake it here...
|
31
|
-
@orig_yaml_constant = Object.send(:remove_const, :YAML)
|
32
|
-
Struct.class_eval do
|
33
|
-
alias __old_to_yaml to_yaml
|
34
|
-
undef to_yaml
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
module_eval(&block)
|
39
|
-
|
40
|
-
after do
|
41
|
-
Object.const_set(:YAML, @orig_yaml_constant)
|
42
|
-
Struct.class_eval do
|
43
|
-
alias to_yaml __old_to_yaml
|
44
|
-
undef __old_to_yaml
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
let(:serializable_object) { RSpec::Mocks::SerializableObject.new(7, "something") }
|
51
|
-
|
52
|
-
def set_stub
|
53
|
-
serializable_object.stub(:bazz => 5)
|
54
|
-
end
|
55
|
-
|
56
|
-
shared_examples_for 'normal YAML serialization' do
|
57
|
-
it 'serializes to yaml the same with and without stubbing, using #to_yaml' do
|
58
|
-
expect { set_stub }.to_not change { serializable_object.to_yaml }
|
59
|
-
end
|
60
|
-
|
61
|
-
it 'serializes to yaml the same with and without stubbing, using YAML.dump' do
|
62
|
-
expect { set_stub }.to_not change { ::YAML.dump(serializable_object) }
|
63
|
-
end
|
64
|
-
end
|
65
|
-
|
66
|
-
with_yaml_loaded do
|
67
|
-
compiled_with_psych = begin
|
68
|
-
require 'psych'
|
69
|
-
true
|
70
|
-
rescue LoadError
|
71
|
-
false
|
72
|
-
end
|
73
|
-
|
74
|
-
if compiled_with_psych
|
75
|
-
context 'using Syck as the YAML engine' do
|
76
|
-
before(:each) { ::YAML::ENGINE.yamler = 'syck' }
|
77
|
-
it_behaves_like 'normal YAML serialization'
|
78
|
-
end
|
79
|
-
|
80
|
-
context 'using Psych as the YAML engine' do
|
81
|
-
before(:each) { ::YAML::ENGINE.yamler = 'psych' }
|
82
|
-
it_behaves_like 'normal YAML serialization'
|
83
|
-
end
|
84
|
-
else
|
85
|
-
it_behaves_like 'normal YAML serialization'
|
86
|
-
end
|
87
|
-
end
|
88
|
-
|
89
|
-
without_yaml_loaded do
|
90
|
-
it 'does not add #to_yaml to the stubbed object' do
|
91
|
-
serializable_object.should_not respond_to(:to_yaml)
|
92
|
-
set_stub
|
93
|
-
serializable_object.should_not respond_to(:to_yaml)
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
it 'marshals the same with and without stubbing' do
|
98
|
-
expect { set_stub }.to_not change { Marshal.dump(serializable_object) }
|
99
|
-
end
|
100
|
-
|
101
|
-
describe "an object that has its own mock_proxy instance variable" do
|
102
|
-
let(:serializable_object) { RSpec::Mocks::SerializableMockProxy.new(:my_mock_proxy) }
|
103
|
-
|
104
|
-
it 'does not interfere with its marshalling' do
|
105
|
-
marshalled_copy = Marshal.load(Marshal.dump(serializable_object))
|
106
|
-
marshalled_copy.should eq serializable_object
|
107
|
-
end
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
@@ -1,27 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
|
6
|
-
describe "only stashing the original method" do
|
7
|
-
let(:klass) do
|
8
|
-
Class.new do
|
9
|
-
def self.foo(arg)
|
10
|
-
:original_value
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
14
|
-
it "keeps the original method intact after multiple expectations are added on the same method" do
|
15
|
-
klass.should_receive(:foo).with(:fizbaz).and_return(:wowwow)
|
16
|
-
klass.should_receive(:foo).with(:bazbar).and_return(:okay)
|
17
|
-
|
18
|
-
klass.foo(:fizbaz)
|
19
|
-
klass.foo(:bazbar)
|
20
|
-
klass.rspec_verify
|
21
|
-
|
22
|
-
klass.rspec_reset
|
23
|
-
klass.foo(:yeah).should equal(:original_value)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,53 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe StashedInstanceMethod do
|
4
|
-
class ExampleClass
|
5
|
-
def hello
|
6
|
-
:hello_defined_on_class
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
def singleton_class_for(obj)
|
11
|
-
class << obj; self; end
|
12
|
-
end
|
13
|
-
|
14
|
-
it "stashes the current implementation of an instance method so it can be temporarily replaced" do
|
15
|
-
obj = Object.new
|
16
|
-
def obj.hello; :hello_defined_on_singleton_class; end;
|
17
|
-
|
18
|
-
stashed_method = StashedInstanceMethod.new(singleton_class_for(obj), :hello)
|
19
|
-
stashed_method.stash
|
20
|
-
|
21
|
-
def obj.hello; :overridden_hello; end
|
22
|
-
expect(obj.hello).to eql :overridden_hello
|
23
|
-
|
24
|
-
stashed_method.restore
|
25
|
-
expect(obj.hello).to eql :hello_defined_on_singleton_class
|
26
|
-
end
|
27
|
-
|
28
|
-
it "stashes private instance methods" do
|
29
|
-
obj = Object.new
|
30
|
-
def obj.hello; :hello_defined_on_singleton_class; end;
|
31
|
-
singleton_class_for(obj).__send__(:private, :hello)
|
32
|
-
|
33
|
-
stashed_method = StashedInstanceMethod.new(singleton_class_for(obj), :hello)
|
34
|
-
stashed_method.stash
|
35
|
-
|
36
|
-
def obj.hello; :overridden_hello; end
|
37
|
-
stashed_method.restore
|
38
|
-
expect(obj.send(:hello)).to eql :hello_defined_on_singleton_class
|
39
|
-
end
|
40
|
-
|
41
|
-
it "only stashes methods directly defined on the given class, not its ancestors" do
|
42
|
-
obj = ExampleClass.new
|
43
|
-
|
44
|
-
stashed_method = StashedInstanceMethod.new(singleton_class_for(obj), :hello)
|
45
|
-
stashed_method.stash
|
46
|
-
|
47
|
-
def obj.hello; :overridden_hello; end;
|
48
|
-
expect(obj.hello).to eql :overridden_hello
|
49
|
-
|
50
|
-
stashed_method.restore
|
51
|
-
expect(obj.hello).to eql :overridden_hello
|
52
|
-
end
|
53
|
-
end
|
@@ -1,154 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
describe "A chained method stub" do
|
6
|
-
let(:object) { Object.new }
|
7
|
-
|
8
|
-
context "with one method in chain" do
|
9
|
-
context "using and_return" do
|
10
|
-
it "returns expected value from chaining only one method call" do
|
11
|
-
object.stub_chain(:msg1).and_return(:return_value)
|
12
|
-
object.msg1.should equal(:return_value)
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
context "using a block" do
|
17
|
-
it "returns the correct value" do
|
18
|
-
object.stub_chain(:msg1) { :return_value }
|
19
|
-
object.msg1.should equal(:return_value)
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
context "using a hash" do
|
24
|
-
it "returns the value of the key/value pair" do
|
25
|
-
object.stub_chain(:msg1 => :return_value)
|
26
|
-
object.msg1.should equal(:return_value)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
context "with two methods in chain" do
|
32
|
-
context "using and_return" do
|
33
|
-
it "returns expected value from chaining two method calls" do
|
34
|
-
object.stub_chain(:msg1, :msg2).and_return(:return_value)
|
35
|
-
object.msg1.msg2.should equal(:return_value)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
context "using a block" do
|
40
|
-
it "returns the correct value" do
|
41
|
-
object.stub_chain(:msg1, :msg2) { :return_value }
|
42
|
-
object.msg1.msg2.should equal(:return_value)
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
context "using a hash" do
|
47
|
-
it "returns the value of the key/value pair" do
|
48
|
-
object.stub_chain(:msg1, :msg2 => :return_value)
|
49
|
-
object.msg1.msg2.should equal(:return_value)
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
context "with four methods in chain" do
|
55
|
-
context "using and_return" do
|
56
|
-
it "returns expected value from chaining two method calls" do
|
57
|
-
object.stub_chain(:msg1, :msg2, :msg3, :msg4).and_return(:return_value)
|
58
|
-
object.msg1.msg2.msg3.msg4.should equal(:return_value)
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
context "using a block" do
|
63
|
-
it "returns the correct value" do
|
64
|
-
object.stub_chain(:msg1, :msg2, :msg3, :msg4) { :return_value }
|
65
|
-
object.msg1.msg2.msg3.msg4.should equal(:return_value)
|
66
|
-
end
|
67
|
-
end
|
68
|
-
|
69
|
-
context "using a hash" do
|
70
|
-
it "returns the value of the key/value pair" do
|
71
|
-
object.stub_chain(:msg1, :msg2, :msg3, :msg4 => :return_value)
|
72
|
-
object.msg1.msg2.msg3.msg4.should equal(:return_value)
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
context "using a hash with a string key" do
|
77
|
-
it "returns the value of the key/value pair" do
|
78
|
-
object.stub_chain("msg1.msg2.msg3.msg4" => :return_value)
|
79
|
-
object.msg1.msg2.msg3.msg4.should equal(:return_value)
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
it "returns expected value from chaining four method calls" do
|
85
|
-
object.stub_chain(:msg1, :msg2, :msg3, :msg4).and_return(:return_value)
|
86
|
-
object.msg1.msg2.msg3.msg4.should equal(:return_value)
|
87
|
-
end
|
88
|
-
|
89
|
-
context "with messages shared across multiple chains" do
|
90
|
-
context "using and_return" do
|
91
|
-
context "starting with the same message" do
|
92
|
-
it "returns expected value" do
|
93
|
-
object.stub_chain(:msg1, :msg2, :msg3).and_return(:first)
|
94
|
-
object.stub_chain(:msg1, :msg2, :msg4).and_return(:second)
|
95
|
-
|
96
|
-
object.msg1.msg2.msg3.should equal(:first)
|
97
|
-
object.msg1.msg2.msg4.should equal(:second)
|
98
|
-
end
|
99
|
-
end
|
100
|
-
|
101
|
-
context "starting with the different messages" do
|
102
|
-
it "returns expected value" do
|
103
|
-
object.stub_chain(:msg1, :msg2, :msg3).and_return(:first)
|
104
|
-
object.stub_chain(:msg4, :msg2, :msg3).and_return(:second)
|
105
|
-
|
106
|
-
object.msg1.msg2.msg3.should equal(:first)
|
107
|
-
object.msg4.msg2.msg3.should equal(:second)
|
108
|
-
end
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
context "using => value" do
|
113
|
-
context "starting with the same message" do
|
114
|
-
it "returns expected value" do
|
115
|
-
object.stub_chain(:msg1, :msg2, :msg3 => :first)
|
116
|
-
object.stub_chain(:msg1, :msg2, :msg4 => :second)
|
117
|
-
|
118
|
-
object.msg1.msg2.msg3.should equal(:first)
|
119
|
-
object.msg1.msg2.msg4.should equal(:second)
|
120
|
-
end
|
121
|
-
end
|
122
|
-
|
123
|
-
context "starting with different messages" do
|
124
|
-
it "returns expected value" do
|
125
|
-
object.stub_chain(:msg1, :msg2, :msg3 => :first)
|
126
|
-
object.stub_chain(:msg4, :msg2, :msg3 => :second)
|
127
|
-
|
128
|
-
object.msg1.msg2.msg3.should equal(:first)
|
129
|
-
object.msg4.msg2.msg3.should equal(:second)
|
130
|
-
end
|
131
|
-
end
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
it "returns expected value when chain is a dot separated string, like stub_chain('msg1.msg2.msg3')" do
|
136
|
-
object.stub_chain("msg1.msg2.msg3").and_return(:return_value)
|
137
|
-
object.msg1.msg2.msg3.should equal(:return_value)
|
138
|
-
end
|
139
|
-
|
140
|
-
it "returns expected value from two chains with shared messages at the beginning" do
|
141
|
-
object.stub_chain(:msg1, :msg2, :msg3, :msg4).and_return(:first)
|
142
|
-
object.stub_chain(:msg1, :msg2, :msg3, :msg5).and_return(:second)
|
143
|
-
|
144
|
-
object.msg1.msg2.msg3.msg4.should equal(:first)
|
145
|
-
object.msg1.msg2.msg3.msg5.should equal(:second)
|
146
|
-
end
|
147
|
-
|
148
|
-
it "handles private instance methods (like Object#select) in the middle of a chain" do
|
149
|
-
object.stub_chain(:msg1, :select, :msg3 => 'answer')
|
150
|
-
expect(object.msg1.select.msg3).to eq 'answer'
|
151
|
-
end
|
152
|
-
end
|
153
|
-
end
|
154
|
-
end
|