Flexmock question

How do I use flexmock to mock a Socket?

It seems to trip up on the defining a method named 'send' on the object.

Simple code to reproduce the problem.

···

-----
require 'test/unit'
require 'flexmock'

class TestSocket < Test::Unit::TestCase
  def setup
    @sock = FlexMock.new
    @sock.mock_handle(:recv, 1) { |size,flag| "hello world\r\n" }
    @sock.mock_handle(:send, 1) { |message,flag| message.size }
    @sock.mock_handle(:xsend, 1) { |message,flag| message.size }
  end

  def test_recv
    assert_equal("hello world\r\n", @sock.recv)
  end

  def test_send
    assert_equal(13, @sock.send("hello world\r\n"))
  end

  def test_xsend
    assert_equal(13, @sock.xsend("hello world\r\n"))
  end

end
-------------

$ ruby test/test_socket.rb
Loaded suite test/test_socket
Started
.E.
Finished in 0.003 seconds.

  1) Error:
test_send(TestSocket):
NoMethodError: undefined method `hello world
' for #<FlexMock:0x25f94a8>
    /usr/lib/ruby/gems/1.8/gems/flexmock-0.0.3-/lib/flexmock.rb:72:in `method_missing'
    test/test_socket.rb:17:in `send'
    test/test_socket.rb:17:in `test_send'

3 tests, 2 assertions, 0 failures, 1 errors

--
J. Lambert

You can't mock out the #send method on a FlexMock object, because that
method is already defined (the usual Object#send method). Your choices
here are to either reopen the FlexMock class and undef the #send
method (probably not a good idea), or just create your own MockSocket
class and define the methods you need in it.

···

On 10/1/05, Jon A. Lambert <jlsysinc@alltel.net> wrote:

How do I use flexmock to mock a Socket?

It seems to trip up on the defining a method named 'send' on the object.

--
Regards,
John Wilger

-----------
Alice came to a fork in the road. "Which road do I take?" she asked.
"Where do you want to go?" responded the Cheshire cat.
"I don't know," Alice answered.
"Then," said the cat, "it doesn't matter."
- Lewis Carrol, Alice in Wonderland

John Wilger wrote:

You can't mock out the #send method on a FlexMock object, because that
method is already defined (the usual Object#send method). Your choices
here are to either reopen the FlexMock class and undef the #send
method (probably not a good idea), or just create your own MockSocket
class and define the methods you need in it.

class FlexMock
  undef_method(:send)
end

I'm not using #send on any of my other mocked objects so this works. I checked that if I do hit code that uses #send on a mock object, other than my mock socket, it will cause the test to fail.

Thanks

···

--
J Lambert