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,97 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ module Rspec
4
+ module Mocks
5
+ describe "at_least" do
6
+ before(:each) do
7
+ @mock = Mock.new("test mock")
8
+ end
9
+
10
+ it "should fail if method is never called" do
11
+ @mock.should_receive(:random_call).at_least(4).times
12
+ lambda do
13
+ @mock.rspec_verify
14
+ end.should raise_error(MockExpectationError)
15
+ end
16
+
17
+ it "should fail when called less than n times" do
18
+ @mock.should_receive(:random_call).at_least(4).times
19
+ @mock.random_call
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 "should fail when at least once method is never called" do
28
+ @mock.should_receive(:random_call).at_least(:once)
29
+ lambda do
30
+ @mock.rspec_verify
31
+ end.should raise_error(MockExpectationError)
32
+ end
33
+
34
+ it "should fail when at least twice method is called once" do
35
+ @mock.should_receive(:random_call).at_least(:twice)
36
+ @mock.random_call
37
+ lambda do
38
+ @mock.rspec_verify
39
+ end.should raise_error(MockExpectationError)
40
+ end
41
+
42
+ it "should fail when at least twice method is never called" do
43
+ @mock.should_receive(:random_call).at_least(:twice)
44
+ lambda do
45
+ @mock.rspec_verify
46
+ end.should raise_error(MockExpectationError)
47
+ end
48
+
49
+ it "should pass when at least n times method is called exactly n times" do
50
+ @mock.should_receive(:random_call).at_least(4).times
51
+ @mock.random_call
52
+ @mock.random_call
53
+ @mock.random_call
54
+ @mock.random_call
55
+ @mock.rspec_verify
56
+ end
57
+
58
+ it "should pass when at least n times method is called n plus 1 times" do
59
+ @mock.should_receive(:random_call).at_least(4).times
60
+ @mock.random_call
61
+ @mock.random_call
62
+ @mock.random_call
63
+ @mock.random_call
64
+ @mock.random_call
65
+ @mock.rspec_verify
66
+ end
67
+
68
+ it "should pass when at least once method is called once" do
69
+ @mock.should_receive(:random_call).at_least(:once)
70
+ @mock.random_call
71
+ @mock.rspec_verify
72
+ end
73
+
74
+ it "should pass when at least once method is called twice" do
75
+ @mock.should_receive(:random_call).at_least(:once)
76
+ @mock.random_call
77
+ @mock.random_call
78
+ @mock.rspec_verify
79
+ end
80
+
81
+ it "should pass when at least twice method is called three times" do
82
+ @mock.should_receive(:random_call).at_least(:twice)
83
+ @mock.random_call
84
+ @mock.random_call
85
+ @mock.random_call
86
+ @mock.rspec_verify
87
+ end
88
+
89
+ it "should pass when at least twice method is called twice" do
90
+ @mock.should_receive(:random_call).at_least(:twice)
91
+ @mock.random_call
92
+ @mock.random_call
93
+ @mock.rspec_verify
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,93 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ module Rspec
4
+ module Mocks
5
+ describe "at_most" do
6
+ before(:each) do
7
+ @mock = Mock.new("test mock")
8
+ end
9
+
10
+ it "should fail when at most n times method is called n plus 1 times" do
11
+ @mock.should_receive(:random_call).at_most(4).times
12
+ @mock.random_call
13
+ @mock.random_call
14
+ @mock.random_call
15
+ @mock.random_call
16
+ @mock.random_call
17
+ lambda do
18
+ @mock.rspec_verify
19
+ end.should raise_error(MockExpectationError)
20
+ end
21
+
22
+ it "should fail when at most once method is called twice" do
23
+ @mock.should_receive(:random_call).at_most(:once)
24
+ @mock.random_call
25
+ @mock.random_call
26
+ lambda do
27
+ @mock.rspec_verify
28
+ end.should raise_error(MockExpectationError)
29
+ end
30
+
31
+ it "should fail when at most twice method is called three times" do
32
+ @mock.should_receive(:random_call).at_most(:twice)
33
+ @mock.random_call
34
+ @mock.random_call
35
+ @mock.random_call
36
+ lambda do
37
+ @mock.rspec_verify
38
+ end.should raise_error(MockExpectationError)
39
+ end
40
+
41
+ it "should pass when at most n times method is called exactly n times" do
42
+ @mock.should_receive(:random_call).at_most(4).times
43
+ @mock.random_call
44
+ @mock.random_call
45
+ @mock.random_call
46
+ @mock.random_call
47
+ @mock.rspec_verify
48
+ end
49
+
50
+ it "should pass when at most n times method is called less than n times" do
51
+ @mock.should_receive(:random_call).at_most(4).times
52
+ @mock.random_call
53
+ @mock.random_call
54
+ @mock.random_call
55
+ @mock.rspec_verify
56
+ end
57
+
58
+ it "should pass when at most n times method is never called" do
59
+ @mock.should_receive(:random_call).at_most(4).times
60
+ @mock.rspec_verify
61
+ end
62
+
63
+ it "should pass when at most once method is called once" do
64
+ @mock.should_receive(:random_call).at_most(:once)
65
+ @mock.random_call
66
+ @mock.rspec_verify
67
+ end
68
+
69
+ it "should pass when at most once method is never called" do
70
+ @mock.should_receive(:random_call).at_most(:once)
71
+ @mock.rspec_verify
72
+ end
73
+
74
+ it "should pass when at most twice method is called once" do
75
+ @mock.should_receive(:random_call).at_most(:twice)
76
+ @mock.random_call
77
+ @mock.rspec_verify
78
+ end
79
+
80
+ it "should pass when at most twice method is called twice" do
81
+ @mock.should_receive(:random_call).at_most(:twice)
82
+ @mock.random_call
83
+ @mock.random_call
84
+ @mock.rspec_verify
85
+ end
86
+
87
+ it "should pass when at most twice method is never called" do
88
+ @mock.should_receive(:random_call).at_most(:twice)
89
+ @mock.rspec_verify
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,8 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ describe "An RSpec Mock" do
4
+ it "should hide internals in its inspect representation" do
5
+ m = mock('cup')
6
+ m.inspect.should =~ /#<Rspec::Mocks::Mock:0x[a-f0-9.]+ @name="cup">/
7
+ end
8
+ end
@@ -0,0 +1,27 @@
1
+ describe "Mock" do
2
+ before do
3
+ @mock = mock("test mock")
4
+ end
5
+
6
+ specify "when one example has an expectation (non-mock) inside the block passed to the mock" do
7
+ @mock.should_receive(:msg) do |b|
8
+ b.should be_true #this call exposes the problem
9
+ end
10
+ begin
11
+ @mock.msg(false)
12
+ rescue Exception
13
+ end
14
+ end
15
+
16
+ specify "then the next example should behave as expected instead of saying" do
17
+ @mock.should_receive(:foobar)
18
+ @mock.foobar
19
+ @mock.rspec_verify
20
+ begin
21
+ @mock.foobar
22
+ rescue Exception => e
23
+ e.message.should == "Mock 'test mock' received unexpected message :foobar with (no args)"
24
+ end
25
+ end
26
+ end
27
+
@@ -0,0 +1,32 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ class LiarLiarPantsOnFire
4
+ def respond_to?(sym, incl_private=false)
5
+ true
6
+ end
7
+
8
+ def self.respond_to?(sym, incl_private=false)
9
+ true
10
+ end
11
+ end
12
+
13
+ describe 'should_receive' do
14
+ before(:each) do
15
+ @liar = LiarLiarPantsOnFire.new
16
+ end
17
+
18
+ it "should work when object lies about responding to a method" do
19
+ @liar.should_receive(:something)
20
+ @liar.something
21
+ end
22
+
23
+ it 'should work when class lies about responding to a method' do
24
+ LiarLiarPantsOnFire.should_receive(:something)
25
+ LiarLiarPantsOnFire.something
26
+ end
27
+
28
+ it 'should cleanup after itself' do
29
+ (class << LiarLiarPantsOnFire; self; end).instance_methods.should_not include("something")
30
+ end
31
+ end
32
+
@@ -0,0 +1,29 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ module Rspec
4
+ module Mocks
5
+ describe "mock failure" do
6
+
7
+ it "should tell you when it receives the right message with the wrong args" do
8
+ m = mock("foo")
9
+ m.should_receive(:bar).with("message")
10
+ lambda {
11
+ m.bar("different message")
12
+ }.should raise_error(Rspec::Mocks::MockExpectationError, %Q{Mock 'foo' expected :bar with ("message") but received it with ("different message")})
13
+ m.bar("message") # allows the spec to pass
14
+ end
15
+
16
+ pending "should tell you when it receives the right message with the wrong args if you stub the method (fix bug 15719)" do
17
+ # NOTE - for whatever reason, if you use a the block style of pending here,
18
+ # rcov gets unhappy. Don't know why yet.
19
+ m = mock("foo")
20
+ m.stub!(:bar)
21
+ m.should_receive(:bar).with("message")
22
+ lambda {
23
+ m.bar("different message")
24
+ }.should raise_error(Rspec::Mocks::MockExpectationError, %Q{Mock 'foo' expected :bar with ("message") but received it with ("different message")})
25
+ m.bar("message") # allows the spec to pass
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,17 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ module BugReport496
4
+ class BaseClass
5
+ end
6
+
7
+ class SubClass < BaseClass
8
+ end
9
+
10
+ describe "a message expectation on a base class object" do
11
+ pending "should correctly pick up message sent to it subclass (awaiting fix for http://rspec.lighthouseapp.com/projects/5645/tickets/496)" do
12
+ BaseClass.should_receive(:new).once
13
+ SubClass.new
14
+ end
15
+ end
16
+ end
17
+
@@ -0,0 +1,22 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ module BugReport600
4
+ class ExampleClass
5
+ def self.method_that_uses_define_method
6
+ define_method "defined_method" do |attributes|
7
+ load_address(address, attributes)
8
+ end
9
+ end
10
+ end
11
+
12
+ describe "stubbing a class method" do
13
+ it "should work" do
14
+ ExampleClass.should_receive(:define_method).with("defined_method")
15
+ ExampleClass.method_that_uses_define_method
16
+ end
17
+
18
+ it "should restore the original method" do
19
+ ExampleClass.method_that_uses_define_method
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,19 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ module Bug7611
4
+ class Foo
5
+ end
6
+
7
+ class Bar < Foo
8
+ end
9
+
10
+ describe "A Partial Mock" do
11
+ it "should respect subclasses" do
12
+ Foo.stub!(:new).and_return(Object.new)
13
+ end
14
+
15
+ it "should" do
16
+ Bar.new.class.should == Bar
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ module Bug7805
4
+ #This is really a duplicate of 8302
5
+
6
+ describe "Stubs should correctly restore module methods" do
7
+ it "1 - stub the open method" do
8
+ File.stub!(:open).and_return("something")
9
+ File.open.should == "something"
10
+ end
11
+ it "2 - use File.open to create example.txt" do
12
+ filename = "#{File.dirname(__FILE__)}/example-#{Time.new.to_i}.txt"
13
+ File.exist?(filename).should be_false
14
+ file = File.open(filename,'w')
15
+ file.close
16
+ File.exist?(filename).should be_true
17
+ File.delete(filename)
18
+ File.exist?(filename).should be_false
19
+ end
20
+ end
21
+
22
+ end
@@ -0,0 +1,31 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ describe "An object where respond_to? is true and does not have method" do
4
+ # When should_receive(:sym) is sent to any object, the Proxy sends
5
+ # respond_to?(:sym) to that object to see if the method should be proxied.
6
+ #
7
+ # If respond_to? itself is proxied, then when the Proxy sends respond_to?
8
+ # to the object, the proxy is invoked and responds yes (if so set in the spec).
9
+ # When the object does NOT actually respond to :sym, an exception is thrown
10
+ # when trying to proxy it.
11
+ #
12
+ # The fix was to keep track of whether :respond_to? had been proxied and, if
13
+ # so, call the munged copy of :respond_to? on the object.
14
+
15
+ it "should not raise an exception for Object" do
16
+ obj = Object.new
17
+ obj.should_receive(:respond_to?).with(:foobar).and_return(true)
18
+ obj.should_receive(:foobar).and_return(:baz)
19
+ obj.respond_to?(:foobar).should be_true
20
+ obj.foobar.should == :baz
21
+ end
22
+
23
+ it "should not raise an exception for mock" do
24
+ obj = mock("obj")
25
+ obj.should_receive(:respond_to?).with(:foobar).and_return(true)
26
+ obj.should_receive(:foobar).and_return(:baz)
27
+ obj.respond_to?(:foobar).should be_true
28
+ obj.foobar.should == :baz
29
+ end
30
+
31
+ end
@@ -0,0 +1,26 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ module Bug8302
4
+ class Foo
5
+ def Foo.class_method(arg)
6
+ end
7
+
8
+ def instance_bar(arg)
9
+ end
10
+ end
11
+
12
+ describe "Bug report 8302:" do
13
+ it "class method is not restored correctly when proxied" do
14
+ Foo.should_not_receive(:class_method).with(Array.new)
15
+ Foo.rspec_verify
16
+ Foo.class_method(Array.new)
17
+ end
18
+
19
+ it "instance method is not restored correctly when proxied" do
20
+ foo = Foo.new
21
+ foo.should_not_receive(:instance_bar).with(Array.new)
22
+ foo.rspec_verify
23
+ foo.instance_bar(Array.new)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,21 @@
1
+ require File.dirname(__FILE__) + '/../../spec_helper.rb'
2
+
3
+ module Rspec
4
+ module Mocks
5
+ describe 'Calling a method that catches StandardError' do
6
+ class Foo
7
+ def self.foo
8
+ bar
9
+ rescue StandardError
10
+ end
11
+ end
12
+
13
+ it 'still reports mock failures' do
14
+ Foo.should_not_receive :bar
15
+ lambda do
16
+ Foo.foo
17
+ end.should raise_error(MockExpectationError)
18
+ end
19
+ end
20
+ end
21
+ end