[mocha-developer] Testing event driven Socket classes

hemant gethemant at gmail.com
Sat Dec 16 04:10:09 EST 2006


Ok, I sort of got it working.

class TickServerTest < Test::Unit::TestCase
  def setup
    setup_client
  end

  def setup_client
    EventMachine::Connection.any_instance.stubs(:receive_data).with(:data).returns(true)
    EventMachine::Connection.any_instance.stubs(:send_data).with(:data).returns(true)
    @server = TickServer.new gid
  end

  def gid
    Guid.new.hexdigest.to_s
  end

  def test_new_request
    request = "640 <ibm>##lol##"
    @server.expects(:send_error_without_callback).never

    # only one of the expected method gets called here
    # if you try to expect both, then you will get a failure
    # in your code

    #@server.expects(:dispatch_request).at_least_once
    @server.expects(:send_to_client).at_least_once


    @server.receive_data request
  end
end



And here is code, I am trying to test:

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 valid_protocol?(@chunked_data)
        dispatch_request
      else
        send_error_without_callback "Invalid Protocol code"
      end
      @chunked_data.replace("")

    end # end of while loop
  end

  def send_error_without_callback msg
    send_data msg +"\n"
  end

  def dispatch_request
    p "###Calling dispatch_request"
    send_to_client "#{Time.now.to_s} #{@chunked_data} \n"
  end

  def send_to_client msg
    p "###Calling send_to_client"
    send_data msg
  end
end

# above test case passes happily, without any problems, but what I
really wonder about the testcase is, the output:

Loaded suite tick_server_test
Started
"###Calling dispatch_request"
.
Finished in 0.002732 seconds.

1 tests, 2 assertions, 0 failures, 0 errors

If you look the two lines of test case:
    # @server.expects(:dispatch_request).at_least_once
    @server.expects(:send_to_client).at_least_once

It would mean, that method send_to_client must be called once, and
since test is passing, I would assume that indeed its getting called.
But then, why "###Calling send_to_client" is not getting printed?

Also, when I comment out test case line for dispatch_request, I get error:
#<Mocha::Mock:0x-242efa62>.send_to_client - expected calls: at least
1, actual calls: 0

How can this be true? since dispatch_request makes an immediate call
to send_to_client, whenever dispatch_request gets called
send_to_client also should be called.

I am surely missing something or have gone blind. Will anybody tell me
the reason of above behaviour.


More information about the mocha-developer mailing list