forked from mongodb/mongoid
/
spec_helper.rb
84 lines (67 loc) · 2.25 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
$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "..", "lib"))
MODELS = File.join(File.dirname(__FILE__), "app/models")
$LOAD_PATH.unshift(MODELS)
require "mongoid"
require "mocha"
require "rspec"
# These environment variables can be set if wanting to test against a database
# that is not on the local machine.
ENV["MONGOID_SPEC_HOST"] ||= "localhost"
ENV["MONGOID_SPEC_PORT"] ||= "27017"
# These are used when creating any connection in the test suite.
HOST = ENV["MONGOID_SPEC_HOST"]
PORT = ENV["MONGOID_SPEC_PORT"].to_i
# Moped.logger.level = Logger::DEBUG
# Mongoid.logger.level = Logger::DEBUG
# When testing locally we use the database named mongoid_test. However when
# tests are running in parallel on Travis we need to use different database
# names for each process running since we do not have transactions and want a
# clean slate before each spec run.
def database_id
ENV["CI"] ? "mongoid_#{Process.pid}" : "mongoid_test"
end
# Can we connect to MongoHQ from this box?
def mongohq_connectable?
ENV["MONGOHQ_REPL_PASS"].present?
end
# Set the database that the spec suite connects to.
Mongoid.configure do |config|
config.connect_to(database_id)
end
# Autoload every model for the test suite that sits in spec/app/models.
Dir[ File.join(MODELS, "*.rb") ].sort.each do |file|
name = File.basename(file, ".rb")
autoload name.camelize.to_sym, name
end
module Rails
class Application
end
end
module MyApp
class Application < Rails::Application
end
end
RSpec.configure do |config|
# Mocha's syntax is preferred to RSpec.
config.mock_with(:mocha)
# Drop all collections and clear the identity map before each spec.
config.before(:each) do
Mongoid.purge!
Mongoid::IdentityMap.clear
end
# On travis we are creating many different databases on each test run. We
# drop the database after the suite.
config.after(:suite) do
if ENV["CI"]
Mongoid::Threaded.sessions[:default].drop
end
end
# Filter out MongoHQ specs if we can't connect to it.
config.filter_run_excluding(config: ->(value){
return true if value == :mongohq && !mongohq_connectable?
})
end
ActiveSupport::Inflector.inflections do |inflect|
inflect.singular("address_components", "address_component")
end