From 6cdae6dc7e852443b4aada50cbad7467b978179c Mon Sep 17 00:00:00 2001 From: Wojciech Piekutowski Date: Sun, 20 Feb 2011 16:00:06 +0100 Subject: [PATCH] Move helper methods away from tests setup to avoid redefinition on each test run --- test/test_helper.rb | 12 +++++++++++- test/unit/connection_test.rb | 13 ------------- test/unit/db_test.rb | 20 +++++++++----------- test/unit/repl_set_connection_test.rb | 13 ------------- 4 files changed, 20 insertions(+), 38 deletions(-) diff --git a/test/test_helper.rb b/test/test_helper.rb index 83167a5..6101e90 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -78,7 +78,17 @@ class Test::Unit::TestCase 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 + def assert_raise_error(klass, message) begin yield diff --git a/test/unit/connection_test.rb b/test/unit/connection_test.rb index 0491f40..ebb3068 100644 --- a/test/unit/connection_test.rb +++ b/test/unit/connection_test.rb @@ -3,19 +3,6 @@ include Mongo class ConnectionTest < Test::Unit::TestCase context "Initialization: " do - setup do - 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 - end - context "given a single node" do setup do @conn = Connection.new('localhost', 27017, :connect => false) diff --git a/test/unit/db_test.rb b/test/unit/db_test.rb index e84086b..5b41808 100644 --- a/test/unit/db_test.rb +++ b/test/unit/db_test.rb @@ -1,18 +1,16 @@ require './test/test_helper' +def insert_message(db, documents) + documents = [documents] unless documents.is_a?(Array) + message = ByteBuffer.new + message.put_int(0) + Mongo::BSON_CODER.serialize_cstr(message, "#{db.name}.test") + documents.each { |doc| message.put_array(Mongo::BSON_CODER.new.serialize(doc, true).to_a) } + message = db.add_message_headers(Mongo::Constants::OP_INSERT, message) +end + class DBTest < Test::Unit::TestCase context "DBTest: " do - setup do - def insert_message(db, documents) - documents = [documents] unless documents.is_a?(Array) - message = ByteBuffer.new - message.put_int(0) - Mongo::BSON_CODER..serialize_cstr(message, "#{db.name}.test") - documents.each { |doc| message.put_array(Mongo::BSON_CODER.new.serialize(doc, true).to_a) } - message = db.add_message_headers(Mongo::Constants::OP_INSERT, message) - end - end - context "DB commands" do setup do @conn = stub() diff --git a/test/unit/repl_set_connection_test.rb b/test/unit/repl_set_connection_test.rb index 1c449f8..6290123 100644 --- a/test/unit/repl_set_connection_test.rb +++ b/test/unit/repl_set_connection_test.rb @@ -3,19 +3,6 @@ include Mongo class ReplSetConnectionTest < Test::Unit::TestCase context "Initialization: " do - setup do - 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 - end - context "connecting to a replica set" do setup do TCPSocket.stubs(:new).returns(new_mock_socket('localhost', 27017))