Note that there are some explanatory texts on larger screens.

plurals
  1. POFactoryGirl.create not increasing `.count` field
    primarykey
    data
    text
    <p>I had the same problem as Nick Rutten in this thread <a href="https://stackoverflow.com/questions/13606603/rails-tutorial-rspec-test-decoupling">Rails Tutorial: RSpec test decoupling</a>. I suspected the problem was the <code>FactoryGirl.create()</code> saving to the database, and thanks to @prusswan I fixed it. However I do not understand why these <code>puts</code> don't indicate different <code>User.count</code>.</p> <p>This is the snippet I'm interested in:</p> <pre><code>describe "with valid information" do puts "Number of users pre-Factory:" + User.count.to_s let(:user){FactoryGirl.create(:user)} puts "Number of users post-Factory:" + User.count.to_s (...) end </code></pre> <p>I get:</p> <p><code>Number of users pre-Factory:0</code><br> <code>Number of users post-Factory:0</code></p> <p>But shouldn't I get instead?: </p> <p><code>Number of users pre-Factory:0</code><br> <code>Number of users post-Factory:1</code></p> <p>Btw, the factory is defined (although not so relevant now) as:</p> <pre><code>FactoryGirl.define do factory :user do name "Example User" email "user@example.com" password "foobar" password_confirmation "foobar" end end </code></pre> <p>I'm stuck trying to understand why the counter is not increasing, wasn't the problem that it did increase to start with? thank you</p> <p><strong>Edit using @derekyau's suggestions:</strong></p> <p>Right now I've got in this order:</p> <pre><code>it "print counter before let" puts "Number of users:" + User.count.to_s end let(:user){FactoryGirl.create(:user)} it "print counter after let" puts "Number of users:" + User.count.to_s end </code></pre> <p>However now I get both values 1:</p> <pre><code>Number of users:1 Number of users:1 </code></pre> <p>Close but not there yet!</p> <p><strong>Edit 2</strong></p> <p>As explained by @Peter Alfvin here: <a href="https://stackoverflow.com/questions/18037920/within-a-given-rspec-describe-block-does-the-order-of-let-before-and-it-statem">Within a given RSpec describe block, does the order of let, before and it statements matter?</a> . The order of <code>let</code>, <code>before</code>, <code>it</code> is pretty much established, regardless of the code. That, together with @derekyau's explanation closes the question.</p>
    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.
 

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