2

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?

jwesonga
  • 4,249
  • 16
  • 56
  • 83

2 Answers2

5

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.

AndrewS
  • 1,395
  • 11
  • 23
  • After fixing the code when I run it I keep getting: AttributeError: 'list' object has no attribute 'AndReturn' How does "AndReturn" work? – jwesonga Oct 27 '12 at 12:38
  • "AndReturn" is a mox-specific function that can be called on a mock object you are using to stub out a function. You are probably calling it on a Python list directly and not on the mock object you intended. You should also confirm that you are doing this in your "record" section, rather than your "replay" section. – AndrewS Oct 28 '12 at 16:26
1
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()