GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Fork of freelancing-god/active-matchers
Description: Helpful rspec matchers for testing validations and associations.
Homepage: http://am.freelancing-gods.com
Clone URL: git://github.com/Narnach/active-matchers.git
commit  b1334a9fc0693104146bca791aac833970a10e6a
tree    b5a60a7286d4ebe85c1c160c6004a445d00b51f6
parent  296bc386c9f27173a565c569057fed0d7a543c21
name age message
file .gitignore Tue Jul 01 22:02:33 -0700 2008 Converted to a proper gem. [chrislloyd]
file LICENSE Tue Jul 01 22:02:33 -0700 2008 Converted to a proper gem. [chrislloyd]
file README.textile Wed Jul 23 04:43:43 -0700 2008 Another documentation fix: we use git:// instea... [Narnach]
file Rakefile Tue Jul 01 22:42:50 -0700 2008 Whoops, lets fix up that spacing and make sure ... [chrislloyd]
file active-matchers.gemspec Tue Jul 01 22:19:35 -0700 2008 0.3.0! [chrislloyd]
file init.rb Thu Jul 03 23:08:44 -0700 2008 documentation of @validates_numericalicity_of w... [quamen]
directory lib/ Mon Jul 21 05:16:06 -0700 2008 Slightly refactored/reordered allow_nil option ... [FiXato]
README.textile

Some helpful rspec matchers for testing validations and associations. It is not complete, especially in the case of validations, and lacking in documentation, but might be useful nonetheless.

Install

Freelancing-god’s original version:

script/plugin install git://github.com/freelancing-god/active-matchers.git

Narnach’s version:

script/plugin install git://github.com/Narnach/active-matchers.git

Make sure, in your spec_helper.rb file, you add an include as follows:

config.include ActiveMatchers::Matchers

Usage

Test validates_presence_of :name

Model.should need(:name).using(@valid_attributes)

Test validates_uniqueness_of :name

Model.should need(:name).to_be_unique.using(@valid_attributes)

Test presence of at least one field being required

Model.should need.one_of(:first_name, :last_name).using(@valid_attributes)

Test validates_inclusion_of :age, :in => 0..99

Model.should need(:age).to_be_in_range(0..99).using(@valid_attributes)

Test validates_length_of :name matches database field length

Model.should limit_length_of(:name).using(@valid_attributes)

Test validates_length_of :name, :maximum => 255

Model.should limit_length_of(:name).to(255).using(@valid_attributes)

Test validates_length_of :name, :minimum => 3

Model.should limit_length_of(:name).from(3).using(@valid_attributes)

Test validates_length_of :name, :within => 3..40

Model.should limit_length_of(:name).from(3).to(40).using(@valid_attributes)

Test validates_numericality_of :age

Model.should need(:age).to_be_numeric.using(@valid_attributes)

You can group multiple validation checks together like so:

using(@valid_attributes) do
  Model.should need(:name)
  Model.should limit_length_of(:name).to(255)
end

Also allows confirmation of the presence of associations

Test belongs_to :parent

Model.should belong_to(:parent)

Test belongs_to :parent, :class_name => "CustomClass", :foreign_key => "some_id"

Model.should belong_to(:parent).with_options(
  :class_name => "CustomClass", :foreign_key => "some_id")

Test has_many :items

Model.should have_many(:items)

Test has_many :items, :class_name => "CustomClass", :foreign_key => "some_id"

Model.should have_many(:items).with_options(
  :class_name => "CustomClass", :foreign_key => "some_id")

Similar testing available for has_one (Model.should have_one) and has_and_belongs_to_many (Model.should have_and_belong_to_many)

When you have the validates_existence plugin installed, or when you manually check the existence of a related model, the following will be useful:

Test validates_existence_of :parent

Model.should validate_existence_of(:parent).using(@valid_attributes)

Test validates_existence_of :parent, :allow_nil => true

Model.should validate_existence_of(:parent).with_options(:allow_nil=>true).using(@valid_attributes)

If 0 (zero) as value is allowed too, then you can add the :allow_zero=>true option:

Model.should validate_existence_of(:parent).with_options(:allow_zero=>true).using(@valid_attributes)

Authors

Copyright© 2007 Pat Allan & James Healy, released under the MIT license