如何理解python minimock中的样本?

时间:2010-08-04 13:38:23

标签: python mocking

如何理解这一行?

>>> smtplib.SMTP.mock_returns =  Mock('smtp_connection')? 

什么是smtp_connection?它 似乎我可以将其修改为任何名称。

以下是来自minimock

Here's an example of something we might test, a simple email sender::                                                        

>>> import smtplib                                                                                                       
>>> def send_email(from_addr, to_addr, subject, body):                                                                   
...     conn = smtplib.SMTP('localhost')                                                                                 
...     msg = 'To: %s\nFrom: %s\nSubject: %s\n\n%s' % (                                                                  
...         to_addr, from_addr, subject, body)                                                                           
...     conn.sendmail(from_addr, [to_addr], msg)                                                                         
...     conn.quit()                                                                                                      

现在我们要创建一个模拟smtplib.SMTP对象。我们必须要 将我们的模拟注入smtplib模块::

>>> smtplib.SMTP = Mock('smtplib.SMTP')                                                                                  
>>> smtplib.SMTP.mock_returns = Mock('smtp_connection')                                                                  

现在我们进行测试::

>>> send_email('ianb@colorstudy.com', 'joe@example.com',                                                                 
...            'Hi there!', 'How is it going?')                                                                          
Called smtplib.SMTP('localhost')                                                                                         
Called smtp_connection.sendmail(                                                                                         
    'ianb@colorstudy.com',                                                                                               
    ['joe@example.com'],                                                                                                 
    'To: joe@example.com\nFrom: ianb@colorstudy.com\nSubject: Hi there!\n\nHow is it going?')                            
Called smtp_connection.quit()

1 个答案:

答案 0 :(得分:2)

如果您阅读rest of the docs,则会看到以下内容:

  

模拟对象有几个属性,   你可以在什么时候设置   实例化对象。避免   名称冲突,所有属性   以mock_开头,而   构造函数参数没有。

     

name:       打印输出消息时使用的对象名称。在示例中   关于它是'smtplib.SMTP'。

这是连接的名称,例如,在:

Called smtp_connection.sendmail(                                                                                         
    'ianb@colorstudy.com',                                                                                               
    ['joe@example.com'],                                                                                                 
    'To: joe@example.com\nFrom: ianb@colorstudy.com\nSubject: Hi there!\n\nHow is it going?')                            
Called smtp_connection.quit()