Sha256: 400f0c4c49839c28792af367265d98e300cd43bf1d52e531343cdaf6f4ecf860

Contents?: true

Size: 1.51 KB

Versions: 35

Compression:

Stored size: 1.51 KB

Contents

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:
      """ruby
      describe "#to_ary" do
        shared_examples "to_ary" do
          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"

          it "returns nil" do
            expect( obj.to_ary ).to eq nil
          end
        end

        context "sent to a double without as_null_object" do
          let(:obj) { double('obj') }
          include_examples "to_ary"

          it "raises a NoMethodError" do
            expect { obj.to_ary }.to raise_error(NoMethodError)
          end
        end
      end
     """
    When I run `rspec example.rb`
    Then the examples should all pass

Version data entries

35 entries across 35 versions & 8 rubygems

Version Path
mango-0.7.1 vendor/bundler/ruby/2.0.0/gems/rspec-mocks-2.14.4/features/method_stubs/to_ary.feature
dxruby_rp5-0.0.3 spec/vendor/rspec-mocks-2.14.4/features/method_stubs/to_ary.feature
tuktuk-rails-0.0.10 vendor/bundle/ruby/2.0.0/gems/rspec-mocks-2.14.4/features/method_stubs/to_ary.feature
tuktuk-rails-0.0.9 vendor/bundle/ruby/2.0.0/gems/rspec-mocks-2.14.4/features/method_stubs/to_ary.feature
tuktuk-rails-0.0.8 vendor/bundle/ruby/2.0.0/gems/rspec-mocks-2.14.4/features/method_stubs/to_ary.feature
tuktuk-rails-0.0.7 vendor/bundle/ruby/2.0.0/gems/rspec-mocks-2.14.4/features/method_stubs/to_ary.feature
dxruby_rp5-0.0.2 spec/vendor/rspec-mocks-2.14.4/features/method_stubs/to_ary.feature
dxruby_rp5-0.0.1 spec/vendor/rspec-mocks-2.14.4/features/method_stubs/to_ary.feature
mango-0.7.0 vendor/bundler/ruby/2.0.0/gems/rspec-mocks-2.14.4/features/method_stubs/to_ary.feature
rspec-mocks-2.99.0.beta1 features/method_stubs/to_ary.feature
rspec-mocks-2.14.4 features/method_stubs/to_ary.feature
clickable_link-0.0.2 vendor/bundle/ruby/1.9.1/gems/rspec-mocks-2.14.3/features/method_stubs/to_ary.feature
syllable_counter-1.0.0 vendor/bundle/gems/rspec-mocks-2.14.3/features/method_stubs/to_ary.feature
rspec-mocks-2.14.3 features/method_stubs/to_ary.feature
rspec-mocks-2.14.2 features/method_stubs/to_ary.feature