lundi 23 novembre 2015

How to write a unit test with mockito in Python

I'm a newbe in Python coding and I have problem with mockito in Python.

My 'production' code looks like below:

from stompest.config import StompConfig
from stompest.sync import Stomp


class Connector:
    def sendMessage(self):
        message = {'message'}
        dest = '/queue/foo'

    def _send(self, message='', dest=''):
        config = StompConfig(uri="tcp://localhost:61613")
        client = Stomp(config)
        client.connect()
        client.send(body=message, destination=dest,
                    headers='')
        client.disconnect()

as you see I would like to send a message using Stomp protocol. I my test I would like to test that w when I invoke a send method from Connector class a send method from Stompest library will be only once invoked.

My unit test looks like:

from Connector import Connector

import unittest
from mockito import *
import stompest
from stompest.config import StompConfig
from stompest.sync import Stomp


class test_Connector(unittest.TestCase):
    def test_shouldInvokeConnectMethod(self):
        stomp_config = StompConfig(uri="tcp://localhost:61613")
        mock_stomp = mock(Stomp(stomp_config))

        connector = Connector()
        connector.sendMessage()
        verify(mock_stomp, times=1).connect()

When I run test in debug mode I see that method for instance connect() is invoked and method send as well, but as a result of test I get:

Failure
Traceback (most recent call last):
  File "C:\development\systemtest_repo\robot_libraries\test_Connector.py", line 16, in test_shouldInvokeConnectMethod
    verify(mock_stomp, times=1).connect()
  File "C:\Python27\lib\site-packages\mockito\invocation.py", line 111, in __call__
    verification.verify(self, len(matched_invocations))
  File "C:\Python27\lib\site-packages\mockito\verification.py", line 63, in verify
    raise VerificationError("\nWanted but not invoked: %s" % (invocation))
VerificationError: 
Wanted but not invoked: connect()

What I do wrong?

BR

Aucun commentaire:

Enregistrer un commentaire