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,119 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
describe "a message expectation with multiple return values and no specified count" do
|
6
|
-
before(:each) do
|
7
|
-
@double = double
|
8
|
-
@return_values = [1,2,3]
|
9
|
-
@double.should_receive(:do_something).and_return(@return_values[0],@return_values[1],@return_values[2])
|
10
|
-
end
|
11
|
-
|
12
|
-
it "returns values in order" do
|
13
|
-
@double.do_something.should eq @return_values[0]
|
14
|
-
@double.do_something.should eq @return_values[1]
|
15
|
-
@double.do_something.should eq @return_values[2]
|
16
|
-
@double.rspec_verify
|
17
|
-
end
|
18
|
-
|
19
|
-
it "falls back to a previously stubbed value" do
|
20
|
-
@double.stub :do_something => :stub_result
|
21
|
-
@double.do_something.should eq @return_values[0]
|
22
|
-
@double.do_something.should eq @return_values[1]
|
23
|
-
@double.do_something.should eq @return_values[2]
|
24
|
-
@double.do_something.should eq :stub_result
|
25
|
-
end
|
26
|
-
|
27
|
-
it "fails when there are too few calls (if there is no stub)" do
|
28
|
-
@double.do_something
|
29
|
-
@double.do_something
|
30
|
-
expect { @double.rspec_verify }.to raise_error
|
31
|
-
end
|
32
|
-
|
33
|
-
it "fails when there are too many calls (if there is no stub)" do
|
34
|
-
@double.do_something
|
35
|
-
@double.do_something
|
36
|
-
@double.do_something
|
37
|
-
@double.do_something
|
38
|
-
expect { @double.rspec_verify }.to raise_error
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
describe "a message expectation with multiple return values with a specified count equal to the number of values" do
|
43
|
-
before(:each) do
|
44
|
-
@double = double
|
45
|
-
@return_values = [1,2,3]
|
46
|
-
@double.should_receive(:do_something).exactly(3).times.and_return(@return_values[0],@return_values[1],@return_values[2])
|
47
|
-
end
|
48
|
-
|
49
|
-
it "returns values in order to consecutive calls" do
|
50
|
-
@double.do_something.should eq @return_values[0]
|
51
|
-
@double.do_something.should eq @return_values[1]
|
52
|
-
@double.do_something.should eq @return_values[2]
|
53
|
-
@double.rspec_verify
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
describe "a message expectation with multiple return values specifying at_least less than the number of values" do
|
58
|
-
before(:each) do
|
59
|
-
@double = double
|
60
|
-
@double.should_receive(:do_something).at_least(:twice).with(no_args).and_return(11, 22)
|
61
|
-
end
|
62
|
-
|
63
|
-
it "uses the last return value for subsequent calls" do
|
64
|
-
@double.do_something.should equal(11)
|
65
|
-
@double.do_something.should equal(22)
|
66
|
-
@double.do_something.should equal(22)
|
67
|
-
@double.rspec_verify
|
68
|
-
end
|
69
|
-
|
70
|
-
it "fails when called less than the specified number" do
|
71
|
-
@double.do_something.should equal(11)
|
72
|
-
expect { @double.rspec_verify }.to raise_error(RSpec::Mocks::MockExpectationError)
|
73
|
-
end
|
74
|
-
|
75
|
-
context "when method is stubbed too" do
|
76
|
-
before { @double.stub(:do_something).and_return :stub_result }
|
77
|
-
|
78
|
-
it "uses the last value for subsequent calls" do
|
79
|
-
@double.do_something.should equal(11)
|
80
|
-
@double.do_something.should equal(22)
|
81
|
-
@double.do_something.should equal(22)
|
82
|
-
@double.rspec_verify
|
83
|
-
end
|
84
|
-
|
85
|
-
it "fails when called less than the specified number" do
|
86
|
-
@double.do_something.should equal(11)
|
87
|
-
expect { @double.rspec_verify }.to raise_error(RSpec::Mocks::MockExpectationError)
|
88
|
-
end
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
describe "a message expectation with multiple return values with a specified count larger than the number of values" do
|
93
|
-
before(:each) do
|
94
|
-
@double = RSpec::Mocks::Mock.new("double")
|
95
|
-
@double.should_receive(:do_something).exactly(3).times.and_return(11, 22)
|
96
|
-
end
|
97
|
-
|
98
|
-
it "uses the last return value for subsequent calls" do
|
99
|
-
@double.do_something.should equal(11)
|
100
|
-
@double.do_something.should equal(22)
|
101
|
-
@double.do_something.should equal(22)
|
102
|
-
@double.rspec_verify
|
103
|
-
end
|
104
|
-
|
105
|
-
it "fails when called less than the specified number" do
|
106
|
-
@double.do_something
|
107
|
-
@double.do_something
|
108
|
-
expect { @double.rspec_verify }.to raise_error
|
109
|
-
end
|
110
|
-
|
111
|
-
it "fails fast when called greater than the specified number" do
|
112
|
-
@double.do_something
|
113
|
-
@double.do_something
|
114
|
-
@double.do_something
|
115
|
-
expect { @double.do_something }.to raise_error
|
116
|
-
end
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
@@ -1,62 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
def remove_last_describe_from_world
|
4
|
-
RSpec::world.example_groups.pop
|
5
|
-
end
|
6
|
-
|
7
|
-
def empty_example_group
|
8
|
-
RSpec::Core::ExampleGroup.describe(Object, 'Empty Behaviour Group') { }
|
9
|
-
remove_last_describe_from_world
|
10
|
-
end
|
11
|
-
|
12
|
-
module RSpec
|
13
|
-
module Mocks
|
14
|
-
|
15
|
-
describe "an expectation set on nil" do
|
16
|
-
it "issues a warning with file and line number information" do
|
17
|
-
expected_warning = %r%An expectation of :foo was set on nil. Called from #{__FILE__}:#{__LINE__+3}(:in .+)?. Use allow_message_expectations_on_nil to disable warnings.%
|
18
|
-
Kernel.should_receive(:warn).with(expected_warning)
|
19
|
-
|
20
|
-
nil.should_receive(:foo)
|
21
|
-
nil.foo
|
22
|
-
end
|
23
|
-
|
24
|
-
it "issues a warning when the expectation is negative" do
|
25
|
-
Kernel.should_receive(:warn)
|
26
|
-
|
27
|
-
nil.should_not_receive(:foo)
|
28
|
-
end
|
29
|
-
|
30
|
-
it "does not issue a warning when expectations are set to be allowed" do
|
31
|
-
allow_message_expectations_on_nil
|
32
|
-
Kernel.should_not_receive(:warn)
|
33
|
-
|
34
|
-
nil.should_receive(:foo)
|
35
|
-
nil.should_not_receive(:bar)
|
36
|
-
nil.foo
|
37
|
-
end
|
38
|
-
|
39
|
-
end
|
40
|
-
|
41
|
-
describe "#allow_message_expectations_on_nil" do
|
42
|
-
|
43
|
-
|
44
|
-
it "does not effect subsequent examples" do
|
45
|
-
example_group = empty_example_group
|
46
|
-
example_group.it("when called in one example that doesn't end up setting an expectation on nil") do
|
47
|
-
allow_message_expectations_on_nil
|
48
|
-
end
|
49
|
-
example_group.it("should not effect the next exapmle ran") do
|
50
|
-
Kernel.should_receive(:warn)
|
51
|
-
nil.should_receive(:foo)
|
52
|
-
nil.foo
|
53
|
-
end
|
54
|
-
|
55
|
-
example_group
|
56
|
-
|
57
|
-
end
|
58
|
-
|
59
|
-
end
|
60
|
-
|
61
|
-
end
|
62
|
-
end
|
@@ -1,106 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
describe "a double _not_ acting as a null object" do
|
6
|
-
before(:each) do
|
7
|
-
@double = double('non-null object')
|
8
|
-
end
|
9
|
-
|
10
|
-
it "says it does not respond to messages it doesn't understand" do
|
11
|
-
@double.should_not respond_to(:foo)
|
12
|
-
end
|
13
|
-
|
14
|
-
it "says it responds to messages it does understand" do
|
15
|
-
@double.stub(:foo)
|
16
|
-
@double.should respond_to(:foo)
|
17
|
-
end
|
18
|
-
|
19
|
-
it "raises an error when interpolated in a string as an integer" do
|
20
|
-
# Not sure why, but 1.9.2 raises a different error than 1.8.7 and 1.9.3...
|
21
|
-
expected_error = RUBY_VERSION == '1.9.2' ?
|
22
|
-
RSpec::Mocks::MockExpectationError :
|
23
|
-
TypeError
|
24
|
-
|
25
|
-
expect { "%i" % @double }.to raise_error(expected_error)
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
describe "a double acting as a null object" do
|
30
|
-
before(:each) do
|
31
|
-
@double = double('null object').as_null_object
|
32
|
-
end
|
33
|
-
|
34
|
-
it "says it responds to everything" do
|
35
|
-
@double.should respond_to(:any_message_it_gets)
|
36
|
-
end
|
37
|
-
|
38
|
-
it "allows explicit stubs" do
|
39
|
-
@double.stub(:foo) { "bar" }
|
40
|
-
@double.foo.should eq("bar")
|
41
|
-
end
|
42
|
-
|
43
|
-
it "allows explicit expectation" do
|
44
|
-
@double.should_receive(:something)
|
45
|
-
@double.something
|
46
|
-
end
|
47
|
-
|
48
|
-
it 'continues to return self from an explicit expectation' do
|
49
|
-
@double.should_receive(:bar)
|
50
|
-
@double.foo.bar.should be(@double)
|
51
|
-
end
|
52
|
-
|
53
|
-
it 'returns an explicitly stubbed value from an expectation with no implementation' do
|
54
|
-
@double.stub(:foo => "bar")
|
55
|
-
@double.should_receive(:foo)
|
56
|
-
@double.foo.should eq("bar")
|
57
|
-
end
|
58
|
-
|
59
|
-
it "fails verification when explicit exception not met" do
|
60
|
-
lambda do
|
61
|
-
@double.should_receive(:something)
|
62
|
-
@double.rspec_verify
|
63
|
-
end.should raise_error(RSpec::Mocks::MockExpectationError)
|
64
|
-
end
|
65
|
-
|
66
|
-
it "ignores unexpected methods" do
|
67
|
-
@double.random_call("a", "d", "c")
|
68
|
-
@double.rspec_verify
|
69
|
-
end
|
70
|
-
|
71
|
-
it "allows expected message with different args first" do
|
72
|
-
@double.should_receive(:message).with(:expected_arg)
|
73
|
-
@double.message(:unexpected_arg)
|
74
|
-
@double.message(:expected_arg)
|
75
|
-
end
|
76
|
-
|
77
|
-
it "allows expected message with different args second" do
|
78
|
-
@double.should_receive(:message).with(:expected_arg)
|
79
|
-
@double.message(:expected_arg)
|
80
|
-
@double.message(:unexpected_arg)
|
81
|
-
end
|
82
|
-
|
83
|
-
it "can be interpolated in a string as an integer" do
|
84
|
-
# This form of string interpolation calls
|
85
|
-
# @double.to_int.to_int.to_int...etc until it gets an integer,
|
86
|
-
# and thus gets stuck in an infinite loop unless our double
|
87
|
-
# returns an int value from #to_int.
|
88
|
-
("%i" % @double).should eq("0")
|
89
|
-
end
|
90
|
-
end
|
91
|
-
|
92
|
-
describe "#as_null_object" do
|
93
|
-
it "sets the object to null_object" do
|
94
|
-
obj = double('anything').as_null_object
|
95
|
-
obj.should be_null_object
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
describe "#null_object?" do
|
100
|
-
it "defaults to false" do
|
101
|
-
obj = double('anything')
|
102
|
-
obj.should_not be_null_object
|
103
|
-
end
|
104
|
-
end
|
105
|
-
end
|
106
|
-
end
|
@@ -1,52 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
describe "#once" do
|
6
|
-
before(:each) do
|
7
|
-
@double = double
|
8
|
-
end
|
9
|
-
|
10
|
-
it "passes when called once" do
|
11
|
-
@double.should_receive(:do_something).once
|
12
|
-
@double.do_something
|
13
|
-
@double.rspec_verify
|
14
|
-
end
|
15
|
-
|
16
|
-
it "passes when called once with specified args" do
|
17
|
-
@double.should_receive(:do_something).once.with("a", "b", "c")
|
18
|
-
@double.do_something("a", "b", "c")
|
19
|
-
@double.rspec_verify
|
20
|
-
end
|
21
|
-
|
22
|
-
it "passes when called once with unspecified args" do
|
23
|
-
@double.should_receive(:do_something).once
|
24
|
-
@double.do_something("a", "b", "c")
|
25
|
-
@double.rspec_verify
|
26
|
-
end
|
27
|
-
|
28
|
-
it "fails when called with wrong args" do
|
29
|
-
@double.should_receive(:do_something).once.with("a", "b", "c")
|
30
|
-
lambda do
|
31
|
-
@double.do_something("d", "e", "f")
|
32
|
-
end.should raise_error(RSpec::Mocks::MockExpectationError)
|
33
|
-
@double.rspec_reset
|
34
|
-
end
|
35
|
-
|
36
|
-
it "fails fast when called twice" do
|
37
|
-
@double.should_receive(:do_something).once
|
38
|
-
@double.do_something
|
39
|
-
lambda do
|
40
|
-
@double.do_something
|
41
|
-
end.should raise_error(RSpec::Mocks::MockExpectationError)
|
42
|
-
end
|
43
|
-
|
44
|
-
it "fails when not called" do
|
45
|
-
@double.should_receive(:do_something).once
|
46
|
-
lambda do
|
47
|
-
@double.rspec_verify
|
48
|
-
end.should raise_error(RSpec::Mocks::MockExpectationError)
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
describe "calling :should_receive with an options hash" do
|
6
|
-
it "reports the file and line submitted with :expected_from" do
|
7
|
-
begin
|
8
|
-
mock = RSpec::Mocks::Mock.new("a mock")
|
9
|
-
mock.should_receive(:message, :expected_from => "/path/to/blah.ext:37")
|
10
|
-
mock.rspec_verify
|
11
|
-
rescue Exception => e
|
12
|
-
ensure
|
13
|
-
e.backtrace.to_s.should =~ /\/path\/to\/blah.ext:37/m
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
it "uses the message supplied with :message" do
|
18
|
-
lambda {
|
19
|
-
m = RSpec::Mocks::Mock.new("a mock")
|
20
|
-
m.should_receive(:message, :message => "recebi nada")
|
21
|
-
m.rspec_verify
|
22
|
-
}.should raise_error("recebi nada")
|
23
|
-
end
|
24
|
-
|
25
|
-
it "uses the message supplied with :message after a similar stub" do
|
26
|
-
lambda {
|
27
|
-
m = RSpec::Mocks::Mock.new("a mock")
|
28
|
-
m.stub(:message)
|
29
|
-
m.should_receive(:message, :message => "from mock")
|
30
|
-
m.rspec_verify
|
31
|
-
}.should raise_error("from mock")
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
@@ -1,171 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec
|
4
|
-
module Mocks
|
5
|
-
describe "using a Partial Mock," do
|
6
|
-
before(:each) do
|
7
|
-
@object = Object.new
|
8
|
-
end
|
9
|
-
|
10
|
-
it "names the class in the failure message" do
|
11
|
-
@object.should_receive(:foo)
|
12
|
-
expect do
|
13
|
-
@object.rspec_verify
|
14
|
-
end.to raise_error(RSpec::Mocks::MockExpectationError, /\(#<Object:.*>\).foo/)
|
15
|
-
end
|
16
|
-
|
17
|
-
it "names the class in the failure message when expectation is on class" do
|
18
|
-
Object.should_receive(:foo)
|
19
|
-
lambda do
|
20
|
-
Object.rspec_verify
|
21
|
-
end.should raise_error(RSpec::Mocks::MockExpectationError, /<Object \(class\)>/)
|
22
|
-
end
|
23
|
-
|
24
|
-
it "does not conflict with @options in the object" do
|
25
|
-
@object.instance_eval { @options = Object.new }
|
26
|
-
@object.should_receive(:blah)
|
27
|
-
@object.blah
|
28
|
-
end
|
29
|
-
|
30
|
-
it "should_not_receive mocks out the method" do
|
31
|
-
@object.should_not_receive(:fuhbar)
|
32
|
-
expect {
|
33
|
-
@object.fuhbar
|
34
|
-
}.to raise_error(
|
35
|
-
RSpec::Mocks::MockExpectationError,
|
36
|
-
/expected\: 0 times\n received\: 1 time/
|
37
|
-
)
|
38
|
-
end
|
39
|
-
|
40
|
-
it "should_not_receive returns a negative message expectation" do
|
41
|
-
@object.should_not_receive(:foobar).should be_kind_of(RSpec::Mocks::NegativeMessageExpectation)
|
42
|
-
end
|
43
|
-
|
44
|
-
it "should_receive mocks out the method" do
|
45
|
-
@object.should_receive(:foobar).with(:test_param).and_return(1)
|
46
|
-
@object.foobar(:test_param).should equal(1)
|
47
|
-
end
|
48
|
-
|
49
|
-
it "should_receive handles a hash" do
|
50
|
-
@object.should_receive(:foobar).with(:key => "value").and_return(1)
|
51
|
-
@object.foobar(:key => "value").should equal(1)
|
52
|
-
end
|
53
|
-
|
54
|
-
it "should_receive handles an inner hash" do
|
55
|
-
hash = {:a => {:key => "value"}}
|
56
|
-
@object.should_receive(:foobar).with(:key => "value").and_return(1)
|
57
|
-
@object.foobar(hash[:a]).should equal(1)
|
58
|
-
end
|
59
|
-
|
60
|
-
it "should_receive returns a message expectation" do
|
61
|
-
@object.should_receive(:foobar).should be_kind_of(RSpec::Mocks::MessageExpectation)
|
62
|
-
@object.foobar
|
63
|
-
end
|
64
|
-
|
65
|
-
it "should_receive verifies method was called" do
|
66
|
-
@object.should_receive(:foobar).with(:test_param).and_return(1)
|
67
|
-
lambda do
|
68
|
-
@object.rspec_verify
|
69
|
-
end.should raise_error(RSpec::Mocks::MockExpectationError)
|
70
|
-
end
|
71
|
-
|
72
|
-
it "should_receive also takes a String argument" do
|
73
|
-
@object.should_receive('foobar')
|
74
|
-
@object.foobar
|
75
|
-
end
|
76
|
-
|
77
|
-
it "should_not_receive also takes a String argument" do
|
78
|
-
@object.should_not_receive('foobar')
|
79
|
-
lambda do
|
80
|
-
@object.foobar
|
81
|
-
end.should raise_error(RSpec::Mocks::MockExpectationError)
|
82
|
-
end
|
83
|
-
|
84
|
-
it "uses reports nil in the error message" do
|
85
|
-
allow_message_expectations_on_nil
|
86
|
-
|
87
|
-
@nil = nil
|
88
|
-
@nil.should_receive(:foobar)
|
89
|
-
expect {
|
90
|
-
@nil.rspec_verify
|
91
|
-
}.to raise_error(
|
92
|
-
RSpec::Mocks::MockExpectationError,
|
93
|
-
%Q|(nil).foobar(any args)\n expected: 1 time\n received: 0 times|
|
94
|
-
)
|
95
|
-
end
|
96
|
-
|
97
|
-
it "includes the class name in the error when mocking a class method that is called an extra time with the wrong args" do
|
98
|
-
klass = Class.new do
|
99
|
-
def self.to_s
|
100
|
-
"MyClass"
|
101
|
-
end
|
102
|
-
end
|
103
|
-
|
104
|
-
klass.should_receive(:bar).with(1)
|
105
|
-
klass.bar(1)
|
106
|
-
|
107
|
-
expect {
|
108
|
-
klass.bar(2)
|
109
|
-
}.to raise_error(RSpec::Mocks::MockExpectationError, /MyClass/)
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
describe "Partially mocking an object that defines ==, after another mock has been defined" do
|
114
|
-
before(:each) do
|
115
|
-
stub("existing mock", :foo => :foo)
|
116
|
-
end
|
117
|
-
|
118
|
-
class PartiallyMockedEquals
|
119
|
-
attr_reader :val
|
120
|
-
def initialize(val)
|
121
|
-
@val = val
|
122
|
-
end
|
123
|
-
|
124
|
-
def ==(other)
|
125
|
-
@val == other.val
|
126
|
-
end
|
127
|
-
end
|
128
|
-
|
129
|
-
it "does not raise an error when stubbing the object" do
|
130
|
-
o = PartiallyMockedEquals.new :foo
|
131
|
-
lambda { o.stub(:bar) }.should_not raise_error(NoMethodError)
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
describe "Method visibility when using partial mocks" do
|
136
|
-
class MockableClass
|
137
|
-
def public_method
|
138
|
-
private_method
|
139
|
-
protected_method
|
140
|
-
end
|
141
|
-
protected
|
142
|
-
def protected_method; end
|
143
|
-
private
|
144
|
-
def private_method; end
|
145
|
-
end
|
146
|
-
|
147
|
-
before(:each) do
|
148
|
-
@object = MockableClass.new
|
149
|
-
end
|
150
|
-
|
151
|
-
it 'keeps public methods public' do
|
152
|
-
@object.should_receive(:public_method)
|
153
|
-
@object.public_methods.should include_method(:public_method)
|
154
|
-
@object.public_method
|
155
|
-
end
|
156
|
-
|
157
|
-
it 'keeps private methods private' do
|
158
|
-
@object.should_receive(:private_method)
|
159
|
-
@object.private_methods.should include_method(:private_method)
|
160
|
-
@object.public_method
|
161
|
-
end
|
162
|
-
|
163
|
-
it 'keeps protected methods protected' do
|
164
|
-
@object.should_receive(:protected_method)
|
165
|
-
@object.protected_methods.should include_method(:protected_method)
|
166
|
-
@object.public_method
|
167
|
-
end
|
168
|
-
|
169
|
-
end
|
170
|
-
end
|
171
|
-
end
|
@@ -1,95 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module RSpec::Mocks
|
4
|
-
describe "PartialMockUsingMocksDirectly" do
|
5
|
-
let(:klass) do
|
6
|
-
Class.new do
|
7
|
-
module MethodMissing
|
8
|
-
remove_method :method_missing rescue nil
|
9
|
-
def method_missing(m, *a, &b)
|
10
|
-
if m == :captured_by_method_missing
|
11
|
-
"response generated by method missing"
|
12
|
-
else
|
13
|
-
super(m, *a, &b)
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
extend MethodMissing
|
19
|
-
include MethodMissing
|
20
|
-
|
21
|
-
def existing_method
|
22
|
-
:original_value
|
23
|
-
end
|
24
|
-
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
let(:obj) { klass.new }
|
29
|
-
|
30
|
-
# See http://rubyforge.org/tracker/index.php?func=detail&aid=10263&group_id=797&atid=3149
|
31
|
-
# specify "should clear expectations on verify" do
|
32
|
-
# obj.should_receive(:msg)
|
33
|
-
# obj.msg
|
34
|
-
# obj.rspec_verify
|
35
|
-
# lambda do
|
36
|
-
# obj.msg
|
37
|
-
# end.should raise_error(NoMethodError)
|
38
|
-
#
|
39
|
-
# end
|
40
|
-
it "fails when expected message is not received" do
|
41
|
-
obj.should_receive(:msg)
|
42
|
-
lambda do
|
43
|
-
obj.rspec_verify
|
44
|
-
end.should raise_error(RSpec::Mocks::MockExpectationError)
|
45
|
-
end
|
46
|
-
|
47
|
-
it "fails when message is received with incorrect args" do
|
48
|
-
obj.should_receive(:msg).with(:correct_arg)
|
49
|
-
lambda do
|
50
|
-
obj.msg(:incorrect_arg)
|
51
|
-
end.should raise_error(RSpec::Mocks::MockExpectationError)
|
52
|
-
obj.msg(:correct_arg)
|
53
|
-
end
|
54
|
-
|
55
|
-
it "passes when expected message is received" do
|
56
|
-
obj.should_receive(:msg)
|
57
|
-
obj.msg
|
58
|
-
obj.rspec_verify
|
59
|
-
end
|
60
|
-
|
61
|
-
it "passes when message is received with correct args" do
|
62
|
-
obj.should_receive(:msg).with(:correct_arg)
|
63
|
-
obj.msg(:correct_arg)
|
64
|
-
obj.rspec_verify
|
65
|
-
end
|
66
|
-
|
67
|
-
it "restores the original method if it existed" do
|
68
|
-
obj.existing_method.should equal(:original_value)
|
69
|
-
obj.should_receive(:existing_method).and_return(:mock_value)
|
70
|
-
obj.existing_method.should equal(:mock_value)
|
71
|
-
obj.rspec_verify
|
72
|
-
obj.existing_method.should equal(:original_value)
|
73
|
-
end
|
74
|
-
|
75
|
-
context "with an instance method handled by method_missing" do
|
76
|
-
it "restores the original behavior" do
|
77
|
-
obj.captured_by_method_missing.should eq("response generated by method missing")
|
78
|
-
obj.stub(:captured_by_method_missing) { "foo" }
|
79
|
-
obj.captured_by_method_missing.should eq("foo")
|
80
|
-
obj.rspec_reset
|
81
|
-
obj.captured_by_method_missing.should eq("response generated by method missing")
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
context "with a class method handled by method_missing" do
|
86
|
-
it "restores the original behavior" do
|
87
|
-
klass.captured_by_method_missing.should eq("response generated by method missing")
|
88
|
-
klass.stub(:captured_by_method_missing) { "foo" }
|
89
|
-
klass.captured_by_method_missing.should eq("foo")
|
90
|
-
klass.rspec_reset
|
91
|
-
klass.captured_by_method_missing.should eq("response generated by method missing")
|
92
|
-
end
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|