PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/test/test_helper.rb

https://github.com/ischroedi/petracommunity
Ruby | 124 lines | 80 code | 12 blank | 32 comment | 9 complexity | cbd22dfad98d7b49e3420294ff82a840 MD5 | raw file
  1. ENV["RAILS_ENV"] = "test"
  2. require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
  3. require 'test_help'
  4. class Test::Unit::TestCase
  5. # Transactional fixtures accelerate your tests by wrapping each test method
  6. # in a transaction that's rolled back on completion. This ensures that the
  7. # test database remains unchanged so your fixtures don't have to be reloaded
  8. # between every test method. Fewer database queries means faster tests.
  9. #
  10. # Read Mike Clark's excellent walkthrough at
  11. # http://clarkware.com/cgi/blosxom/2005/10/24#Rails10FastTesting
  12. #
  13. # Every Active Record database supports transactions except MyISAM tables
  14. # in MySQL. Turn off transactional fixtures in this case; however, if you
  15. # don't care one way or the other, switching from MyISAM to InnoDB tables
  16. # is recommended.
  17. self.use_transactional_fixtures = true
  18. # Instantiated fixtures are slow, but give you @david where otherwise you
  19. # would need people(:david). If you don't want to migrate your existing
  20. # test cases which use the @david style and don't mind the speed hit (each
  21. # instantiated fixtures translates to a database query per test method),
  22. # then set this back to true.
  23. self.use_instantiated_fixtures = false
  24. # Assert the form tag.
  25. def assert_form_tag(action)
  26. assert_tag "form", :attributes => { :action => action,
  27. :method => "post" }
  28. end
  29. # Assert submit button with optional label.
  30. def assert_submit_button(button_label = nil)
  31. if button_label
  32. assert_tag "input", :attributes => { :type => "submit",
  33. :value => button_label }
  34. else
  35. assert_tag "input", :attributes => { :type => "submit" }
  36. end
  37. end
  38. ERROR_DIV = { :tag => "div", :attributes => { :class => "fieldWithErrors" } }
  39. # Assert existence of form input field with attributes.
  40. def assert_input_field(name, value, field_type, size, maxlength, options = {})
  41. attributes = { :name => name,
  42. :type => field_type,
  43. :size => size,
  44. :maxlength => maxlength }
  45. # Surprisingly, attributes[:value] == nil is different from attributes
  46. # not having a :value key at all.
  47. attributes[:value] = value unless value.nil?
  48. tag = { :tag => "input", :attributes => attributes }
  49. # Merge tag hash with options, especially to handle :parent => ERROR_DIV
  50. # option in error tests.
  51. tag.merge!(options)
  52. assert_tag tag
  53. end
  54. # Test the minimum or maximum length of an attribute.
  55. def assert_length(boundary, object, attribute, length, options = {})
  56. valid_char = options[:valid_char] || "a"
  57. barely_invalid = barely_invalid_string(boundary, length, valid_char)
  58. # Test one over the boundary.
  59. object[attribute] = barely_invalid
  60. assert !object.valid?,
  61. "#{object[attribute]} (length #{object[attribute].length}) " +
  62. "should raise a length error"
  63. assert_equal correct_error_message(boundary, length),
  64. object.errors.on(attribute)
  65. # Test the boundary itself.
  66. barely_valid = valid_char * length
  67. object[attribute] = barely_valid
  68. assert object.valid?,
  69. "#{object[attribute]} (length #{object[attribute].length}) " +
  70. "should be on the boundary of validity"
  71. end
  72. # Create an attribute that is just barely invalid.
  73. def barely_invalid_string(boundary, length, valid_char)
  74. if boundary == :max
  75. invalid_length = length + 1
  76. elsif boundary == :min
  77. invalid_length = length - 1
  78. else
  79. raise ArgumentError, "boundary must be :max or :min"
  80. end
  81. valid_char * invalid_length
  82. end
  83. # Return the correct error message for the length test.
  84. def correct_error_message(boundary, length)
  85. error_messages = ActiveRecord::Errors.default_error_messages
  86. if boundary == :max
  87. sprintf(error_messages[:too_long], length)
  88. elsif boundary == :min
  89. sprintf(error_messages[:too_short], length)
  90. else
  91. raise ArgumentError, "boundary must be :max or :min"
  92. end
  93. end
  94. # Authorize a user.
  95. def authorize(user)
  96. @request.session[:user_id] = user.id
  97. end
  98. # Simulate an uploaded file.
  99. # From http://wiki.rubyonrails.org/rails/pages/HowtoUploadFiles
  100. def uploaded_file(filename, content_type)
  101. t = Tempfile.new(filename)
  102. t.binmode
  103. path = RAILS_ROOT + "/test/fixtures/" + filename
  104. FileUtils.copy_file(path, t.path)
  105. (class << t; self; end).class_eval do
  106. alias local_path path
  107. define_method(:original_filename) {filename}
  108. define_method(:content_type) {content_type}
  109. end
  110. return t
  111. end
  112. end