I'm trying to mock out some xbmc-related classes and methods to use in my unittests.
I have installed xbmc-stubs in order to be able to write test and plugin code, but I'm running into a particular problem with the Player class.
The (relevant) structure of my code is:
- resources
- service.py contains
import xbmc
from resources.player import XBMCPlayer
class Service(object):
def __init__(self):
self.player = XBMCPlayer()
def do_stuff_with_time(self):
print self.player.getTime() + 0.0
- player.py contains
import xbmc
class XBMCPlayer(xbmc.Player)
...
In my tests, I want to mock the getTime() method of the XBMCPlayer class which it inherits from the xbmc module (i.e. it's not explicitly defined in the XBMCPlayer class).
What I've tried to far:
@mock.patch.object("xbmc.Player", "getTime", mock_getTime())
def test_get_time(self):
self.service.player.do_stuff_with_time() # self.service is an instance of service.Service
Raises: AttributeError: xbmc.Player does not have the attribute 'getTime'
@mock.patch.object("resources.player.XBMCPlayer", "getTime", mock_getTime())
def test_get_time(self):
self.service.player.do_stuff_with_time()
Raises: AttributeError: resources.player.XBMCPlayer does not have the attribute 'getTime'
@mock.patch("xbmc.Player", "getTime", mock_getTime())
def test_get_time(self):
self.service.player.do_stuff_with_time()
Raises: TypeError: unsupported operand type(s) for +: 'float' and 'type'
I'm on to something with the last one, because the mocking itself did not raise an error. The TypeError is raised because the xbmc stub ´xbmc.Player.getTime()` returns a type:
def getTime(self):
return float
This indicates that getTime() has not been successfully mocked and that's the stub's method was called instead of the mock function.
Whatever I try, I just can't succeed in patching things the right way (i.e. it's always the stub that's being called).
It makes me think there's something fundamentally wrong with the way I've set things up, or with the way I understand the mock library (which has proven to be very helpful to me in many other instances by the way).
(I'm using python 2.7.6. and mock 1.0.1)
Aucun commentaire:
Enregistrer un commentaire