spec_helper.rb 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. $LOAD_PATH.push File.expand_path('../lib', __FILE__)
  2. require 'rspec'
  3. require 'rails/all'
  4. require 'sqlite3'
  5. require 'simplecov'
  6. if ENV['CI'] == 'true'
  7. require 'codecov'
  8. SimpleCov.formatter = SimpleCov::Formatter::Codecov
  9. end
  10. SimpleCov.start
  11. require 'rails-settings-cached'
  12. if RailsSettings::Settings.respond_to? :raise_in_transactional_callbacks=
  13. RailsSettings::Settings.raise_in_transactional_callbacks = true
  14. end
  15. class TestApplication < Rails::Application
  16. end
  17. module Rails
  18. def self.root
  19. Pathname.new(File.expand_path('../', __FILE__))
  20. end
  21. def self.cache
  22. @cache ||= ActiveSupport::Cache::MemoryStore.new
  23. end
  24. def self.env
  25. 'test'
  26. end
  27. end
  28. def count_queries(&block)
  29. count = 0
  30. counter_f = lambda do |_name, _started, _finished, _unique_id, payload|
  31. count += 1 unless payload[:name].in? %w(CACHE SCHEMA)
  32. end
  33. ActiveSupport::Notifications.subscribed(counter_f, 'sql.active_record', &block)
  34. count
  35. end
  36. # run cache initializers
  37. RailsSettings::Railtie.initializers.each(&:run)
  38. # ActiveRecord::Base.logger = Logger.new(STDOUT)
  39. ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: ':memory:'
  40. # ActiveRecord::Base.configurations = true
  41. ActiveRecord::Schema.verbose = false
  42. ActiveRecord::Schema.define(version: 1) do
  43. create_table :settings do |t|
  44. t.string :var, null: false
  45. t.text :value
  46. t.integer :thing_id
  47. t.string :thing_type, limit: 30
  48. t.datetime :created_at
  49. t.datetime :updated_at
  50. end
  51. create_table :users do |t|
  52. t.string :login
  53. t.string :password
  54. t.datetime :created_at
  55. t.datetime :updated_at
  56. end
  57. end
  58. RSpec.configure do |config|
  59. config.before(:all) do
  60. class Setting < RailsSettings::Base
  61. end
  62. class CustomSetting < RailsSettings::Base
  63. end
  64. class User < ActiveRecord::Base
  65. include RailsSettings::Extend
  66. end
  67. ActiveRecord::Base.connection.execute('delete from settings')
  68. Rails.cache.clear
  69. end
  70. config.after(:all) do
  71. Object.send(:remove_const, :Setting)
  72. end
  73. end
  74. Rails.application.instance_variable_set('@initialized', true)