Skip to content
Adds several handy expectations for testing ActiveRecord model validations.
Find file
Pull request Compare This branch is 7 commits ahead, 16 commits behind pelargir:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


= rspec_validation_expectations

Adds several handy expectations for testing ActiveRecord model validations.

== Dependencies

Requires the validation_reflection plugin:

  script/plugin install git://

== Installation

Install the gem directly:

  gem sources -a (you only have to do this once)
  sudo gem install pelargir-rspec_validation_expectations
Or install the gem in your Rails project:
  gem sources -a
  script/plugin install pelargir-rspec_validation_expectations

Or clone the project:

  git clone git://

== Usage

Ensure that validations and assocations are properly defined in your models:

  describe User do
    it_should_validate_presence_of     :first_name, :last_name, :email
    it_should_validate_numericality_of :zip
    it_should_validate_uniqueness_of   :email
    it_should_validate_inclusion_of    :gender,   :in => %w(Male Female)
    it_should_validate_exclusion_of    :login,    :in => %w(admin root)
    it_should_validate_length_of       :password, :within => 4..40
    it_should_validate_length_of       :login,    :minimum => 8
    it_should_validate_length_of       :email,    :maximum => 40
    it_should_validate_length_of       :pin,      :is => 4
    it_should_validate_confirmation_of :password, :email
    it_should_validate_format_of       :email,    :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
    it_should_validate_acceptance_of   :terms_of_service, :eula_agreement
    # tests that User.count increases by 1
    it_should_be_createable :with => {:first_name => 'Ed', :last_name => 'The Duck', :email => ''}
    it_should_belong_to :employer
    it_should_have_many :friends, :romans, :countrymen
    it_should_have_one  :account
    it_should_have_and_belong_to_many :comments
Which gives you:

ruby script/spec --format s spec/models/user_spec.rb:

- should validate presence of first name
- should validate presence of last name
- should validate presence of email
- should validate numericality of zip
- should validate uniqueness of email
- should validate inclusion of gender as one of Male or Female
- should be creatable
- should belong to employer
- should have many friends
- should have many romans
- should have many countrymen
- should have one account
- should have and belong to many comments

== Resources

Author: Matthew Bass
Something went wrong with that request. Please try again.