This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
spec_helper.rb
135 lines (111 loc) · 3.26 KB
/
spec_helper.rb
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
ENV["RACK_ENV"] ||= 'test'
require 'active_record'
require 'database_rewinder'
require 'days'
require 'days/models'
require 'days/migrator'
require 'rack/test'
require 'pry'
Days::App.set :environment, :test
Days::App.set :raise_errors, true
module AppSpecHelper
include Rack::Test::Methods
def app
::Days::App.rack
end
def response
last_response
end
def env
@env ||= {'rack.session' => session}
end
def session
@session ||= {}
end
def last_render
@renders.last
end
def render
subject; @renders.last
end
def login(user)
session.merge!(user_id: user.id)
env
end
def self.included(k)
k.module_eval do
before(:all) do
Days::App.config = RSpec.configuration.days_config
ActiveRecord::Base.logger = nil
end
before(:example) do |example|
@renders = []
unless example.metadata[:render]
allow_any_instance_of(Days::App).to receive(:render) do |instance, *args, &block|
@renders << {engine: args[0], data: args[1], options: args[2] || {}, locals: args[3] || {}, ivars: args[4] || {}}
""
end
Days::App.class_eval do
alias render_orig render
def render(*args)
render_orig *args, Hash[self.instance_variables.map{ |k| [k, instance_variable_get(k)] }]
end
private :render, :render_orig
end
end
end
end
end
end
module SetupAndTeardown
extend ActiveSupport::Concern
module ClassMethods
def setup(*methods)
methods.each do |m|
prepend_before do
send m
end
end
end
def teardown(*methods)
methods.each do |m|
after do
send m
end
end
end
end
end
RSpec.configure do |config|
config.run_all_when_everything_filtered = true
config.filter_run :focus
# config.order = 'random'
env = File.join(File.dirname(__FILE__), 'environment')
config.add_setting :days_env, default: env
config.add_setting :days_config, default: Days::Config.new(File.join(env, 'config.yml'))
config.before(:suite) do
Days::App.environment = ENV["RACK_ENV"] || :test
DatabaseRewinder.database_configuration = { 'test' => config.days_config['database'] }
config.days_config.establish_db_connection()
ActiveRecord::Base.configurations = {'test' => Hash[config.days_config.database]}
Days::Migrator.start(config.days_config, verbose: true)
ActiveRecord::Base.logger = nil
DatabaseRewinder.clean_all
end
config.after(:each) do
DatabaseRewinder.clean
end
config.include AppSpecHelper, type: :controller
config.tty = true
config.mock_with :rspec do |mocks|
# In RSpec 3, `any_instance` implementation blocks will be yielded the receiving
# instance as the first block argument to allow the implementation block to use
# the state of the receiver.
# In RSpec 2.99, to maintain compatibility with RSpec 3 you need to either set
# this config option to `false` OR set this to `true` and update your
# `any_instance` implementation blocks to account for the first block argument
# being the receiving instance.
mocks.yield_receiver_to_any_instance_implementation_blocks = true
end
end
require_relative "./shared/admin"