mongo-ruby-driver/test/test_helper.rb

103 lines
1.9 KiB
Ruby
Raw Normal View History

$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
require 'rubygems' if ENV['C_EXT']
2009-11-30 22:50:54 +00:00
require 'mongo'
require 'test/unit'
2011-02-20 14:38:21 +00:00
def silently
warn_level = $VERBOSE
$VERBOSE = nil
result = yield
$VERBOSE = warn_level
result
end
begin
require 'rubygems'
2011-02-20 14:38:21 +00:00
silently { require 'shoulda' }
require 'mocha'
2011-02-20 14:39:09 +00:00
rescue LoadError
puts <<MSG
2010-02-22 20:49:04 +00:00
This test suite requires shoulda and mocha.
You can install them as follows:
gem install shoulda
gem install mocha
MSG
2011-02-20 14:39:09 +00:00
exit
end
require 'bson_ext/cbson' if !(RUBY_PLATFORM =~ /java/) && ENV['C_EXT']
unless defined? MONGO_TEST_DB
MONGO_TEST_DB = 'ruby-test-db'
end
2010-04-05 19:48:35 +00:00
unless defined? TEST_PORT
TEST_PORT = ENV['MONGO_RUBY_DRIVER_PORT'] ? ENV['MONGO_RUBY_DRIVER_PORT'].to_i : Mongo::Connection::DEFAULT_PORT
end
unless defined? TEST_HOST
TEST_HOST = ENV['MONGO_RUBY_DRIVER_HOST'] || 'localhost'
end
class Test::Unit::TestCase
include Mongo
2010-04-05 14:39:55 +00:00
include BSON
2009-11-23 18:13:14 +00:00
def self.standard_connection(options={})
Connection.new(TEST_HOST, TEST_PORT, options)
end
def standard_connection(options={})
self.class.standard_connection(options)
end
def self.host_port
"#{mongo_host}:#{mongo_port}"
end
def self.mongo_host
TEST_HOST
end
def self.mongo_port
TEST_PORT
end
def host_port
self.class.host_port
end
def mongo_host
self.class.mongo_host
end
def mongo_port
self.class.mongo_port
end
def new_mock_socket(host='localhost', port=27017)
socket = Object.new
socket.stubs(:setsockopt).with(Socket::IPPROTO_TCP, Socket::TCP_NODELAY, 1)
socket.stubs(:close)
socket
end
def new_mock_db
db = Object.new
end
2010-05-04 20:00:05 +00:00
def assert_raise_error(klass, message)
begin
yield
rescue => e
assert_equal klass, e.class
assert e.message.include?(message), "#{e.message} does not include #{message}."
else
flunk "Expected assertion #{klass} but none was raised."
2010-05-04 20:00:05 +00:00
end
end
end