tags:

views:

24

answers:

1

How to understand this line?

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

What is smtp_connection? It seems I can modify it to any name.

following is from 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()                                                                                                      

Now we want to make a mock smtplib.SMTP object. We'll have to
inject our mock into the smtplib module::

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

Now we do the test::

>>> send_email('[email protected]', '[email protected]',                                                                 
...            'Hi there!', 'How is it going?')                                                                          
Called smtplib.SMTP('localhost')                                                                                         
Called smtp_connection.sendmail(                                                                                         
    '[email protected]',                                                                                               
    ['[email protected]'],                                                                                                 
    'To: [email protected]\nFrom: [email protected]\nSubject: Hi there!\n\nHow is it going?')                            
Called smtp_connection.quit()
+1  A: 

If you read the rest of the docs you'll see the following:

Mock objects have several attributes, all of which you can set when instantiating the object. To avoid name collision, all the attributes start with mock_, while the constructor arguments don't.

name: The name of the object, used when printing out messages. In the example about it was 'smtplib.SMTP'.

It's the name of the connection, used e.g. in:

Called smtp_connection.sendmail(                                                                                         
    '[email protected]',                                                                                               
    ['[email protected]'],                                                                                                 
    'To: [email protected]\nFrom: [email protected]\nSubject: Hi there!\n\nHow is it going?')                            
Called smtp_connection.quit()
katrielalex
What is the type of smtp_connection? really confused.
chenge
It's a string. Just a name for the test to use, so that you can identify it when mock methods are called. If you changed it to `spam`, the above code would contain e.g. `Called spam.sendmail( ... )`.
katrielalex