test_helper.rb 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # frozen_string_literal: true
  2. require "minitest/autorun"
  3. require File.expand_path("../test/dummy/config/environment.rb", __dir__)
  4. # Filter out Minitest backtrace while allowing backtrace from other libraries
  5. # to be shown.
  6. Minitest.backtrace_filter = Minitest::BacktraceFilter.new
  7. class ActiveSupport::TestCase
  8. teardown do
  9. Setting.destroy_all
  10. end
  11. def assert_number_of_queries(count, &block)
  12. queries_count = 0
  13. queries = []
  14. counter_f = lambda do |_name, _started, _finished, _unique_id, payload|
  15. unless payload[:name].in? %w[CACHE SCHEMA]
  16. queries_count += 1
  17. queries << payload
  18. end
  19. end
  20. ActiveSupport::Notifications.subscribed(counter_f, "sql.active_record", &block)
  21. assert_equal count, queries_count, message: queries.join("\n")
  22. end
  23. def assert_no_queries(&block)
  24. assert_number_of_queries 0, &block
  25. end
  26. def assert_errors_on(model, key, messages)
  27. messages = Array(messages) unless messages.is_a?(Array)
  28. assert_equal true, model.errors.has_key?(key), "#{model.errors.messages.keys} not include #{key}"
  29. assert_equal messages, model.errors.full_messages_for(key)
  30. end
  31. def assert_raise_with_validation_message(message)
  32. ex = assert_raise(ActiveRecord::RecordInvalid) { yield }
  33. assert_equal message, ex.message
  34. end
  35. end