Friday, 15 February 2013

unit testing - Python Mock not asserting calls -



unit testing - Python Mock not asserting calls -

i'm using mock library patch class in programme connects external resource , sends dictioanry.

the construction goes litle this...

code.py

def make_connection(): connection = originalclass(host, port) connection.connect() connection.send(param) connection.close()

test.py

@mock.path('code.originalclass') def test_connection(self, mocked_conn): code.make_connection() mocked_conn.assert_called_with(host, port) mocked_conn.connect.assert_called_once() mocked_conn.send.assert_called_with(param)

the first assert_called_with works perfectly, calls method of mocked class don't pass. have tried using patch.object decorator no luck.

the connect() , send() methods called on return value of first call; adjust test accordingly:

mocked_conn.return_value.connect.assert_called_once() mocked_conn.return_value.send.assert_called_with(param)

i store reference 'instance' first:

@mock.path('code.originalclass') def test_connection(self, mocked_conn): code.make_connection() mocked_conn.assert_called_with(host, port) mocked_instance = mocked_conn.return_value mocked_instance.connect.assert_called_once() mocked_instance.send.assert_called_with(param)

python unit-testing mocking python-mock

No comments:

Post a Comment