[mocha-developer] Testing event driven Socket classes

Jay jay at jayfields.com
Fri Dec 15 18:37:39 EST 2006


Looks like there's a couple options.

You could mock the start_server call:
EventMachine.expects(:start_server).with("192.168.2.252", 
6200,TickServer).yields(conn=mock)
conn.expects(:hello_world)

Or, you should be able to mock any instance of TickServer:
TickServer.any_instance.expects(:hello_world)

I didn't read anything in the code below that made me think this  
wouldn't work.

On Dec 15, 2006, at 6:17 PM, hemant wrote:

> Ok, here is the class, I want to Unit Test, its part of a large app
> and is based on EventMachine library.
>
> I want to mock the class TickServer ( i.e not stub it) . Since in
> actual scenario, you can't do this on this class:
>
> @server = TickServer.new # will toss an exception at your face
>
> you must initialize the server like this:
>
> EventMachine.run {
>   EventMachine.start_server("192.168.2.252",6200,TickServer) do |conn|
>     conn.hello_world
>   end
> }
>
> Where conn holds the instance of the class TickServer. So, I want to
> mock this class, so that i don't have to create an actual socket
> connection.
>
> Please look in the code, carefully. most of the methods in class are
> actually callbacks, which respond to certain events.
>
> Is it possible to unit test this with Mocha?
>
>
>
> require "eventmachine"
>
> class TickServer < EventMachine::Connection
>   attr_accessor :chunked_data,:client_status
>
>   def valid_protocol? data
>     return false unless data =~ /(\d{3})([^<>]*)<([^><]*)>##(.*?)##/
>     return true
>   end
>
>   # method is a callback, gets called whenever data is there in  
> socket to read
>   def receive_data data
>     data.chomp!
>     while data && data.length > 0
>       data_array = data.split(/\r?\n/m,2)
>       @chunked_data << data_array[0]
>       data = data_array[1]
>       # two level of protocol handling
>       if @chunked_data =~ /(\d{3})(.*)<(.*)?>(##(.*)##)/
>           if valid_protocol?(@chunked_data)
>             dispatch_request
>           else
>             send_error_without_callback "Invalid Protocol code"
>           end
>         @chunked_data = ""
>       end
>     end # end of while loop
>   end
>
>   def send_error_without_callback msg
>     send_data msg +"\n"
>   end
>
>   def dispatch_request
>     send_data "#{Time.now.to_s} #{@chunked_data} \n"
>   end
>
>   # this method gets automatically called, when a client connects
>   # so this place for initializing stuff
>   def post_init
>     @chunked_data = ""
>     @client_status = true
>   end
>
>   # method gets called when a client disconnects
>   def unbind
>     @client_status = false
>   end
>
>   def hello_world
>     p "Hello World"
>   end
>
> end
>
>
>
> -- 
> gnufied
> -----------
> There was only one Road; that it was like a great river: its springs
> were at every doorstep, and every path was its tributary.
> _______________________________________________
> mocha-developer mailing list
> mocha-developer at rubyforge.org
> http://rubyforge.org/mailman/listinfo/mocha-developer



More information about the mocha-developer mailing list