Skip to content
This repository
Browse code

When not as_null_object, respond_to? now behaves as expected.

  • Loading branch information...
commit dae790fdc9eff8350dcd0de8bf16ab87d435db4a 1 parent 56323d9
David Chelimsky dchelimsky authored
2  lib/rspec/mocks/mock.rb
@@ -35,7 +35,7 @@ def to_s
35 35 alias_method :to_str, :to_s
36 36
37 37 def respond_to?(sym, incl_private=false)
38   - __mock_proxy.respond_to?(sym, incl_private)
  38 + __mock_proxy.null_object? ? true : super
39 39 end
40 40
41 41 private
4 lib/rspec/mocks/proxy.rb
@@ -38,10 +38,6 @@ def null_object?
38 38 @null_object
39 39 end
40 40
41   - def respond_to?(sym, incl_private=false)
42   - null_object? ? true : super
43   - end
44   -
45 41 # Tells the object to ignore any messages that aren't explicitly set as
46 42 # stubs or message expectations.
47 43 def as_null_object
63 spec/rspec/mocks/null_object_mock_spec.rb
@@ -2,42 +2,64 @@
2 2
3 3 module RSpec
4 4 module Mocks
5   - describe "a mock acting as a NullObject" do
  5 + describe "a double _not_ acting as a null object" do
6 6 before(:each) do
7   - @mock = RSpec::Mocks::Mock.new("null_object").as_null_object
  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 + end
  19 +
  20 + describe "a double acting as a null object" do
  21 + before(:each) do
  22 + @double = double('null object').as_null_object
  23 + end
  24 +
  25 + it "says it responds to everything" do
  26 + @double.should respond_to(:any_message_it_gets)
8 27 end
9 28
10 29 it "allows explicit expectation" do
11   - @mock.should_receive(:something)
12   - @mock.something
  30 + @double.should_receive(:something)
  31 + @double.something
13 32 end
14 33
15 34 it "fails verification when explicit exception not met" do
16 35 lambda do
17   - @mock.should_receive(:something)
18   - @mock.rspec_verify
  36 + @double.should_receive(:something)
  37 + @double.rspec_verify
19 38 end.should raise_error(RSpec::Mocks::MockExpectationError)
20 39 end
21 40
22 41 it "ignores unexpected methods" do
23   - @mock.random_call("a", "d", "c")
24   - @mock.rspec_verify
  42 + @double.random_call("a", "d", "c")
  43 + @double.rspec_verify
25 44 end
26 45
27 46 it "allows 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)
  47 + @double.should_receive(:message).with(:expected_arg)
  48 + @double.message(:unexpected_arg)
  49 + @double.message(:expected_arg)
31 50 end
32 51
33 52 it "allows 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)
  53 + @double.should_receive(:message).with(:expected_arg)
  54 + @double.message(:expected_arg)
  55 + @double.message(:unexpected_arg)
37 56 end
38   -
39   - it "responds to everything" do
40   - @mock.should respond_to(:any_message_it_gets)
  57 + end
  58 +
  59 + describe "#as_null_object" do
  60 + it "sets the object to null_object" do
  61 + obj = double('anything').as_null_object
  62 + obj.should be_null_object
41 63 end
42 64 end
43 65
@@ -47,12 +69,5 @@ module Mocks
47 69 obj.should_not be_null_object
48 70 end
49 71 end
50   -
51   - describe "#as_null_object" do
52   - it "sets the object to null_object" do
53   - obj = double('anything').as_null_object
54   - obj.should be_null_object
55   - end
56   - end
57 72 end
58 73 end

0 comments on commit dae790f

Please sign in to comment.
Something went wrong with that request. Please try again.