Reputation: 125
I'm attempting to write tests around an application that makes heavy use of TCPSockets (an IRC bot to be specific). While writing my first class's tests, I had been skimping by with doing:
#In the describe block
before(:all) { TCPServer.new 6667 }
...which allowed for my TCPSockets to function (by connecting to localhost:6667), though they are not actually being properly mocked. However, this has now caused problems when moving onto my second class as I cannot create a TCPServer on the same port.
How can I mock the TCPSocket class in such a way that will allow me to test things such as its(:socket) { should be_kind_of(TCPSocket) }
and other common operations like #readline and #write?
Upvotes: 2
Views: 1816
Reputation: 101
You could try keeping track and closing the TCPServer in your before and after:
before do
@server = TCPServer.new 6667
end
after do
@server.close
end
it ... do
end
it ... do
end
After each of the individual tests, the TCPServer is killed so you can create a new one with the same port.
Upvotes: 3
Reputation: 754
I'm not quite sure if I understand your problem, but why don't you just install some kind irc server on your local machine? ircd-irc2, ircd-hybrid or something like that?
Suppose you have irc client implemented this way:
class Bot
attr_accessor :socket
def initialize
socket = TCPSocket.new("localhost", 6667)
end
end
You can then test it like this
let(:bot) { Bot.new }
it "should be kind of TCP Socket"
bot.should be_kind_of(TCPSocket)
bot.should be_a(TCPSocket)
end
Upvotes: 0