Browse files

improve explanation as to why we raise NoMethodError on to_ary

  • Loading branch information...
1 parent 8d35db6 commit b0ea68df0b89f2ddb022e478eac5d3651f5170a3 @dchelimsky dchelimsky committed Sep 29, 2011
Showing with 12 additions and 10 deletions.
  1. +12 −10 features/method_stubs/to_ary.feature
@@ -1,22 +1,24 @@
Feature: double handling to_ary
- Ruby implicitly sends to_ary to any objects in an Array when the array
- receives `flatten`:
+ Ruby implicitly sends `to_ary` to all of the objects in an `Array` when the
+ array receives `flatten`:
[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_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
Given a file named "example.rb" with:
- describe "a double receiving to_ary" do
+ describe "#to_ary" do
shared_examples "to_ary" do
- it "returns nil" do
- expect do
- obj.to_ary.should be_nil
- raise_error(NoMethodError)
+ it "raises a NoMethodError" do
+ expect { obj.to_ary }.to raise_error(NoMethodError)
it "can be overridden with a stub" do
@@ -30,12 +32,12 @@ Feature: double handling to_ary
- context "double as_null_object" do
+ context "sent to a double as_null_object" do
let(:obj) { double('obj').as_null_object }
include_examples "to_ary"
- context "double without as_null_object" do
+ context "sent to a double without as_null_object" do
let(:obj) { double('obj') }
include_examples "to_ary"

0 comments on commit b0ea68d

Please sign in to comment.