mocking builtin os.listdir

27 views
Skip to first unread message

Sanjeev Kumar

unread,
Nov 23, 2013, 12:34:18 AM11/23/13
to mox-d...@googlegroups.com
I have this method i want to test

def imageFilePaths(paths):
    imagesWithPath = []
    for _path in paths:
        try:
            dirContent = os.listdir(_path)
        except OSError:
            raise OSError("Provided path '%s' doesn't exists." % _path)

        for each in dirContent:
            selFile = os.path.join(_path, each)
            if os.path.isfile(selFile) and isExtensionSupported(selFile):
                imagesWithPath.append(selFile)
    return list(set(imagesWithPath))
But then i needed to have something in discontent, so using mox i tried this:
def setUp(self):
    self._filePaths = ["/test/file/path"]
    self.mox = mox.Mox()

def test_imageFilePaths(self):
    filePaths = self._filePaths[0]
    self.mox.StubOutWithMock(os,'listdir')
    dirContent = os.listdir(filePaths).AndReturn(['file1.jpg','file2.PNG','file3.png'])

    self.mox.ReplayAll()

    utils.imageFilePaths(filePaths)
    self.mox.VerifyAll()
 but i get this error UnexpectedMethodCallError: Unexpected method call. unexpected:- expected:+ - Stub for <built-in function listdir>.__call__('/test/file/path') -> None + Stub for <built-in function listdir>.__call__(['/test/file/path', 'test2/file/path']) -> ['file1.jpg', 'file2.PNG', 'file3.png']
Reply all
Reply to author
Forward
0 new messages