Expected methods never called when running python mox test

2.7k views Asked by At

I'm trying to write a mox test that reads a spreadsheet (4 columns), gets the feed and writes specific columns (2 columns) to a CSV file. I'm trying to get past the first step which is get the list feed, my code is as follows:

class SpreadsheetReader(mox.MoxTestBase):

  def setUp(self):
    mox.MoxTestBase.setUp(self)
    self.mock_gclient = self.mox.CreateMock(
                                            gdata.spreadsheet.service.SpreadsheetsService)
    self.mock_spreadsheet_key = 'fake_spreadsheet_key'
    self.mock_worksheet_id = 'default'
    self.test_data = [{'str_col':'col1', 'str_col':'col2', 'str_col':'col13'}]


  def testGetFeed(self):

    self.mock_gclient.GetListFeed(self.mock_spreadsheet_key,
                                  self.mock_worksheet_id).AndReturn(self.test_data)

    self.mox.ReplayAll()
    self.mox.Verify()


  def tearDown(self):
    mox.MoxTestBase.tearDown(self)

When i run this I get the following error:

ExpectedMethodCallsError: Verify: Expected methods never called:
  0.  SpreadsheetsService.GetListFeed('fake_spreadsheet_key', 'default') -> [{'str_col': 'col13'}]

Any idea how to fix this error?

2

There are 2 answers

2
AndrewS On BEST ANSWER

You need to actually trigger the function that would call GetListFeed. Up until the point you call self.mox.ReplayAll(), you are only "recording" what mox should expect to see once it's put into replay mode. After you put mox into replay mode, you need to actually call to whatever function would call GetListFeed. In your case, that appears to be testGetFeed or whatever its parent function is.

Also, because you are subclassing mox.MoxTestBase() in your class definition, you do not need to call self.mox.Verify() at the end — per the docs,

you can make your test case be a subclass of mox.MoxTestBase; this will automatically create a mock object factory in self.mox, and will automatically verify all mock objects and unset stubs at the end of each test.

0
AudioBubble On
self.mox_gclient = self.mox.CreateMock(gdata.spreadsheet.service.SpreadsheetsService)
self.mox_gclient.StubOutWithMock(ActualClass,"method_to_be_tested").AndReturn(retValue)
self.mox_gclient.VerifyAll()