Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pat/active-matchers
Helpful rspec matchers for testing validations and associations.
https://github.com/pat/active-matchers
Last synced: 17 days ago
JSON representation
Helpful rspec matchers for testing validations and associations.
- Host: GitHub
- URL: https://github.com/pat/active-matchers
- Owner: pat
- License: mit
- Created: 2008-04-21T09:37:49.000Z (over 16 years ago)
- Default Branch: master
- Last Pushed: 2015-11-15T12:54:23.000Z (about 9 years ago)
- Last Synced: 2024-11-29T23:42:49.194Z (24 days ago)
- Language: Ruby
- Homepage: http://am.freelancing-gods.com
- Size: 123 KB
- Stars: 23
- Watchers: 4
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.textile
- License: LICENSE
Awesome Lists containing this project
README
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.
h2. Install
script/plugin install http://github.com/pat/active-matchers.git
Make sure, in your spec_helper.rb file, you add an include as follows:
config.include ActiveMatchers::Matchersh2. 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_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)
endAlso 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@)h2. Authors
Copyright (c) 2007 Pat Allan & James Healy, released under the MIT license
h2. Contributors
* Chris Lloyd
* Gareth Townsend
* Wes Oldenbeuving