<commit_before>Feature: Any Instance

  rspec-mocks provides two methods, `allow_any_instance_of` and
  `expect_any_instance_of`, that will allow you to stub or mock any instance of a class. They
  are used in place of [`allow`](../basics/allowing-messages) or [`expect`](../basics/expecting-messages):

  ```ruby
  allow_any_instance_of(Widget).to receive(:name).and_return("Wibble")
  expect_any_instance_of(Widget).to receive(:name).and_return("Wobble")
  ```

  These methods add the appropriate stub or expectation to all instances of `Widget`.

  This feature is sometimes useful when working with legacy code, though in general we
  discourage its use for a number of reasons:

  * The `rspec-mocks` API is designed for individual object instances, but this feature
    operates on entire classes of objects. As a result there are some semantically confusing
    edge cases. For example, in `expect_any_instance_of(Widget).to
    receive(:name).twice` it isn't clear whether each specific instance is expected to
    receive `name` twice, or if two receives total are expected. (It's the former.)
  * Using this feature is often a design smell. It may be that your test is trying to do too
    much or that the object under test is too complex.
  * It is the most complicated feature of `rspec-mocks`, and has historically received the
    most bug reports. (None of the core team actively use it, which doesn't help.)

  Scenario: Use `allow_any_instance_of` to stub a method
    Given a file named "example_spec.rb" with:
      """ruby
      RSpec.describe "allow_any_instance_of" do
        it "returns the specified value on any instance of the class" do
          allow_any_instance_of(Object).to receive(:foo).and_return(:return_value)

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

  Scenario: Use `allow_any_instance_of` to stub multiple methods
    Given a file named "example_spec.rb" with:<commit_msg>correct documentation to highlight the expected behaviour [skip ci]<commit_after>Feature: Any Instance

  rspec-mocks provides two methods, `allow_any_instance_of` and
  `expect_any_instance_of`, that will allow you to stub or mock any instance of a class. They
  are used in place of [`allow`](../basics/allowing-messages) or [`expect`](../basics/expecting-messages):

  ```ruby
  allow_any_instance_of(Widget).to receive(:name).and_return("Wibble")
  expect_any_instance_of(Widget).to receive(:name).and_return("Wobble")
  ```

  These methods add the appropriate stub or expectation to all instances of `Widget`.

  This feature is sometimes useful when working with legacy code, though in general we
  discourage its use for a number of reasons:

  * The `rspec-mocks` API is designed for individual object instances, but this feature
    operates on entire classes of objects. As a result there are some semantically confusing
    edge cases. For example, in `expect_any_instance_of(Widget).to
    receive(:name).twice` it isn't clear whether a specific instance is expected to
    receive `name` twice, or if two receives total are expected. (It's the former.)
  * Using this feature is often a design smell. It may be that your test is trying to do too
    much or that the object under test is too complex.
  * It is the most complicated feature of `rspec-mocks`, and has historically received the
    most bug reports. (None of the core team actively use it, which doesn't help.)

  Scenario: Use `allow_any_instance_of` to stub a method
    Given a file named "example_spec.rb" with:
      """ruby
      RSpec.describe "allow_any_instance_of" do
        it "returns the specified value on any instance of the class" do
          allow_any_instance_of(Object).to receive(:foo).and_return(:return_value)

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

  Scenario: Use `allow_any_instance_of` to stub multiple methods
    Given a file named "example_spec.rb" with:
{'fraction_non_alphanumeric': 0.07505412557132547, 'fraction_numerical': 0.0, 'mean_word_length': 3.719636776390465, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5896456', 'n_tokens_mistral': 1162, 'n_tokens_neox': 1133, 'n_words': 497}