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_handlerecv, 1) { |size,flag| "hello world\r\n" }
@sock.mock_handlesend, 1) { |message,flag| message.size }
@sock.mock_handlexsend, 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
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_handlerecv, 1) { |size,flag| "hello world\r\n" }
@sock.mock_handlesend, 1) { |message,flag| message.size }
@sock.mock_handlexsend, 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