Skip to content

Commit

Permalink
When not as_null_object, respond_to? now behaves as expected.
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Sep 13, 2010
1 parent 56323d9 commit dae790f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/rspec/mocks/mock.rb
Expand Up @@ -35,7 +35,7 @@ def to_s
alias_method :to_str, :to_s alias_method :to_str, :to_s


def respond_to?(sym, incl_private=false) def respond_to?(sym, incl_private=false)
__mock_proxy.respond_to?(sym, incl_private) __mock_proxy.null_object? ? true : super
end end


private private
Expand Down
4 changes: 0 additions & 4 deletions lib/rspec/mocks/proxy.rb
Expand Up @@ -38,10 +38,6 @@ def null_object?
@null_object @null_object
end end


def respond_to?(sym, incl_private=false)
null_object? ? true : super
end

# Tells the object to ignore any messages that aren't explicitly set as # Tells the object to ignore any messages that aren't explicitly set as
# stubs or message expectations. # stubs or message expectations.
def as_null_object def as_null_object
Expand Down
63 changes: 39 additions & 24 deletions spec/rspec/mocks/null_object_mock_spec.rb
Expand Up @@ -2,42 +2,64 @@


module RSpec module RSpec
module Mocks module Mocks
describe "a mock acting as a NullObject" do describe "a double _not_ acting as a null object" do
before(:each) do before(:each) do
@mock = RSpec::Mocks::Mock.new("null_object").as_null_object @double = double('non-null object')
end

it "says it does not respond to messages it doesn't understand" do
@double.should_not respond_to(:foo)
end

it "says it responds to messages it does understand" do
@double.stub(:foo)
@double.should respond_to(:foo)
end
end

describe "a double acting as a null object" do
before(:each) do
@double = double('null object').as_null_object
end

it "says it responds to everything" do
@double.should respond_to(:any_message_it_gets)
end end


it "allows explicit expectation" do it "allows explicit expectation" do
@mock.should_receive(:something) @double.should_receive(:something)
@mock.something @double.something
end end


it "fails verification when explicit exception not met" do it "fails verification when explicit exception not met" do
lambda do lambda do
@mock.should_receive(:something) @double.should_receive(:something)
@mock.rspec_verify @double.rspec_verify
end.should raise_error(RSpec::Mocks::MockExpectationError) end.should raise_error(RSpec::Mocks::MockExpectationError)
end end


it "ignores unexpected methods" do it "ignores unexpected methods" do
@mock.random_call("a", "d", "c") @double.random_call("a", "d", "c")
@mock.rspec_verify @double.rspec_verify
end end


it "allows expected message with different args first" do it "allows expected message with different args first" do
@mock.should_receive(:message).with(:expected_arg) @double.should_receive(:message).with(:expected_arg)
@mock.message(:unexpected_arg) @double.message(:unexpected_arg)
@mock.message(:expected_arg) @double.message(:expected_arg)
end end


it "allows expected message with different args second" do it "allows expected message with different args second" do
@mock.should_receive(:message).with(:expected_arg) @double.should_receive(:message).with(:expected_arg)
@mock.message(:expected_arg) @double.message(:expected_arg)
@mock.message(:unexpected_arg) @double.message(:unexpected_arg)
end end

end
it "responds to everything" do
@mock.should respond_to(:any_message_it_gets) describe "#as_null_object" do
it "sets the object to null_object" do
obj = double('anything').as_null_object
obj.should be_null_object
end end
end end


Expand All @@ -47,12 +69,5 @@ module Mocks
obj.should_not be_null_object obj.should_not be_null_object
end end
end end

describe "#as_null_object" do
it "sets the object to null_object" do
obj = double('anything').as_null_object
obj.should be_null_object
end
end
end end
end end

0 comments on commit dae790f

Please sign in to comment.