krystosan 0 Junior Poster

I have a function in which i want to mock os.listdir that returns directory content to variable dirContent

def setUp(self):
    self._filePaths = ["/test/file/path"]
    self.mox = mox.Mox()

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))

and testing method i have tried so far is

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()

which gives error

- Stub for <built-in function listdir>.__call__('/test/file/path') -> None
?                                                                      ^ ^

+ Stub for <built-in function listdir>.__call__(['/test/file/path']) -> ['file1.jpg', 'file2.PNG', 'file3.png']
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.