2014-09-10 43 views
1

我一直在使用python mockito https://code.google.com/p/mockito-python/來測試我的代碼。python mockito自變量捕獲器

到目前爲止蟒蛇似乎的Mockito只提供2匹配器:包括()和任何()https://code.google.com/p/mockito-python/wiki/Matchers

我想知道我怎麼能寫一些代碼,這樣我可以捕捉整個論點。

因此,舉例來說,如果我的代碼是

deleteSqlStatement = "DELETE from %s WHERE lower(z_id)=lower('%s') and y_id=%s" \ 
         % (self.SOME_TABLE, zId, yId) 
cursor.execute(deleteSqlStatement) 

目前,所有我可以驗證能做的就是

verify(self.cursor_mock, times=1).execute(contains("DELETE")) 

這將是巨大的,如果我能捕捉傳遞到執行的整個參數串。

對此提出建議?

+0

有你使用只爲這一個Python模擬,這將只是一個模仿很容易認爲 – Thomasleveil 2014-09-29 16:10:39

回答

1

我想你可以實現你自己的[Matcher]來捕捉這個參數。

class Captor(Matcher): 

    def matches(self, arg): 
    self.value = arg 
    return True 

    def getValue(self): 
    return self.value 

,然後在您的測試中使用它:

captor = Captor() 
verify(self.cursor_mock, times=1).execute(captor) 
self.assertEqual("expected query", captor.getValue())