rspec - Ruby EventMachine testing -
my first question concerning ruby. i'm trying test eventmachine interaction inside reactor loop - guess classified "functional" testing.
say have 2 classes - server , client. , want test both sides - need sure interaction.
server:
require 'singleton' class echoserver < em::connection include em::protocols::lineprotocol def post_init puts "-- connected echo server!" end def receive_data data send_data ">>>you sent: #{data}" close_connection if data =~ /quit/i end def unbind puts "-- disconnected echo server!" end end
client:
class echoclient < em::connection include em::protocols::lineprotocol def post_init send_data "hello" end def receive_data(data) @message = data p data end def unbind puts "-- disconnected echo server!" end end
so, i've tried different approaches , came nothing.
the fundamental question - somehow test code rspec, using should_recive?
eventmachine parameter should class or module, can't send instantiated/mocked code inside. right?
something this?
describe 'simple rspec test' 'should pass test' eventmachine.run { eventmachine::start_server "127.0.0.1", 8081, echoserver puts 'running echo server on 8081' echoserver.should_receive(:receive_data) eventmachine.connect '127.0.0.1', 8081, echoclient eventmachine.add_timer 1 puts 'second passed. stop loop.' eventmachine.stop_event_loop end } end end
and, if not, how em::spechelper? have code using it, , can't figure out i'm doing wrong.
describe 'when server run , client sends data' include em::spechelper default_timeout 2 def start_server em.start_server('0.0.0.0', 12345) { |ws| yield ws if block_given? } end def start_client client = em.connect('0.0.0.0', 12345, fakewebsocketclient) yield client if block_given? return client end describe "examples spec" "should accept single-frame text message" em { start_server start_client { |client| client.onopen { client.send_data("\x04\x05hello") } } } end end end
tried lot of variations of these tests , can't figure out. i'm sure i'm missing here...
thanks help.
the simplest solution can think of change this:
echoserver.should_receive(:receive_data)
to this:
echoserver.any_instance.should_receive(:receive_data)
since em expecting class start server, above any_instance
trick expect instance of class receive method.
the emspechelper example (while being official/standard) quite convoluted, i'd rather stick first rspec , use any_instance
, simplicity's sake.
Comments
Post a Comment