rspec-mocks 2.0.0.a1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (62) hide show
  1. data/.document +5 -0
  2. data/.gitignore +6 -0
  3. data/License.txt +22 -0
  4. data/README.markdown +8 -0
  5. data/Rakefile +50 -0
  6. data/VERSION +1 -0
  7. data/VERSION.yml +5 -0
  8. data/lib/rspec/mocks.rb +201 -0
  9. data/lib/rspec/mocks/argument_expectation.rb +51 -0
  10. data/lib/rspec/mocks/argument_matchers.rb +233 -0
  11. data/lib/rspec/mocks/error_generator.rb +81 -0
  12. data/lib/rspec/mocks/errors.rb +10 -0
  13. data/lib/rspec/mocks/extensions.rb +1 -0
  14. data/lib/rspec/mocks/extensions/object.rb +3 -0
  15. data/lib/rspec/mocks/framework.rb +15 -0
  16. data/lib/rspec/mocks/message_expectation.rb +326 -0
  17. data/lib/rspec/mocks/methods.rb +63 -0
  18. data/lib/rspec/mocks/mock.rb +65 -0
  19. data/lib/rspec/mocks/order_group.rb +29 -0
  20. data/lib/rspec/mocks/proxy.rb +230 -0
  21. data/lib/rspec/mocks/space.rb +28 -0
  22. data/lib/rspec/mocks/spec_methods.rb +39 -0
  23. data/lib/spec/mocks.rb +1 -0
  24. data/spec/rspec/mocks/any_number_of_times_spec.rb +36 -0
  25. data/spec/rspec/mocks/argument_expectation_spec.rb +23 -0
  26. data/spec/rspec/mocks/at_least_spec.rb +97 -0
  27. data/spec/rspec/mocks/at_most_spec.rb +93 -0
  28. data/spec/rspec/mocks/bug_report_10260_spec.rb +8 -0
  29. data/spec/rspec/mocks/bug_report_10263_spec.rb +27 -0
  30. data/spec/rspec/mocks/bug_report_11545_spec.rb +32 -0
  31. data/spec/rspec/mocks/bug_report_15719_spec.rb +29 -0
  32. data/spec/rspec/mocks/bug_report_496_spec.rb +17 -0
  33. data/spec/rspec/mocks/bug_report_600_spec.rb +22 -0
  34. data/spec/rspec/mocks/bug_report_7611_spec.rb +19 -0
  35. data/spec/rspec/mocks/bug_report_7805_spec.rb +22 -0
  36. data/spec/rspec/mocks/bug_report_8165_spec.rb +31 -0
  37. data/spec/rspec/mocks/bug_report_8302_spec.rb +26 -0
  38. data/spec/rspec/mocks/bug_report_830_spec.rb +21 -0
  39. data/spec/rspec/mocks/failing_argument_matchers_spec.rb +95 -0
  40. data/spec/rspec/mocks/hash_including_matcher_spec.rb +90 -0
  41. data/spec/rspec/mocks/hash_not_including_matcher_spec.rb +67 -0
  42. data/spec/rspec/mocks/mock_ordering_spec.rb +94 -0
  43. data/spec/rspec/mocks/mock_space_spec.rb +54 -0
  44. data/spec/rspec/mocks/mock_spec.rb +583 -0
  45. data/spec/rspec/mocks/multiple_return_value_spec.rb +113 -0
  46. data/spec/rspec/mocks/nil_expectation_warning_spec.rb +63 -0
  47. data/spec/rspec/mocks/null_object_mock_spec.rb +54 -0
  48. data/spec/rspec/mocks/once_counts_spec.rb +53 -0
  49. data/spec/rspec/mocks/options_hash_spec.rb +35 -0
  50. data/spec/rspec/mocks/partial_mock_spec.rb +164 -0
  51. data/spec/rspec/mocks/partial_mock_using_mocks_directly_spec.rb +66 -0
  52. data/spec/rspec/mocks/passing_argument_matchers_spec.rb +145 -0
  53. data/spec/rspec/mocks/precise_counts_spec.rb +52 -0
  54. data/spec/rspec/mocks/record_messages_spec.rb +26 -0
  55. data/spec/rspec/mocks/stub_chain_spec.rb +34 -0
  56. data/spec/rspec/mocks/stub_implementation_spec.rb +31 -0
  57. data/spec/rspec/mocks/stub_spec.rb +198 -0
  58. data/spec/rspec/mocks/stubbed_message_expectations_spec.rb +26 -0
  59. data/spec/rspec/mocks/twice_counts_spec.rb +67 -0
  60. data/spec/spec_helper.rb +52 -0
  61. data/spec/support/macros.rb +29 -0
  62. metadata +172 -0
