Already got something like that. The main difference, is that I'm using
factories.
The problem with my test, which almost follow those examples, is that I'm
repeating myself each time I want to validate the same (uniqueness, length,
etc).
One option is to create methods for each type of validation and that way the
test will be DRY. Instead of that I was looking if there is a gem that
provide me with those (keep them updated, etc).
At that point is when I found shoulda-matchers.

Otherwise this is an example of how the test looks

class User < ActiveRecord::Base
...
  validates :first_name,
    :presence => true,
    :length => { :maximum => 32 }
  validates :last_name,
    :presence => true,
    :length => { :maximum => 32 }
...
end

According to the examples your tests for those fields should been:

describe User do

  before(:each) do
    @attr = { :first_name => "Foo", :last_name => "Bar" }
  end
...
  it "should require a first name" do
    no_email_user = User.new(@attr.merge(:first_name => ""))
    no_email_user.should_not be_valid
  end

  it "should require a last name" do
    no_email_user = User.new(@attr.merge(:last_name => ""))
    no_email_user.should_not be_valid
  end

  it "should reject first names that are too long" do
    long_first_name = "a" * 33
    long_first_name_user = User.new(@attr.merge(:first_name =>
long_first_name))
    long_first_name_user.should_not be_valid
  end

  it "should reject last names that are too long" do
    long_last_name = "a" * 33
    long_last_name_user = User.new(@attr.merge(:last_name =>
long_last_name))
    long_last_name_user.should_not be_valid
  end
end

On Tue, Jul 26, 2011 at 9:55 PM, 7stud -- <[email protected]> wrote:

> You can start reading at section 6.2, and see how the Rails 3 Tutorial
> tests validations:
>
>
> http://ruby.railstutorial.org/chapters/modeling-and-viewing-users-one#sec:user_model
>
> --
> Posted via http://www.ruby-forum.com/.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Talk" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en.

Reply via email to