rails_helper.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # frozen_string_literal: true
  2. # This file is copied to spec/ when you run 'rails generate rspec:install'
  3. require 'simplecov'
  4. SimpleCov.start('rails')
  5. require 'spec_helper'
  6. require 'capybara_helper'
  7. ENV['RAILS_ENV'] ||= 'test'
  8. require File.expand_path('../config/environment', __dir__)
  9. # Prevent database truncation if the environment is production
  10. if Rails.env.production?
  11. abort('The Rails environment is running in production mode!')
  12. end
  13. require 'rspec/rails'
  14. # Add additional requires below this line. Rails is not loaded until this point!
  15. Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
  16. # Requires supporting ruby files with custom matchers and macros, etc, in
  17. # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
  18. # run as spec files by default. This means that files in spec/support that end
  19. # in _spec.rb will both be required and run as specs, causing the specs to be
  20. # run twice. It is recommended that you do not name files matching this glob to
  21. # end with _spec.rb. You can configure this pattern with the --pattern
  22. # option on the command line or in ~/.rspec, .rspec or `.rspec-local`.
  23. #
  24. # The following line is provided for convenience purposes. It has the downside
  25. # of increasing the boot-up time by auto-requiring all files in the support
  26. # directory. Alternatively, in the individual `*_spec.rb` files, manually
  27. # require only the support files necessary.
  28. #
  29. # Dir[Rails.root.join('spec', 'support', '**', '*.rb')].each { |f| require f }
  30. # Checks for pending migrations and applies them before tests are run.
  31. # If you are not using ActiveRecord, you can remove these lines.
  32. begin
  33. ActiveRecord::Migration.maintain_test_schema!
  34. rescue ActiveRecord::PendingMigrationError => e
  35. puts e.to_s.strip
  36. exit 1
  37. end
  38. RSpec.configure do |config|
  39. # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
  40. # config.fixture_path = "#{::Rails.root}/spec/fixtures"
  41. # If you're not using ActiveRecord, or you'd prefer not to run each of your
  42. # examples within a transaction, remove the following line or assign false
  43. # instead of true.
  44. config.use_transactional_fixtures = true
  45. # RSpec Rails can automatically mix in different behaviours to your tests
  46. # based on their file location, for example enabling you to call `get` and
  47. # `post` in specs under `spec/controllers`.
  48. #
  49. # You can disable this behaviour by removing the line below, and instead
  50. # explicitly tag your specs with their type, e.g.:
  51. #
  52. # RSpec.describe UsersController, :type => :controller do
  53. # # ...
  54. # end
  55. #
  56. # The different available types are documented in the features, such as in
  57. # https://relishapp.com/rspec/rspec-rails/docs
  58. config.infer_spec_type_from_file_location!
  59. # Filter lines from Rails gems in backtraces.
  60. config.filter_rails_from_backtrace!
  61. # arbitrary gems may also be filtered via:
  62. # config.filter_gems_from_backtrace("gem name")
  63. config.include FactoryBot::Syntax::Methods
  64. config.include GraphQLHelpers, type: :graphql
  65. config.before(:each, type: :system) do
  66. # Enforce app host location by setting on every spec. Yes, it is strange
  67. # that re-configuring on every system test is a necessity.
  68. if (app_host_port = ENV['APP_HOST_PORT'])
  69. Capybara.configure do |capybara_config|
  70. host_ip = Socket.getaddrinfo(
  71. Socket.gethostname,
  72. 'echo',
  73. Socket::AF_INET,
  74. )[0][3]
  75. capybara_config.app_host = "http://#{host_ip}:#{app_host_port}"
  76. capybara_config.default_host = "http://#{host_ip}:#{app_host_port}"
  77. end
  78. end
  79. driven_by selenium_driver
  80. end
  81. config.after(:each, type: :graphql) do
  82. if described_class.is_a? Resolvers::BaseResolver
  83. # binding.pry
  84. if described_class.is_a? Resolvers::BaseResolver
  85. described_class.instance_variable_set('@plurals', nil)
  86. end
  87. end
  88. end
  89. end
  90. Shoulda::Matchers.configure do |config|
  91. config.integrate do |with|
  92. with.test_framework :rspec
  93. with.library :rails
  94. end
  95. end