This file is indexed.

/usr/share/doc/ruby-rspec-mocks/features/old_syntax/any_instance.feature is in ruby-rspec-mocks 3.4.0c3e0m1s1-1ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
@allow-old-syntax
Feature: `any_instance`

  `any_instance` is the old way to stub or mock any instance of a class but carries the baggage of a global monkey patch on all classes.
  Note that we [generally recommend against](../working-with-legacy-code/any-instance) using this feature.

  Background:
    Given a file named "spec/spec_helper.rb" with:
      """ruby
      RSpec.configure do |config|
        config.mock_with :rspec do |mocks|
          mocks.syntax = :should
        end
      end
      """
    And a file named ".rspec" with:
      """
      --require spec_helper
      """

  Scenario: Stub a method on any instance of a class
    Given a file named "spec/example_spec.rb" with:
      """ruby
      RSpec.describe "Stubbing a method with any_instance" do
        it "returns the specified value on any instance of the class" do
          Object.any_instance.stub(:foo).and_return(:return_value)

          o = Object.new
          expect(o.foo).to eq(:return_value)
        end
      end
      """
    When I run `rspec spec/example_spec.rb`
    Then the examples should all pass

  Scenario: Stub multiple methods on any instance of a class
    Given a file named "spec/example_spec.rb" with:
      """ruby
      RSpec.describe "Stubbing multiple methods with any_instance" do
        it "returns the specified values for the givne messages" do
          Object.any_instance.stub(:foo => 'foo', :bar => 'bar')

          o = Object.new
          expect(o.foo).to eq('foo')
          expect(o.bar).to eq('bar')
        end
      end
      """
    When I run `rspec spec/example_spec.rb`
    Then the examples should all pass

  Scenario: Stubbing any instance of a class with specific arguments
    Given a file named "spec/example_spec.rb" with:
      """ruby
      RSpec.describe "Stubbing any instance with arguments" do
        it "returns the stubbed value when arguments match" do
          Object.any_instance.stub(:foo).with(:param_one, :param_two).and_return(:result_one)
          Object.any_instance.stub(:foo).with(:param_three, :param_four).and_return(:result_two)

          o = Object.new
          expect(o.foo(:param_one, :param_two)).to eq(:result_one)
          expect(o.foo(:param_three, :param_four)).to eq(:result_two)
        end
      end
      """
    When I run `rspec spec/example_spec.rb`
    Then the examples should all pass

  Scenario: Block implementation is passed the receiver as first arg
    Given a file named "spec/example_spec.rb" with:
      """ruby
      RSpec.describe "Stubbing any instance of a class" do
        it 'yields the receiver to the block implementation' do
          String.any_instance.stub(:slice) do |value, start, length|
            value[start, length]
          end

          expect('string'.slice(2, 3)).to eq('rin')
        end
      end
      """
    When I run `rspec spec/example_spec.rb`
    Then the examples should all pass

  Scenario: Expect a message on any instance of a class
    Given a file named "spec/example_spec.rb" with:
      """ruby
      RSpec.describe "Expecting a message on any instance of a class" do
        before do
          Object.any_instance.should_receive(:foo)
        end

        it "passes when an instance receives the message" do
          Object.new.foo
        end

        it "fails when no instance receives the message" do
          Object.new.to_s
        end
      end
      """
    When I run `rspec spec/example_spec.rb`
    Then it should fail with the following output:
      | 2 examples, 1 failure |
      | Exactly one instance should have received the following message(s) but didn't: foo |