File: test_helper.rb

package info (click to toggle)
ruby-state-machines-activerecord 0.9.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 340 kB
  • sloc: ruby: 1,922; makefile: 5
file content (58 lines) | stat: -rw-r--r-- 1,649 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
begin
  require 'pry-byebug'
rescue LoadError
end
require 'minitest/reporters'
Minitest::Reporters.use!(Minitest::Reporters::SpecReporter.new)
require 'state_machines-activerecord'
require 'minitest/autorun'
require 'securerandom'

# Establish database connection
ActiveRecord::Base.establish_connection('adapter' => 'sqlite3', 'database' => ':memory:')
ActiveRecord::Base.logger = Logger.new("#{File.dirname(__FILE__)}/../log/active_record.log")
ActiveSupport.test_order = :random

class BaseTestCase < ActiveSupport::TestCase
  protected
  # Creates a new ActiveRecord model (and the associated table)
  def new_model(create_table = :foo, &block)
    name = create_table || :foo
    table_name = "#{name}_#{SecureRandom.hex(6)}"

    model = Class.new(ActiveRecord::Base) do
      self.table_name = table_name.to_s
      connection.create_table(table_name, :force => true) { |t| t.string(:state) } if create_table

      define_method(:abort_from_callback) do
        throw :abort
      end

      (
      class << self;
        self;
      end).class_eval do
        define_method(:name) { "#{name.to_s.capitalize}" }
      end
    end
    model.class_eval(&block) if block_given?
    model.reset_column_information if create_table
    model
  end

  def clear_active_support_dependencies
    return unless defined?(ActiveSupport::Dependencies)

    if ActiveSupport::Dependencies.respond_to?(:autoloader=)
      ActiveSupport::Dependencies.autoloader ||= stubbed_autoloader
    end

    ActiveSupport::Dependencies.clear
  end

  def stubbed_autoloader
    Object.new.tap do |obj|
      obj.define_singleton_method(:reload) {}
    end
  end
end