[mocha-developer] Testing event driven Socket classes

hemant gethemant at gmail.com
Fri Dec 15 18:52:52 EST 2006


On 12/16/06, Jay <jay at jayfields.com> wrote:
> 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.
>

Cool...

I did this:
$:.unshift File.join(File.dirname(__FILE__), "mocha", "lib")
require "mocha"
require "tick_server"
require "test/unit"

class TickServerTest < Test::Unit::TestCase

  def setup
    setup_client
  end

  def setup_client
    EventMachine::Connection.stubs(:initialize).returns(true)
    @server = TickServer.new "foo"
  end

  def test_new_request

    request = "310 <IBM>##lol##"
    @server.receive_data request
    @server.expects(:send_error_without_callback).never
  end

end

I know, I did wrong, and i got the error:

Loaded suite tick_server_test
Started
terminate called after throwing an instance of 'std::runtime_error'
  what():  not initialized
Aborted

I will try your approach and see.

-- 
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.


More information about the mocha-developer mailing list