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()