SpicyCode

The ramblings of Chad Humphries

Rspec Expectation Matchers – Part II

The new release of my RSpec extensions gem (along with a newly minted plugin version) will be out within a day or two. Barring any major show-stoppers I just need to finish adding :message => specification support so you can indicate what error message you expect to receive if it is invalid (if you choose to). Once that is in I’ll let the release notifications fly.

The goal of these expectation matchers is to allow you to test validations, and associations easily. Whether or not you wish to test these items I will leave up to you, but if you choose to this will definitely help keep things simple and quick.

Poor mans code outline

validate_presence_of(attr, options = { })
#    ex:  model.should validate_presence_of(:name)
#           model.should validate_presence_of(:name, :valid_value => 'Bob', :invalid_value => nil)
validate_confirmation_of(attr, options = { })

validate_uniqueness_of(attr, options = { })

validate_acceptance_of(attr, options = { })

validate_associated(associated_model, options = { })

validate_exclusion_of(attr, options = { })

validate_format_of(attr, options = { })

validate_inclusion_of(attr, options = { })

validate_length_of(attr, options = { })
alias_method :validate_size_of, :validate_length_of
#    ex:  model.should validate_length_of(:name, :within => 3..30)
#          model.should validate_length_of(:name, :maximum => 100)

validate_numericality_of(attr, options = { })

have_association(type, name, options = {})

belong_to(name, options = {})
#  ex: model.should belong_to(:parent)
#             (or more detail) model.should belong_to(:parent, :class_name => 'People')
have_one(name, options = {})

have_many(name, options = {})

have_and_belong_to_many(name, options = {})

Example usage:

context "A User" do

  setup do
    @user = User.new(valid_user_attributes)
  end

  specify do
    @user.should validate_presence_of(:first_name)
  end

  specify do
    @user.should validate_length_of(:first_name, :in => 1..50)
  end

  specify do
    @user.should validate_presence_of(:last_name)
  end

  specify do
    @user.should validate_length_of(:last_name, :in => 1..50)
  end

  specify do
    @user.should validate_presence_of(:username)
  end

  specify do
    @user.should validate_length_of(:username, :in => 3..40)
  end

  specify do
    @user.should validate_presence_of(:email)
  end

  specify do
    @user.should validate_length_of(:email, :in => 3..100)
  end

  specify do
    @user.should validate_presence_of(:password)
  end

  specify do
    @user.should validate_confirmation_of(:password)
  end

  specify do
    @user.should validate_presence_of(:account_id)
  end

  specify do 
    @user.should belong_to(:account)
  end

  specify do 
    @user.should have_many(:project_assignments)
  end  

  specify do 
    @user.should have_many(:projects, :source => :project, :through => :project_assignments, :order=>"projects.name")
  end

end

Example output:

A User
- should validate presence of first_name
- should validate length of first_name
- should validate presence of last_name
- should validate length of last_name
- should validate presence of username
- should validate length of username
- should validate presence of email
- should validate length of email
- should validate presence of password
- should validate confirmation of password
- should validate presence of account_id
- should have a belongs_to association called :account
- should have a has_many association called :project_assignments
- should have a has_many association called :projects
Advertisements

Written by spicycode

April 1, 2007 at 5:55 pm

Posted in programming

Tagged with , ,

%d bloggers like this: