spec_helper.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # frozen_string_literal: true
  2. RSpec.configure do |config|
  3. config.example_status_persistence_file_path = 'tmp/rspec/examples.txt'
  4. config.expect_with :rspec do |expectations|
  5. expectations.include_chain_clauses_in_custom_matcher_descriptions = true
  6. end
  7. config.mock_with :rspec do |mocks|
  8. mocks.verify_partial_doubles = true
  9. config.around(:example, :without_verify_partial_doubles) do |example|
  10. mocks.verify_partial_doubles = false
  11. example.call
  12. mocks.verify_partial_doubles = true
  13. end
  14. end
  15. config.before :suite do
  16. Rails.application.load_seed
  17. Chewy.strategy(:bypass)
  18. # NOTE: we switched registrations mode to closed by default, but the specs
  19. # very heavily rely on having it enabled by default, as it relies on users
  20. # being approved by default except in select cases where explicitly testing
  21. # other registration modes
  22. Setting.registrations_mode = 'open'
  23. end
  24. config.after :suite do
  25. FileUtils.rm_rf(Dir[Rails.root.join('spec', 'test_files')])
  26. end
  27. # Use the GitHub Annotations formatter for CI
  28. if ENV['GITHUB_ACTIONS'] == 'true' && ENV['GITHUB_RSPEC'] == 'true'
  29. require 'rspec/github'
  30. config.add_formatter RSpec::Github::Formatter
  31. end
  32. end
  33. def body_as_json
  34. json_str_to_hash(response.body)
  35. end
  36. def json_str_to_hash(str)
  37. JSON.parse(str, symbolize_names: true)
  38. end
  39. def serialized_record_json(record, serializer, adapter: nil)
  40. options = { serializer: serializer }
  41. options[:adapter] = adapter if adapter.present?
  42. JSON.parse(
  43. ActiveModelSerializers::SerializableResource.new(
  44. record,
  45. options
  46. ).to_json
  47. )
  48. end
  49. def expect_push_bulk_to_match(klass, matcher)
  50. allow(Sidekiq::Client).to receive(:push_bulk)
  51. yield
  52. expect(Sidekiq::Client).to have_received(:push_bulk).with(hash_including({
  53. 'class' => klass,
  54. 'args' => matcher,
  55. }))
  56. end