Permalink
Browse files

Mock#to_ary raises a NoMethodError

- supports Array#flatten with a Mock in the Array
- Closes #31.
  • Loading branch information...
1 parent 66737e5 commit 1907cf9bd8ee735bc1f9bb16b8c8a346b391ba50 @dchelimsky dchelimsky committed Apr 17, 2011
@@ -1,7 +1,10 @@
-Feature: as_null_object method stub
+Feature: as_null_object
- Use the `as_null_object` method to ignore any messages that
- aren't explicitly set as stubs or message expectations.
+ Use the `as_null_object` method to ignore any messages that aren't explicitly
+ set as stubs or message expectations.
+
+ EXCEPTION: `to_ary` will raise a NoMethodError unless explicitly stubbed in
+ order to support `flatten` on an Array containing a double.
Scenario: double acting as_null_object
Given a file named "as_null_object_spec.rb" with:
@@ -22,6 +25,11 @@ Feature: as_null_object method stub
null_object.should_receive(:something)
null_object.something
end
+
+ it "supports Array#flatten" do
+ null_object = double('foo')
+ [null_object].flatten.should eq([null_object])
+ end
end
"""
When I run `rspec as_null_object_spec.rb`
@@ -0,0 +1,45 @@
+Feature: double handling to_ary
+
+ Ruby implicitly sends to_ary to any objects in an Array when the array
+ receives `flatten`:
+
+ [obj].flatten # => obj.to_ary
+
+ To support this, an RSpec double will raise a NoMethodError when it receives
+ `to_ary`, unless that method is explicitly stubbed.
+
+ Scenario: double receiving to_ary
+ Given a file named "example.rb" with:
+ """
+ describe "a double receiving to_ary" do
+ shared_examples "to_ary" do
+ it "returns nil" do
+ expect do
+ obj.to_ary.should be_nil
+ end.to raise_error(NoMethodError)
+ end
+
+ it "can be overridden with a stub" do
+ obj.stub(:to_ary) { :non_nil_value }
+ obj.to_ary.should be(:non_nil_value)
+ end
+
+ it "supports Array#flatten" do
+ obj = double('foo')
+ [obj].flatten.should eq([obj])
+ end
+ end
+
+ context "double as_null_object" do
+ let(:obj) { double('obj').as_null_object }
+ include_examples "to_ary"
+ end
+
+ context "double without as_null_object" do
+ let(:obj) { double('obj') }
+ include_examples "to_ary"
+ end
+ end
+ """
+ When I run `rspec example.rb`
+ Then the examples should all pass
View
@@ -41,10 +41,10 @@ def respond_to?(sym, incl_private=false)
private
def method_missing(sym, *args, &block)
+ raise NoMethodError if sym == :to_ary
__mock_proxy.record_message_received(sym, *args, &block)
begin
- return self if __mock_proxy.null_object?
- super
+ __mock_proxy.null_object? ? self : super
rescue NameError
__mock_proxy.raise_unexpected_message_error(sym, *args)
end
@@ -0,0 +1,31 @@
+require "spec_helper"
+
+describe "a double receiving to_ary" do
+ shared_examples "to_ary" do
+ it "returns nil" do
+ expect do
+ obj.to_ary.should be_nil
+ end.to raise_error(NoMethodError)
+ end
+
+ it "can be overridden with a stub" do
+ obj.stub(:to_ary) { :non_nil_value }
+ obj.to_ary.should be(:non_nil_value)
+ end
+
+ it "supports Array#flatten" do
+ obj = double('foo')
+ [obj].flatten.should eq([obj])
+ end
+ end
+
+ context "double as_null_object" do
+ let(:obj) { double('obj').as_null_object }
+ include_examples "to_ary"
+ end
+
+ context "double without as_null_object" do
+ let(:obj) { double('obj') }
+ include_examples "to_ary"
+ end
+end

0 comments on commit 1907cf9

Please sign in to comment.