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?
You could try keeping track and closing the TCPServer in your before and after:
After each of the individual tests, the TCPServer is killed so you can create a new one with the same port.