Flexmock question

J

Jon A. Lambert

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

John Wilger

How do I use flexmock to mock a Socket?

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

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.

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

Jon A. Lambert

John said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top