I have a dummy module in which A class is used to filter the list containing only string items and then a filter based on starting character is returned using filter list method.
import sys
import ast
from PyQt4 import QtCore,QtGui
class ItemNotString(Exception):
pass
class FilterList(object):
""" docstring for FilterList
"""
def __init__(self, filterby):
super(FilterList, self).__init__()
self._filter = filterby
def checkListItem(self, lst):
for index, item in enumerate(lst):
if type(item) == str:
continue
else:
raise ItemNotString("%i item '%s' is not of type string" % (index+1, item))
return self.filterList(lst)
def filterList(self, lst):
filteredList = []
for eachItem in lst:
if eachItem.startswith(self._filter):
filteredList.append(eachItem)
return filteredList
if __name__ == "__main__":
try:
filterby = sys.argv[2]
except IndexError:
filterby = 'H'
flObj = FilterList(filterby)
print flObj.checkListItem(ast.literal_eval(sys.argv[1]))
but the above module filterList.py tested method filterList() using mox gives error
import unittest
import filterList
import mox
class TestFilterList(unittest.TestCase):
""" docstring for TestFilterList
"""
def setUp(self):
self._filterby = 'B'
self.flObj = filterList.FilterList(self._filterby)
self.mox = mox.Mox()
def tearDown(self):
self.mox.UnsetStubs()
def test_checkListItem(self):
self.assertRaises(filterList.ItemNotString, self.flObj.checkListItem, ['hello', 'Boy', 1])
def test_checkListItem_True(self):
self.assertTrue(self.flObj.checkListItem(['Boy','Bar','Home']))
def test_filterList(self):
self.assertEquals(['Boy', 'Bar'], self.flObj.filterList(['Boy','Bar','Home']))
def test_usingMox(self):
self.mox.StubOutWithMock(self.flObj,'checkListItem')
mockLst = self.flObj.checkListItem().AndReturn(['Bar'])
self.mox.ReplayAll()
filteredList = self.flObj.filterList(mockLst)
self.assertEquals(['Bar'], filteredList)
self.mox.VerifyAll()
if __name__ == '__main__':
unittest.main()
And error I get is
...F
======================================================================
FAIL: test_usingMox (__main__.TestFilterList)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_filterList.py", line 33, in test_usingMox
self.mox.VerifyAll()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mox.py", line 284, in VerifyAll
mock_obj._Verify()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/mox.py", line 504, in _Verify
raise ExpectedMethodCallsError(self._expected_calls_queue)
ExpectedMethodCallsError: Verify: Expected methods never called:
0. Stub for <bound method FilterList.checkListItem of <filterList.FilterList object at 0x106fd4250>>.__call__() -> ['Bar']
----------------------------------------------------------------------
Ran 4 tests in 0.001s
FAILED (failures=1)
please help me out clear the concept of testing using mox.