Skip to content

Commit 53cc6b5

Browse files
committed
remove tcpsocket stubbing
with FakeTCPSocket class
1 parent 76dde7b commit 53cc6b5

File tree

1 file changed

+33
-21
lines changed

1 file changed

+33
-21
lines changed

test/test_ldap_connection.rb

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,53 @@ def capture_stderr
99
$stderr = stderr
1010
end
1111

12+
# Fake socket for testing
13+
#
14+
# FakeTCPSocket.new("success", 636)
15+
# FakeTCPSocket.new("fail.SocketError", 636) # raises SocketError
16+
class FakeTCPSocket
17+
def initialize(host, port)
18+
status, error = host.split(".")
19+
if status == "fail"
20+
raise Object.const_get(error)
21+
end
22+
end
23+
end
24+
1225
def test_list_of_hosts_with_first_host_successful
1326
hosts = [
14-
['test.mocked.com', 636],
15-
['test2.mocked.com', 636],
16-
['test3.mocked.com', 636],
27+
["success.host", 636],
28+
["fail.SocketError", 636],
29+
["fail.SocketError", 636],
1730
]
18-
flexmock(TCPSocket).should_receive(:new).ordered.with(*hosts[0]).once.and_return(nil)
19-
flexmock(TCPSocket).should_receive(:new).ordered.never
20-
Net::LDAP::Connection.new(:hosts => hosts)
31+
32+
connection = Net::LDAP::Connection.new(:hosts => hosts)
33+
connection.socket_class = FakeTCPSocket
34+
connection.socket
2135
end
2236

2337
def test_list_of_hosts_with_first_host_failure
2438
hosts = [
25-
['test.mocked.com', 636],
26-
['test2.mocked.com', 636],
27-
['test3.mocked.com', 636],
39+
["fail.SocketError", 636],
40+
["success.host", 636],
41+
["fail.SocketError", 636],
2842
]
29-
flexmock(TCPSocket).should_receive(:new).ordered.with(*hosts[0]).once.and_raise(SocketError)
30-
flexmock(TCPSocket).should_receive(:new).ordered.with(*hosts[1]).once.and_return(nil)
31-
flexmock(TCPSocket).should_receive(:new).ordered.never
32-
Net::LDAP::Connection.new(:hosts => hosts)
43+
connection = Net::LDAP::Connection.new(:hosts => hosts)
44+
connection.socket_class = FakeTCPSocket
45+
connection.socket
3346
end
3447

3548
def test_list_of_hosts_with_all_hosts_failure
3649
hosts = [
37-
['test.mocked.com', 636],
38-
['test2.mocked.com', 636],
39-
['test3.mocked.com', 636],
50+
["fail.SocketError", 636],
51+
["fail.SocketError", 636],
52+
["fail.SocketError", 636],
4053
]
41-
flexmock(TCPSocket).should_receive(:new).ordered.with(*hosts[0]).once.and_raise(SocketError)
42-
flexmock(TCPSocket).should_receive(:new).ordered.with(*hosts[1]).once.and_raise(SocketError)
43-
flexmock(TCPSocket).should_receive(:new).ordered.with(*hosts[2]).once.and_raise(SocketError)
44-
flexmock(TCPSocket).should_receive(:new).ordered.never
54+
55+
connection = Net::LDAP::Connection.new(:hosts => hosts)
56+
connection.socket_class = FakeTCPSocket
4557
assert_raise Net::LDAP::ConnectionError do
46-
Net::LDAP::Connection.new(:hosts => hosts)
58+
connection.socket
4759
end
4860
end
4961

0 commit comments

Comments
 (0)