Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are 2 differences but the result is exactly the same. Both are in regards to the rspec mocks/expectations syntax.</p> <ol> <li><p>Use of #allow instead of #stub method. First case uses the new rspec syntax introduced this year. <strong>This is now the preferred way of using rspec.</strong> Altough the old syntax isn't deprecated, it will probably be disabled by default in rspec3. More info on this topic from the maintainer of rspec: <a href="http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax">http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax</a> <a href="http://myronmars.to/n/dev-blog/2013/07/the-plan-for-rspec-3">http://myronmars.to/n/dev-blog/2013/07/the-plan-for-rspec-3</a></p></li> <li><p>Use of block instead of #and_return to define the returning value. This has nothing to do with the mentioned syntax change; both approaches have been available for quite a while (since rspec 1). It is more appropriate to use the #and_return method, since it is (a) the default way, (b) more readable and (c) comes without any runtime overhead. The second approach using block is usually reserved to the corner cases, when you wish to return something of more dynamic nature - not a constant, but some kind of calculation.</p></li> </ol> <p>The answer to your question would be to use combination of both:</p> <ul> <li>use the #allow instead of #stub </li> <li>use #and_return instead of block, unless you need to return dynamically calculated value</li> </ul> <p>E.g.:</p> <pre><code>book = double('book') allow(book).to receive(:title).and_return('The RSpec Book') </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload