Skip to content

Commit

Permalink
improve explanation as to why we raise NoMethodError on to_ary
Browse files Browse the repository at this point in the history
  • Loading branch information
dchelimsky committed Sep 29, 2011
1 parent 8d35db6 commit b0ea68d
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions features/method_stubs/to_ary.feature
Original file line number Original file line Diff line number Diff line change
@@ -1,22 +1,24 @@
Feature: double handling to_ary Feature: double handling to_ary


Ruby implicitly sends to_ary to any objects in an Array when the array Ruby implicitly sends `to_ary` to all of the objects in an `Array` when the
receives `flatten`: array receives `flatten`:


[obj].flatten # => obj.to_ary [obj].flatten # => obj.to_ary


If `to_ary` raises a `NoMethodError`, Ruby sees that the object can not be coerced
into an array and moves on to the next object.

To support this, an RSpec double will raise a NoMethodError when it receives To support this, an RSpec double will raise a NoMethodError when it receives
`to_ary`, unless that method is explicitly stubbed. `to_ary` _even if it is set `as_null_object`_, unless `to_ary` is explicitly
stubbed.


Scenario: double receiving to_ary Scenario: double receiving to_ary
Given a file named "example.rb" with: Given a file named "example.rb" with:
""" """
describe "a double receiving to_ary" do describe "#to_ary" do
shared_examples "to_ary" do shared_examples "to_ary" do
it "returns nil" do it "raises a NoMethodError" do
expect do expect { obj.to_ary }.to raise_error(NoMethodError)
obj.to_ary.should be_nil
end.to raise_error(NoMethodError)
end end
it "can be overridden with a stub" do it "can be overridden with a stub" do
Expand All @@ -30,12 +32,12 @@ Feature: double handling to_ary
end end
end end
context "double as_null_object" do context "sent to a double as_null_object" do
let(:obj) { double('obj').as_null_object } let(:obj) { double('obj').as_null_object }
include_examples "to_ary" include_examples "to_ary"
end end
context "double without as_null_object" do context "sent to a double without as_null_object" do
let(:obj) { double('obj') } let(:obj) { double('obj') }
include_examples "to_ary" include_examples "to_ary"
end end
Expand Down

0 comments on commit b0ea68d

Please sign in to comment.