title: Rails 3.1, RSpec: testing model validations
body: <p>I have started my journey with TDD in Rails and have run into a small issue regarding tests for model validations that I can't seem to find a solution to. Let's say I have a User model,</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    validates :username, :presence =&gt; true
  end
  </code></pre>
  
  <p>and a simple test</p>
  
  <pre><code>it "should require a username" do
    User.new(:username =&gt; "").should_not be_valid
  end
  </code></pre>
  
  <p>This correctly tests the presence validation, but what if I want to be more specific? For example, testing full_messages on the errors object..</p>
  
  <pre><code>it "should require a username" do
    user = User.create(:username =&gt; "")
    user.errors[:username].should ~= /can't be blank/
  end
  </code></pre>
  
  <p>My concern about the initial attempt (using should_not be_valid) is that RSpec won't produce a descriptive error message. It simply says "expected valid? to return false, got true." However, the second test example has a minor drawback: it uses the create method instead of the new method in order to get at the errors object.</p>
  
  <p>I would like my tests to be more specific about what they're testing, but at the same time not have to touch a database.</p>
  
  <p>Anyone have any input?</p>