2012-08-14 2 views
1

스프레드 시트 (4 개 열)를 읽고, 피드를 가져 와서 특정 열 (2 개 열)을 CSV 파일에 쓰는 mox 테스트를 작성하려고합니다. 내가 첫 번째 단계는 목록 피드를 얻을 과거 얻으려고, 내 코드는 다음과 같다 :python mox 테스트를 실행할 때 예상 메서드가 호출되지 않습니다.

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) 

나는이를 실행하면 다음과 같은 에러가 발생

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

어떤 생각 방법을 이 오류를 수정 하시겠습니까?

답변

3

실제로 GetListFeed를 호출 할 함수를 트리거해야합니다. self.mox.ReplayAll()라고 부르는 지점까지는 몹이 재생 모드에 들어가면 볼 수있는 부분을 "녹음"합니다. Mox를 재생 모드에 넣은 후에는 이라고하는 함수를 실제로 호출해야합니다. 귀하의 경우에는 그 부모 기능이 무엇이든간에 testGetFeed 인 것으로 보입니다. 또한

, 당신이 당신의 클래스 정의에 mox.MoxTestBase()를 서브 클래 싱하고 있기 때문에, 당신은 마지막에 self.mox.Verify()를 호출 할 필요가 없습니다 않음 - 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

를 코드를 수정 한 후 나는 계속 그것을 실행할 때 점점 : AttributeError : '목록'개체에 'AndReturn'속성이 없습니다. "AndReturn"은 어떻게 작동합니까? – jwesonga

+0

"AndReturn"은 함수를 스텁 아웃하기 위해 사용중인 mock 객체에서 호출 할 수있는 mox 관련 함수입니다. 아마도 의도 한 모의 객체가 아닌 파이썬리스트에서 직접 호출하고있을 것이다. 또한 "재생"섹션보다는 "기록"섹션에서이 작업을 수행하고 있는지 확인해야합니다. – AndrewS

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() 
관련 문제