title: RSpec allow/expect vs just expect/and_return
body: <p>In RSpec, specifically version >= 3, is there any difference between:</p>
  
  <ul>
  <li>Using <code>allow</code> to set up message expectations with parameters that return test doubles, and then using <code>expect</code> to make an assertion on the returned test doubles</li>
  <li>Just using <code>expect</code> to set up the expectation with parameters and return the test double</li>
  </ul>
  
  <p>or is it all just semantics?  I know that providing/specifying a return value with <code>expect</code> was <a href="https://www.relishapp.com/rspec/rspec-mocks/v/2-13/docs/message-expectations/expect-a-message#provide-a-return-value" rel="noreferrer">the syntax in RSpec mocks 2.13</a>, but as far as I can see, <a href="http://www.relishapp.com/rspec/rspec-mocks/v/3-1/docs/configuring-responses/returning-a-value#specify-a-return-value" rel="noreferrer">the syntax changed in RSpec mocks 3</a> to use <code>allow</code>. </p>
  
  <p>However, in the (passing) sample code below, using either <code>allow</code>/<code>expect</code> or just <code>expect</code>/<code>and_return</code> seems to generate the same result.  If one syntax was favoured over another, perhaps I would have expected there to be some kind of deprecation notice, but since there isn't, it would seem that both syntaxes are considered valid:</p>
  
  <pre class="lang-rb prettyprint-override"><code>class Foo
    def self.bar(baz)
      # not important what happens to baz parameter
      # only important that it is passed in
      new
    end
  
    def qux
      # perform some action
    end
  end
  
  class SomethingThatCallsFoo
    def some_long_process(baz)
      # do some processing
      Foo.bar(baz).qux
      # do other processing
    end
  end
  
  describe SomethingThatCallsFoo do
    let(:foo_caller) { SomethingThatCallsFoo.new }
  
    describe '#some_long_process' do
      let(:foobar_result) { double('foobar_result') }
      let(:baz) { double('baz') }
  
      context 'using allow/expect' do
        before do
          allow(Foo).to receive(:bar).with(baz).and_return(foobar_result)
        end
  
        it 'calls qux method on result of Foo.bar(baz)' do
          expect(foobar_result).to receive(:qux)
          foo_caller.some_long_process(baz)
        end
      end
  
      context 'using expect/and_return' do
        it 'calls qux method on result of Foo.bar(baz)' do
          expect(Foo).to receive(:bar).with(baz).and_return(foobar_result)
          expect(foobar_result).to receive(:qux)
          foo_caller.some_long_process(baz)
        end
      end
    end
  end
  </code></pre>
  
  <p>If I deliberately make the tests fail by changing the passed-in <code>baz</code> parameter in the expectation to a different test double, the errors are pretty much the same:</p>
  
  <pre class="lang-rb prettyprint-override"><code>  1) SomethingThatCallsFoo#some_long_process using allow/expect calls quux method on result of Foo.bar(baz)
       Failure/Error: Foo.bar(baz).qux
         &lt;Foo (class)&gt; received :bar with unexpected arguments
           expected: (#&lt;RSpec::Mocks::Double:0x3fe97a0127fc @name="baz"&gt;)
                got: (#&lt;RSpec::Mocks::Double:0x3fe97998540c @name=nil&gt;)
          Please stub a default value first if message might be received with other args as well.
       # ./foo_test.rb:16:in `some_long_process'
       # ./foo_test.rb:35:in `block (4 levels) in &lt;top (required)&gt;'
  
    2) SomethingThatCallsFoo#some_long_process using expect/and_return calls quux method on result of Foo.bar(baz)
       Failure/Error: Foo.bar(baz).qux
         &lt;Foo (class)&gt; received :bar with unexpected arguments
           expected: (#&lt;RSpec::Mocks::Double:0x3fe979935fd8 @name="baz"&gt;)
                got: (#&lt;RSpec::Mocks::Double:0x3fe979cc5c0c @name=nil&gt;)
       # ./foo_test.rb:16:in `some_long_process'
       # ./foo_test.rb:43:in `block (4 levels) in &lt;top (required)&gt;'
  </code></pre>
  
  <p>So, are there any real differences between these two tests, either in result or expressed intent, or is it just semantics and/or personal preference?  Should <code>allow</code>/<code>expect</code> be used over <code>expect</code>/<code>and_return</code> in general as it seems like it's the replacement syntax, or are each of them meant to be used in specific test scenarios?</p>
  
  <p><strong>Update</strong></p>
  
  <p>After reading <a href="https://stackoverflow.com/a/28007007/567863">Mori's answer</a>'s, I commented out the <code>Foo.bar(baz).qux</code> line from the example code above, and got the following errors:</p>
  
  <pre class="lang-rb prettyprint-override"><code>  1) SomethingThatCallsFoo#some_long_process using allow/expect calls qux method on result of Foo.bar(baz)
       Failure/Error: expect(foobar_result).to receive(:qux)
         (Double "foobar_result").qux(any args)
             expected: 1 time with any arguments
             received: 0 times with any arguments
       # ./foo_test.rb:34:in `block (4 levels) in &lt;top (required)&gt;'
  
    2) SomethingThatCallsFoo#some_long_process using expect/and_return calls qux method on result of Foo.bar(baz)
       Failure/Error: expect(Foo).to receive(:bar).with(baz).and_return(foobar_result)
         (&lt;Foo (class)&gt;).bar(#&lt;RSpec::Mocks::Double:0x3fc211944fa4 @name="baz"&gt;)
             expected: 1 time with arguments: (#&lt;RSpec::Mocks::Double:0x3fc211944fa4 @name="baz"&gt;)
             received: 0 times
       # ./foo_test.rb:41:in `block (4 levels) in &lt;top (required)&gt;'
  </code></pre>
  
  <ul>
  <li>The <code>allow</code> spec fails because the <code>foobar_result</code> double never gets to stand in for the result of <code>Foo.bar(baz)</code>, and hence never has <code>#qux</code> called on it</li>
  <li>The <code>expect</code> spec fails at the point of <code>Foo</code> never receiving <code>.bar(baz)</code> so we don't even get to the point of interrogating the <code>foobar_result</code> double</li>
  </ul>
  
  <p>Makes sense: it's not just a syntax change, and that <code>expect</code>/<code>and_return</code> does have a purpose different to <code>allow</code>/<code>expect</code>.  I really should have checked the most obvious place: the <a href="https://github.com/rspec/rspec-mocks/blob/master/README.md" rel="noreferrer">RSpec Mocks README</a>, specifically the following sections:</p>
  
  <ul>
  <li><a href="https://github.com/rspec/rspec-mocks/blob/master/README.md#mock-objects-and-test-stubs" rel="noreferrer">Mock Objects and Test Stubs</a></li>
  <li><a href="https://github.com/rspec/rspec-mocks/blob/master/README.md#test-specific-extension" rel="noreferrer">Test-Specific Extension</a></li>
  <li><a href="https://github.com/rspec/rspec-mocks/blob/master/README.md#setting-responses" rel="noreferrer">Setting Responses</a></li>
  </ul>