我解決了這個問題,只需在EventMachine中實例化連接類的實例, tehn直接調用該方法 - 不知道爲什麼我沒有想到這一點!
describe 'Network manager' do
it 'should call the ChangeStatus handler when it receives the ChangeStatus packet' do
# Arrange
connection = TcpConnection.new
# Set up the packet handler
packet_handler = double 'TcpPacketHandler'
# Inject dependency
connection.packet_handler = packet_handler
# Create the message
data = [ 15, # message code + payload size
0, # message code
4, # protocol version
12, # size of string
]
data.push 'hello, world'.bytes.to_a
reader, writer = IO.pipe
writer.write data
writer.close
puts data
# Assert
expect(packet_handler).to receive(:handle_message).with(data[1], anything()).once
message = reader.read
# Act
connection.receive_data message
end
end
我必須說我不太瞭解Ruby,但肯定有一個半可接受的嘲笑框架可以用來解決這個問題嗎? –
這不是使用模擬框架的問題,我問如何模擬連接到服務器的連接與交互之間的交互。 :(例如,我想知道如何讓'我的課程'意識到它已經獲得了連接。EventMachine似乎是通過魔術來實現的 –
一個更好的例子是在C#中(儘管它仍然發生在Ruby中)你有BeginAcceptAsync方法 - 你如何在測試中觸發一個事件來說明已經接收到連接(並且因此調用EndAcceptAsync)? –