Skip to content
This repository
branch: fix_confusing_…
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 48 lines (38 sloc) 1.451 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
Feature: double handling to_ary

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` _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 "#to_ary" do
shared_examples "to_ary" do
it "raises a NoMethodError" do
expect { obj.to_ary }.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 "sent to a double as_null_object" do
let(:obj) { double('obj').as_null_object }
include_examples "to_ary"
end

context "sent to a 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
Something went wrong with that request. Please try again.