Return to Snippet

Revision: 298
at July 8, 2006 20:21 by tjstankus


Updated Code
def test_required_attributes
    required_atts = [:first_name, :last_name, :credentials, :title1, 
                     :department, :organization, :street, :city, :state, :zip, 
                     :phone, :email]
    required_atts.each do |att|
      r = create_registrant(att => nil)
      assert r.errors.on(att)
    end
  end
  
  private
  
  def create_registrant(options = {})
    Registrant.create({:meeting_id => 1, 
                       :first_name => \"John\", 
                       :middle_name => \"Q\", 
                       :last_name => \"Doe\", 
                       :credentials => \"PhD\", 
                       :title1 => \"Doc\", 
                       :title2 => \"\", 
                       :title3 => \"\", 
                       :department => \"Cardiology\", 
                       :organization => \"DUHS\", 
                       :street => \"12 Erwin Road\", 
                       :city => \"Durham\", 
                       :state => \"NC\", 
                       :zip => \"27707\", 
                       :phone => \"919-490-1000\", 
                       :fax => \"919-490-1001\", 
                       :email => \"[email protected]\"}.merge(options))
  end

Revision: 297
at July 8, 2006 20:17 by tjstankus


Initial Code
  def test_required_attributes
    required_atts = [:first_name, :last_name, :credentials, :title1, 
                     :department, :organization, :street, :city, :state, :zip, 
                     :phone, :email]
    required_atts.each do |att|
      r = create_registrant(att => nil)
      assert r.errors.on(att)
    end
  end
  
  private
  
  def create_registrant(options = {})
    Registrant.create({:meeting_id => 1, 
                       :first_name => \"John\", 
                       :middle_name => \"Q\", 
                       :last_name => \"Doe\", 
                       :credentials => \"PhD\", 
                       :title1 => \"Doc\", 
                       :title2 => \"\", 
                       :title3 => \"\", 
                       :department => \"Cardiology\", 
                       :organization => \"DUHS\", 
                       :street => \"12 Erwin Road\", 
                       :city => \"Durham\", 
                       :state => \"NC\", 
                       :zip => \"27707\", 
                       :phone => \"919-490-1000\", 
                       :fax => \"919-490-1001\", 
                       :email => \"[email protected]\"}.merge(options))
  end

Initial URL


Initial Description
A fairly DRY way to test for required attributes of a model. It's sort of an alternative to fixtures. It breaks the one-assertion-per-test rule that some people favor. Drop in your unit test and edit as necessary.

A better (yet not quite perfect) approach:
http://johnwilger.com/articles/2005/12/07/a-bit-of-dryness-for-unit-tests-in-rails

Initial Title
Test for required attributes

Initial Tags
rails

Initial Language
Ruby