Posted By

tjstankus on 07/08/06


Tagged

testing rails


Versions (?)

Who likes this?

7 people have marked this snippet as a favorite

alvaroisorna
jonbaer
jpb
sergeizen
tkmr
webstic
schroedinger


Test for required attributes


 / Published in: Ruby
 

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

  1. def test_required_attributes
  2. required_atts = [:first_name, :last_name, :credentials, :title1,
  3. :department, :organization, :street, :city, :state, :zip,
  4. :phone, :email]
  5. required_atts.each do |att|
  6. r = create_registrant(att => nil)
  7. assert r.errors.on(att)
  8. end
  9. end
  10.  
  11. private
  12.  
  13. def create_registrant(options = {})
  14. Registrant.create({:meeting_id => 1,
  15. :first_name => \"John\",
  16. :middle_name => \"Q\",
  17. :last_name => \"Doe\",
  18. :credentials => \"PhD\",
  19. :title1 => \"Doc\",
  20. :title2 => \"\",
  21. :title3 => \"\",
  22. :department => \"Cardiology\",
  23. :organization => \"DUHS\",
  24. :street => \"12 Erwin Road\",
  25. :city => \"Durham\",
  26. :state => \"NC\",
  27. :zip => \"27707\",
  28. :phone => \"919-490-1000\",
  29. :fax => \"919-490-1001\",
  30. :email => \"[email protected]\"}.merge(options))
  31. end

Report this snippet  

You need to login to post a comment.