-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathhelper.rb
More file actions
96 lines (79 loc) · 2.28 KB
/
helper.rb
File metadata and controls
96 lines (79 loc) · 2.28 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# frozen_string_literal: true
require "bundler"
require "minitest/reporters"
Bundler.setup :default, :test
if ENV['CIRCLECI'] == "true"
Minitest::Reporters.use! Minitest::Reporters::JUnitReporter.new
else
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new
end
ENV["DATABASE_URL"] ||= "postgres:///queue_classic_test"
require_relative '../lib/queue_classic'
require "stringio"
require 'timeout'
require "minitest/autorun"
class QCTest < Minitest::Test
def setup
init_db
end
def teardown
QC.delete_all
end
def init_db
c = QC::ConnAdapter.new
c.execute("SET client_min_messages TO 'warning'")
QC::Setup.drop(c.connection)
QC::Setup.create(c.connection)
c.execute(File.read('./test/helper.sql'))
c.disconnect
end
def capture_stderr_output
original_stderr = $stderr
$stderr = StringIO.new
yield
$stderr.string
ensure
$stderr = original_stderr
end
def capture_debug_output
original_debug = ENV['DEBUG']
original_stdout = $stdout
ENV['DEBUG'] = "true"
$stdout = StringIO.new
yield
$stdout.string
ensure
ENV['DEBUG'] = original_debug
$stdout = original_stdout
end
def with_env(temporary_environment)
original_environment = {}
temporary_environment.each do |name, value|
original_environment[name] = ENV[name]
ENV[name] = value
end
yield
ensure
original_environment.each { |name, value| ENV[name] = value }
end
def stub_any_instance(class_name, method_name, definition)
new_method_name = "new_#{method_name}"
original_method_name = "original_#{method_name}"
method_present = class_name.instance_methods(false).include? method_name
if method_present
class_name.send(:alias_method, original_method_name, method_name)
class_name.send(:define_method, new_method_name, definition)
class_name.send(:alias_method, method_name, new_method_name)
yield
else
message = "#{class_name} does not have method #{method_name}."
message << "\nAvailable methods: #{class_name.instance_methods(false)}"
raise ArgumentError.new message
end
ensure
if method_present
class_name.send(:alias_method, method_name, original_method_name)
class_name.send(:undef_method, new_method_name)
end
end
end