File: spec_helper.rb

package info (click to toggle)
ruby-rspec-set 0.1.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 128 kB
  • sloc: ruby: 246; makefile: 4
file content (71 lines) | stat: -rw-r--r-- 1,490 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
59
60
61
62
63
64
65
66
67
68
69
70
71
require 'rspec-set'
require 'database_cleaner'
require 'active_record'

require_relative './fixtures/non_active_record_class'
require_relative './fixtures/active_record_class_example'

RSpec.configure do |config|
  config.treat_symbols_as_metadata_keys_with_true_values = true
  config.run_all_when_everything_filtered = true
  config.filter_run :focus

  config.before(:suite) do
    setup_database
  end

  config.after(:suite) do
    destroy_database
  end
  
  config.before(:each) do
    DatabaseCleaner.strategy = :transaction
    DatabaseCleaner.start
  end

  config.after(:each) do
    DatabaseCleaner.clean
  end

  config.order = 'random'
end

def db
  ActiveRecord::Base.connection
end

def setup_database
  ActiveRecord::Base.establish_connection(
    :adapter => 'sqlite3',
    :database => 'spec/db/rspec-set-test.sqlite3'
  )

  db.tables.each do |table|
    db.execute("DROP TABLE #{table}")
  end

  Dir[File.join(File.dirname(__FILE__), "db/migrate", "*.rb")].each do |f| 
    require f
    migration = Kernel.const_get(f.split("/").last.split(".rb").first.gsub(/\d+/, "").split("_").collect{|w| w.strip.capitalize}.join())
    migration.migrate(:up)
  end
end

def destroy_database
  db.tables.each do |table|
    db.execute("DROP TABLE #{table}")
  end
end

def setup_for_error_checking(klass)
  before do
    @orig_stderr = $stderr
    $stderr = StringIO.new
  end

  set(:my_object) { klass.create(name: 'Test Name') }

  after do
    $stderr = @orig_stderr
  end
end