/test/support/helpers.rb

https://github.com/bwalding/devise · Ruby · 60 lines · 49 code · 9 blank · 2 comment · 0 complexity · 42bffa40b1b774270f0ca60124fa94b0 MD5 · raw file

  1. require 'active_support/test_case'
  2. class ActiveSupport::TestCase
  3. VALID_AUTHENTICATION_TOKEN = 'AbCdEfGhIjKlMnOpQrSt'.freeze
  4. def setup_mailer
  5. ActionMailer::Base.deliveries = []
  6. end
  7. def store_translations(locale, translations, &block)
  8. begin
  9. I18n.backend.store_translations(locale, translations)
  10. yield
  11. ensure
  12. I18n.reload!
  13. end
  14. end
  15. def generate_unique_email
  16. @@email_count ||= 0
  17. @@email_count += 1
  18. "test#{@@email_count}@example.com"
  19. end
  20. def valid_attributes(attributes={})
  21. { :username => "usertest",
  22. :email => generate_unique_email,
  23. :password => '123456',
  24. :password_confirmation => '123456' }.update(attributes)
  25. end
  26. def new_user(attributes={})
  27. User.new(valid_attributes(attributes))
  28. end
  29. def create_user(attributes={})
  30. User.create!(valid_attributes(attributes))
  31. end
  32. def create_admin(attributes={})
  33. valid_attributes = valid_attributes(attributes)
  34. valid_attributes.delete(:username)
  35. Admin.create!(valid_attributes)
  36. end
  37. # Execute the block setting the given values and restoring old values after
  38. # the block is executed.
  39. def swap(object, new_values)
  40. old_values = {}
  41. new_values.each do |key, value|
  42. old_values[key] = object.send key
  43. object.send :"#{key}=", value
  44. end
  45. yield
  46. ensure
  47. old_values.each do |key, value|
  48. object.send :"#{key}=", value
  49. end
  50. end
  51. end