Posted By

Catfish on 09/20/06


Tagged

testing rails activerecord


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

webstic


ARTestHelper


 / Published in: Ruby
 

  1. # A mixin for Test::Unit classes to ease AR testing
  2. #
  3. # Use in your test class by adding
  4. #
  5. # include ARTestHelper
  6. #
  7. # Written by Nicholas Seckar aka Ulysses
  8. module ARTestHelper
  9. module InstanceMethods
  10. # Access the temporary instance to test.
  11. # In my setup method, I store a new, valid instance in an instance variable such as @p.
  12. #
  13. # The ivar is the first character of the class (@p for Page, @u for User), and usually has an
  14. # attr_reader for it.
  15. #
  16. # This method should return that instance so that the validation checker can access it.
  17. def instance
  18. instance_variable_get("@#{self.class.name[0, 1].downcase}")
  19. end
  20. end
  21.  
  22. module ClassMethods
  23. # Test that the named associations can be loaded
  24. # Catches errors such as missing tables, classes, etc.
  25. # Only use one directive per test-case
  26. #
  27. # Example: check_associations %w(friends downloads links)
  28. def check_associations(*names)
  29. define_method "test_associations" do
  30. assert(instance.save, 'Must be able to save instance!') if instance.new_record?
  31. names.flatten.each do |assoc|
  32. v = instance.send assoc
  33. v.each { } if v.respond_to?(:each) # Load collections by using each
  34. end
  35. end
  36. end
  37.  
  38. # Check the validations for the named field
  39. #
  40. # Example:
  41. #
  42. # check_validations_for :email do
  43. # invalid nil, "can't be blank"
  44. # invalid 'boo', /not .* valid .* email/
  45. # valid '[email protected]'
  46. # end
  47. def check_validations_for(field, &checks)
  48. define_method("test_validations_for_#{field}") do
  49. ValidationChecker.new(field, checks, self, instance).run
  50. end
  51. end
  52. end
  53.  
  54. class ValidationChecker < Struct.new(:field_name, :proc, :testcase, :instance)
  55. # Assign the given value to the instance
  56. def assign(value)
  57. instance.send "#{field_name}=", value
  58. end
  59.  
  60. # Ensure that this value is invalid.
  61. # If a message is supplied, it will be matched against the validation error message.
  62. # The message is matched using === so you can use Regexp's.
  63. def invalid(value, message = nil)
  64. assign value
  65. assert ! instance.valid?, "expected #{field_name} = #{value.inspect} to be invalid"
  66.  
  67. return unless message
  68.  
  69. found_msg = instance.errors.on(field_name)
  70. assert message === found_msg, "\nexpected error message: #{message.inspect}\nfound: #{found_msg.inspect}"
  71. end
  72.  
  73. # Ensure that the given value is valid.
  74. def valid(value)
  75. assign value
  76. assert instance.valid?, "expected #{field_name} = #{value.inspect} to be valid"
  77. assert instance.save # Make sure the database agree's with the validations
  78. end
  79.  
  80. # Run the validation tests
  81. def run
  82. assert(instance.valid?, "Initial object ought to be valid!")
  83. instance_eval(&proc)
  84. end
  85.  
  86. def assert
  87. end
  88.  
  89. undef_method :assert # Use the testcase's method
  90.  
  91. # Forward missing methods to the testcase if possible
  92. def method_missing(sel, *args)
  93. testcase.respond_to?(sel) ? testcase.send(sel, *args) : super(sel, *args)
  94. end
  95. end
  96.  
  97. def self.append_features(cls)
  98. cls.send :include, InstanceMethods
  99. class << cls
  100. include ClassMethods
  101. end
  102. end
  103. end

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Catfish on September 20, 2006

See http://snipplr.com/view/1100/artesthelper-example/ for sample

You need to login to post a comment.