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. # end
  46. def check_validations_for(field, &checks)
  47. define_method("test_validations_for_#{field}") do
  48. ValidationChecker.new(field, checks, self, instance).run
  49. end
  50. end
  51. end
  52.  
  53. class ValidationChecker < Struct.new(:field_name, :proc, :testcase, :instance)
  54. # Assign the given value to the instance
  55. def assign(value)
  56. instance.send "#{field_name}=", value
  57. end
  58.  
  59. # Ensure that this value is invalid.
  60. # If a message is supplied, it will be matched against the validation error message.
  61. # The message is matched using === so you can use Regexp's.
  62. def invalid(value, message = nil)
  63. assign value
  64. assert ! instance.valid?, "expected #{field_name} = #{value.inspect} to be invalid"
  65.  
  66. return unless message
  67.  
  68. found_msg = instance.errors.on(field_name)
  69. assert message === found_msg, "\nexpected error message: #{message.inspect}\nfound: #{found_msg.inspect}"
  70. end
  71.  
  72. # Ensure that the given value is valid.
  73. def valid(value)
  74. assign value
  75. assert instance.valid?, "expected #{field_name} = #{value.inspect} to be valid"
  76. assert instance.save # Make sure the database agree's with the validations
  77. end
  78.  
  79. # Run the validation tests
  80. def run
  81. assert(instance.valid?, "Initial object ought to be valid!")
  82. instance_eval(&proc)
  83. end
  84.  
  85. def assert
  86. end
  87.  
  88. undef_method :assert # Use the testcase's method
  89.  
  90. # Forward missing methods to the testcase if possible
  91. def method_missing(sel, *args)
  92. testcase.respond_to?(sel) ? testcase.send(sel, *args) : super(sel, *args)
  93. end
  94. end
  95.  
  96. def self.append_features(cls)
  97. cls.send :include, InstanceMethods
  98. class << cls
  99. include ClassMethods
  100. end
  101. end
  102. 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.