{"action":"created","issue":{"url":"https://api.github.com/repos/rspec/rspec-mocks/issues/997","labels_url":"https://api.github.com/repos/rspec/rspec-mocks/issues/997/labels{/name}","comments_url":"https://api.github.com/repos/rspec/rspec-mocks/issues/997/comments","events_url":"https://api.github.com/repos/rspec/rspec-mocks/issues/997/events","html_url":"https://github.com/rspec/rspec-mocks/issues/997","id":97814787,"number":997,"title":"allow().to receive doesn't respect order","user":{"login":"rwz","id":129043,"avatar_url":"https://avatars.githubusercontent.com/u/129043?v=3","gravatar_id":"","url":"https://api.github.com/users/rwz","html_url":"https://github.com/rwz","followers_url":"https://api.github.com/users/rwz/followers","following_url":"https://api.github.com/users/rwz/following{/other_user}","gists_url":"https://api.github.com/users/rwz/gists{/gist_id}","starred_url":"https://api.github.com/users/rwz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rwz/subscriptions","organizations_url":"https://api.github.com/users/rwz/orgs","repos_url":"https://api.github.com/users/rwz/repos","events_url":"https://api.github.com/users/rwz/events{/privacy}","received_events_url":"https://api.github.com/users/rwz/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2015-07-28T22:25:38Z","updated_at":"2015-07-28T22:41:31Z","closed_at":null,"body":"```ruby\r\nrequire \"rspec/autorun\"\r\n\r\nclass MyClass\r\n  def initialize(client)\r\n    @client = client\r\n  end\r\n\r\n  def fetch_results\r\n    [].tap do |results|\r\n      results << @client.get_value while results.length < 3\r\n    end\r\n  end\r\nend\r\n\r\ndescribe MyClass do\r\n  let(:client){ double(\"Client\") }\r\n  subject{ described_class.new(client) }\r\n\r\n  it \"works\" do\r\n    expect(client).to receive(:get_value).and_return(1).ordered\r\n    expect(client).to receive(:get_value).and_return(2).ordered\r\n    expect(client).to receive(:get_value).and_return(3).ordered\r\n\r\n    expect(subject.fetch_results).to eq([1,2,3])\r\n  end\r\n\r\n  it \"should work, but fails\" do\r\n    allow(client).to receive(:get_value).and_return(1).ordered\r\n    allow(client).to receive(:get_value).and_return(2).ordered\r\n    allow(client).to receive(:get_value).and_return(3).ordered\r\n\r\n    expect(subject.fetch_results).to eq([1,2,3])\r\n  end\r\nend\r\n```\r\n\r\nThis is expected to pass in both cases but only actually passes with `expect` syntax.\r\n\r\n```\r\nFailures:\r\n\r\n  1) MyClass should work, but fails\r\n     Failure/Error: expect(subject.fetch_results).to eq([1,2,3])\r\n\r\n       expected: [1, 2, 3]\r\n            got: [3, 3, 3]\r\n\r\n       (compared using ==)\r\n     # run.rb:32:in `block (2 levels) in <main>'\r\n\r\nFinished in 0.01525 seconds (files took 0.09345 seconds to load)\r\n2 examples, 1 failure\r\n\r\nFailed examples:\r\n\r\nrspec 'run.rb[1:2]' # MyClass should work, but fails\r\n```"},"comment":{"url":"https://api.github.com/repos/rspec/rspec-mocks/issues/comments/125773091","html_url":"https://github.com/rspec/rspec-mocks/issues/997#issuecomment-125773091","issue_url":"https://api.github.com/repos/rspec/rspec-mocks/issues/997","id":125773091,"user":{"login":"myronmarston","id":49391,"avatar_url":"https://avatars.githubusercontent.com/u/49391?v=3","gravatar_id":"","url":"https://api.github.com/users/myronmarston","html_url":"https://github.com/myronmarston","followers_url":"https://api.github.com/users/myronmarston/followers","following_url":"https://api.github.com/users/myronmarston/following{/other_user}","gists_url":"https://api.github.com/users/myronmarston/gists{/gist_id}","starred_url":"https://api.github.com/users/myronmarston/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/myronmarston/subscriptions","organizations_url":"https://api.github.com/users/myronmarston/orgs","repos_url":"https://api.github.com/users/myronmarston/repos","events_url":"https://api.github.com/users/myronmarston/events{/privacy}","received_events_url":"https://api.github.com/users/myronmarston/received_events","type":"User","site_admin":false},"created_at":"2015-07-28T22:41:31Z","updated_at":"2015-07-28T22:41:31Z","body":"If you want to return different sequential values for multiple calls, just pass multiple values to `and_return`:\r\n\r\nhttps://relishapp.com/rspec/rspec-mocks/v/3-3/docs/configuring-responses/returning-a-value#specify-different-return-values-for-multiple-calls\r\n\r\nThe problem in your example is that stubs _replace_ earlier stubs rather than specifying responses for later calls.  This is intended to support cases like:\r\n\r\n``` ruby\r\nRSpec.describe do\r\n  before do\r\n    allow(Collaborator).to receive(:foo).and_return(:generic_response)\r\n  end\r\n\r\n  it \"does x\" do\r\n    # ...\r\n  end\r\n\r\n  it \"does y\" do\r\n    # ...\r\n  end\r\n\r\n  context \"with a more specific response\" do\r\n    before do\r\n      allow(Collaborator).to receive(:foo).and_return(:specific response)\r\n    end\r\n\r\n    it \"does z\" do\r\n      # ...\r\n    end\r\n  end\r\nend\r\n```\r\n\r\nHere the second `allow` overrides what was set as the response in the first `before` hook.  We can't make `allow` additive like you thought it was and support that.\r\n\r\nMessage expectations (e.g. via `expect`) are additive because each one specifies something that _must_ be satisfied for the example to pass, so we have to keep each one.\r\n\r\nDo you have any suggestions for how we can improve our docs to make this more clear?"}}