title: Factory Girl failing Rspec validation tests
body: <p>I've been trying to get a grasp on writing tests, but having a lot of trouble as the tests never seem to validate the way I want them to. In particular, I've been trying to use Factory Girl as opposed to fixtures - as suggested by a recent Railscasts and other advice I've seen on the net - due to the benefits it professes, and that hasn't worked out.</p>
  
  <p>For example, here is a simple Rspec test for a user model, testing to make sure a username is present...</p>
  
  <pre><code>describe User do
    it "should not be valid without a username" do
      user = FactoryGirl.create(:user, :username =&gt; "", :password =&gt; "secret")
      user.should_not be_valid
    end
  end
  </code></pre>
  
  <p>And my factories.rb file, if it helps...</p>
  
  <pre><code>FactoryGirl.define do
    factory :user do
      sequence(:username) { |n| "registered-#{n}" }
      password "foobar"
    end
  end
  </code></pre>
  
  <p>When I run 'rake spec,' it tells me...</p>
  
  <pre><code>1) User should not be valid without a username
       Failure/Error: user = FactoryGirl.create(:user, :username =&gt; "", :password =&gt; "secret")
       ActiveRecord::RecordInvalid:
         Validation failed: Username can't be blank
  </code></pre>
  
  <p>Ummm...that's the POINT. If I specified that the user should NOT be valid, shouldn't this test actually pass?</p>
  
  <p>If I replace the Factory Girl line and set the user in the test with something like <code>'user = User.new(:username =&gt; "", :password =&gt; "secret")'</code>, to no surprise the test passes fine. So why is Factory Girl not working right?</p>