@@ -0,0 +1,113 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper'
2
+
3
+ module Rspec
4
+ module Mocks
5
+ describe "a Mock expectation with multiple return values and no specified count" do
6
+ before(:each) do
7
+ @mock = Mock.new("mock")
8
+ @return_values = ["1",2,Object.new]
9
+ @mock.should_receive(:message).and_return(@return_values[0],@return_values[1],@return_values[2])
10
+ end
11
+
12
+ it "should return values in order to consecutive calls" do
13
+ @mock.message.should == @return_values[0]
14
+ @mock.message.should == @return_values[1]
15
+ @mock.message.should == @return_values[2]
16
+ @mock.rspec_verify
17
+ end
18
+
19
+ it "should complain when there are too few calls" do
20
+ third = Object.new
21
+ @mock.message.should == @return_values[0]
22
+ @mock.message.should == @return_values[1]
23
+ lambda { @mock.rspec_verify }.should raise_error(MockExpectationError, "Mock 'mock' expected :message with (any args) 3 times, but received it twice")
24
+ end
25
+
26
+ it "should complain when there are too many calls" do
27
+ third = Object.new
28
+ @mock.message.should == @return_values[0]
29
+ @mock.message.should == @return_values[1]
30
+ @mock.message.should == @return_values[2]
31
+ @mock.message.should == @return_values[2]
32
+ lambda { @mock.rspec_verify }.should raise_error(MockExpectationError, "Mock 'mock' expected :message with (any args) 3 times, but received it 4 times")
33
+ end
34
+ end
35
+
36
+ describe "a Mock expectation with multiple return values with a specified count equal to the number of values" do
37
+ before(:each) do
38
+ @mock = Mock.new("mock")
39
+ @return_values = ["1",2,Object.new]
40
+ @mock.should_receive(:message).exactly(3).times.and_return(@return_values[0],@return_values[1],@return_values[2])
41
+ end
42
+
43
+ it "should return values in order to consecutive calls" do
44
+ @mock.message.should == @return_values[0]
45
+ @mock.message.should == @return_values[1]
46
+ @mock.message.should == @return_values[2]
47
+ @mock.rspec_verify
48
+ end
49
+
50
+ it "should complain when there are too few calls" do
51
+ third = Object.new
52
+ @mock.message.should == @return_values[0]
53
+ @mock.message.should == @return_values[1]
54
+ lambda { @mock.rspec_verify }.should raise_error(MockExpectationError, "Mock 'mock' expected :message with (any args) 3 times, but received it twice")
55
+ end
56
+
57
+ it "should complain when there are too many calls" do
58
+ third = Object.new
59
+ @mock.message.should == @return_values[0]
60
+ @mock.message.should == @return_values[1]
61
+ @mock.message.should == @return_values[2]
62
+ @mock.message.should == @return_values[2]
63
+ lambda { @mock.rspec_verify }.should raise_error(MockExpectationError, "Mock 'mock' expected :message with (any args) 3 times, but received it 4 times")
64
+ end
65
+ end
66
+
67
+ describe "a Mock expectation with multiple return values specifying at_least less than the number of values" do
68
+ before(:each) do
69
+ @mock = Mock.new("mock")
70
+ @mock.should_receive(:message).at_least(:twice).with(no_args).and_return(11, 22)
71
+ end
72
+
73
+ it "should use last return value for subsequent calls" do
74
+ @mock.message.should equal(11)
75
+ @mock.message.should equal(22)
76
+ @mock.message.should equal(22)
77
+ @mock.rspec_verify
78
+ end
79
+
80
+ it "should fail when called less than the specified number" do
81
+ @mock.message.should equal(11)
82
+ lambda { @mock.rspec_verify }.should raise_error(MockExpectationError, "Mock 'mock' expected :message with (no args) twice, but received it once")
83
+ end
84
+ end
85
+ describe "a Mock expectation with multiple return values with a specified count larger than the number of values" do
86
+ before(:each) do
87
+ @mock = Mock.new("mock")
88
+ @mock.should_receive(:message).exactly(3).times.and_return(11, 22)
89
+ end
90
+
91
+ it "should use last return value for subsequent calls" do
92
+ @mock.message.should equal(11)
93
+ @mock.message.should equal(22)
94
+ @mock.message.should equal(22)
95
+ @mock.rspec_verify
96
+ end
97
+
98
+ it "should fail when called less than the specified number" do
99
+ @mock.message.should equal(11)
100
+ lambda { @mock.rspec_verify }.should raise_error(MockExpectationError, "Mock 'mock' expected :message with (any args) 3 times, but received it once")
101
+ end
102
+
103
+ it "should fail when called greater than the specified number" do
104
+ @mock.message.should equal(11)
105
+ @mock.message.should equal(22)
106
+ @mock.message.should equal(22)
107
+ @mock.message.should equal(22)
108
+ lambda { @mock.rspec_verify }.should raise_error(MockExpectationError, "Mock 'mock' expected :message with (any args) 3 times, but received it 4 times")
109
+ end
110
+ end
111
+ end
112
+ end
113
+
@@ -0,0 +1,63 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ def remove_last_describe_from_world
4
+ Rspec::Core.world.behaviours.pop
5
+ end
6
+
7
+ def empty_example_group
8
+ group = 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
+
17
+ it "should issue a warning with file and line number information" do
18
+ expected_warning = %r%An expectation of :foo was set on nil. Called from #{__FILE__}:#{__LINE__+3}(:in `block \(2 levels\) in <module:Mocks>')?. Use allow_message_expectations_on_nil to disable warnings.%
19
+ Kernel.should_receive(:warn).with(expected_warning)
20
+
21
+ nil.should_receive(:foo)
22
+ nil.foo
23
+ end
24
+
25
+ it "should issue a warning when the expectation is negative" do
26
+ Kernel.should_receive(:warn)
27
+
28
+ nil.should_not_receive(:foo)
29
+ end
30
+
31
+ it "should not issue a warning when expectations are set to be allowed" do
32
+ allow_message_expectations_on_nil
33
+ Kernel.should_not_receive(:warn)
34
+
35
+ nil.should_receive(:foo)
36
+ nil.should_not_receive(:bar)
37
+ nil.foo
38
+ end
39
+
40
+ end
41
+
42
+ describe "#allow_message_expectations_on_nil" do
43
+
44
+
45
+ it "should not effect subsequent examples" do
46
+ example_group = empty_example_group
47
+ example_group.it("when called in one example that doesn't end up setting an expectation on nil") do
48
+ allow_message_expectations_on_nil
49
+ end
50
+ example_group.it("should not effect the next exapmle ran") do
51
+ Kernel.should_receive(:warn)
52
+ nil.should_receive(:foo)
53
+ nil.foo
54
+ end
55
+
56
+ example_group
57
+
58
+ end
59
+
60
+ end
61
+
62
+ end
63
+ end
@@ -0,0 +1,54 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ module Rspec
4
+ module Mocks
5
+ describe "a mock acting as a NullObject" do
6
+ before(:each) do
7
+ @mock = Mock.new("null_object", :null_object => true)
8
+ end
9
+
10
+ it "should allow explicit expectation" do
11
+ @mock.should_receive(:something)
12
+ @mock.something
13
+ end
14
+
15
+ it "should fail verification when explicit exception not met" do
16
+ lambda do
17
+ @mock.should_receive(:something)
18
+ @mock.rspec_verify
19
+ end.should raise_error(MockExpectationError)
20
+ end
21
+
22
+ it "should ignore unexpected methods" do
23
+ @mock.random_call("a", "d", "c")
24
+ @mock.rspec_verify
25
+ end
26
+
27
+ it "should expected message with different args first" do
28
+ @mock.should_receive(:message).with(:expected_arg)
29
+ @mock.message(:unexpected_arg)
30
+ @mock.message(:expected_arg)
31
+ end
32
+
33
+ it "should expected message with different args second" do
34
+ @mock.should_receive(:message).with(:expected_arg)
35
+ @mock.message(:expected_arg)
36
+ @mock.message(:unexpected_arg)
37
+ end
38
+ end
39
+
40
+ describe "#null_object?" do
41
+ it "should default to false" do
42
+ obj = mock('anything')
43
+ obj.should_not be_null_object
44
+ end
45
+ end
46
+
47
+ describe "#as_null_object" do
48
+ it "should set the object to null_object" do
49
+ obj = mock('anything').as_null_object
50
+ obj.should be_null_object
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,53 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ module Rspec
4
+ module Mocks
5
+ describe "OnceCounts" do
6
+ before(:each) do
7
+ @mock = mock("test mock")
8
+ end
9
+
10
+ it "once should fail when called once with wrong args" do
11
+ @mock.should_receive(:random_call).once.with("a", "b", "c")
12
+ lambda do
13
+ @mock.random_call("d", "e", "f")
14
+ end.should raise_error(MockExpectationError)
15
+ @mock.rspec_reset
16
+ end
17
+
18
+ it "once should fail when called twice" do
19
+ @mock.should_receive(:random_call).once
20
+ @mock.random_call
21
+ @mock.random_call
22
+ lambda do
23
+ @mock.rspec_verify
24
+ end.should raise_error(MockExpectationError)
25
+ end
26
+
27
+ it "once should fail when not called" do
28
+ @mock.should_receive(:random_call).once
29
+ lambda do
30
+ @mock.rspec_verify
31
+ end.should raise_error(MockExpectationError)
32
+ end
33
+
34
+ it "once should pass when called once" do
35
+ @mock.should_receive(:random_call).once
36
+ @mock.random_call
37
+ @mock.rspec_verify
38
+ end
39
+
40
+ it "once should pass when called once with specified args" do
41
+ @mock.should_receive(:random_call).once.with("a", "b", "c")
42
+ @mock.random_call("a", "b", "c")
43
+ @mock.rspec_verify
44
+ end
45
+
46
+ it "once should pass when called once with unspecified args" do
47
+ @mock.should_receive(:random_call).once
48
+ @mock.random_call("a", "b", "c")
49
+ @mock.rspec_verify
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,35 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ module Rspec
4
+ module Mocks
5
+ describe "calling :should_receive with an options hash" do
6
+ it "should report 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 "should use 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 "should use 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
@@ -0,0 +1,164 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
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 "should name the class in the failure message" do
11
+ @object.should_receive(:foo)
12
+ lambda do
13
+ @object.rspec_verify
14
+ end.should raise_error(Rspec::Mocks::MockExpectationError, /<Object:.*> expected/)
15
+ end
16
+
17
+ it "should name 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 "should 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 should mock out the method" do
31
+ @object.should_not_receive(:fuhbar)
32
+ lambda do
33
+ @object.fuhbar
34
+ end.should raise_error(MockExpectationError, /<Object:.*> expected :fuhbar with \(no args\) 0 times/)
35
+ end
36
+
37
+ it "should_not_receive should return a negative message expectation" do
38
+ @object.should_not_receive(:foobar).should be_kind_of(NegativeMessageExpectation)
39
+ end
40
+
41
+ it "should_receive should mock out the method" do
42
+ @object.should_receive(:foobar).with(:test_param).and_return(1)
43
+ @object.foobar(:test_param).should equal(1)
44
+ end
45
+
46
+ it "should_receive should handle a hash" do
47
+ @object.should_receive(:foobar).with(:key => "value").and_return(1)
48
+ @object.foobar(:key => "value").should equal(1)
49
+ end
50
+
51
+ it "should_receive should handle an inner hash" do
52
+ hash = {:a => {:key => "value"}}
53
+ @object.should_receive(:foobar).with(:key => "value").and_return(1)
54
+ @object.foobar(hash[:a]).should equal(1)
55
+ end
56
+
57
+ it "should_receive should return a message expectation" do
58
+ @object.should_receive(:foobar).should be_kind_of(MessageExpectation)
59
+ @object.foobar
60
+ end
61
+
62
+ it "should_receive should verify method was called" do
63
+ @object.should_receive(:foobar).with(:test_param).and_return(1)
64
+ lambda do
65
+ @object.rspec_verify
66
+ end.should raise_error(Rspec::Mocks::MockExpectationError)
67
+ end
68
+
69
+ it "should_receive should also take a String argument" do
70
+ @object.should_receive('foobar')
71
+ @object.foobar
72
+ end
73
+
74
+ it "should_not_receive should also take a String argument" do
75
+ @object.should_not_receive('foobar')
76
+ lambda do
77
+ @object.foobar
78
+ end.should raise_error(Rspec::Mocks::MockExpectationError)
79
+ end
80
+
81
+ it "should use report nil in the error message" do
82
+ allow_message_expectations_on_nil
83
+
84
+ @this_will_resolve_to_nil.should_receive(:foobar)
85
+ lambda do
86
+ @this_will_resolve_to_nil.rspec_verify
87
+ end.should raise_error(Rspec::Mocks::MockExpectationError, /nil expected :foobar with/)
88
+ end
89
+ end
90
+
91
+ describe "Partially mocking an object that defines ==, after another mock has been defined" do
92
+ before(:each) do
93
+ stub("existing mock", :foo => :foo)
94
+ end
95
+
96
+ class PartiallyMockedEquals
97
+ attr_reader :val
98
+ def initialize(val)
99
+ @val = val
100
+ end
101
+
102
+ def ==(other)
103
+ @val == other.val
104
+ end
105
+ end
106
+
107
+ it "should not raise an error when stubbing the object" do
108
+ o = PartiallyMockedEquals.new :foo
109
+ lambda { o.stub!(:bar) }.should_not raise_error(NoMethodError)
110
+ end
111
+ end
112
+
113
+ describe "Method visibility when using partial mocks" do
114
+ class MockableClass
115
+ def public_method
116
+ private_method
117
+ protected_method
118
+ end
119
+ protected
120
+ def protected_method; end
121
+ private
122
+ def private_method; end
123
+ end
124
+
125
+ before(:each) do
126
+ @object = MockableClass.new
127
+ end
128
+
129
+ it 'should keep public methods public' do
130
+ @object.should_receive(:public_method)
131
+ with_ruby('1.9') do
132
+ @object.public_methods.should include(:public_method)
133
+ end
134
+ with_ruby('1.8') do
135
+ @object.public_methods.should include('public_method')
136
+ end
137
+ @object.public_method
138
+ end
139
+
140
+ it 'should keep private methods private' do
141
+ @object.should_receive(:private_method)
142
+ with_ruby('1.9') do
143
+ @object.private_methods.should include(:private_method)
144
+ end
145
+ with_ruby('1.8') do
146
+ @object.private_methods.should include('private_method')
147
+ end
148
+ @object.public_method
149
+ end
150
+
151
+ it 'should keep protected methods protected' do
152
+ @object.should_receive(:protected_method)
153
+ with_ruby('1.9') do
154
+ @object.protected_methods.should include(:protected_method)
155
+ end
156
+ with_ruby('1.8') do
157
+ @object.protected_methods.should include('protected_method')
158
+ end
159
+ @object.public_method
160
+ end
161
+
162
+ end
163
+ end
164
+ end