2014-10-27 68 views
1

我想模擬sendEmails()方法,並想測試第二個參數是否用「[email protected]」電子郵件地址調用。如何測試模擬方法的第二個參數?

@mock.patch('apps.dbank.management.commands.optin_invites.OptinBase.sendEmails') 
def test_x_send_emails(self, send_emails_mock): 
    oi = OptinInvitesX() 
    oi.compute(True, "[email protected]") 
    self.assertTrue(send_emails_mock.assert_called_with(???, test_email_address="[email protected]")) 

我可以利用assert_called_with但我不關心這個測試情況下,第一個參數。有沒有辦法說第一個參數接受任何東西?

回答

0

我無法弄清楚當我試圖做同樣的事情時該怎麼做。

這是一個有點哈克解決方法:

from mock import MagicMock 

class YesMan(object): 
    def __eq__(self, other): 
     return True 

anything = YesMan() 

mock = MagicMock() 
mock(x=123, y=456) 
mock.assert_called_once_with(x=anything, y=456) 

另一種方法是看屬性

>>> mock.call_args 
call(y=456, x=123) 

,並建立該對象的斷言。

+0

另請參閱'mock.call_args_list'(list),'mock.call_count'(int)和'mock.called'(bool)更復雜的案例 – wim 2014-10-27 17:22:31

+0

是的,它的工作原理類似'self.assertEquals(args [0 ] [1] ['test_email_address'],「[email protected]」)。它不漂亮。我希望有更好的辦法。如果沒有更好的解決方案,我會將問題留待一天,我會接受。非常感謝 – Houman 2014-10-27 18:02:58

相關問題