简体   繁体   中英

python how to mock 3rd party library response

I'm using a 3rd party library "mailjet" to send email. Here's the doc: https://dev.mailjet.com/email/guides/getting-started/#prerequisites

This is the method in send_email.py that I want to test:

from mailjet_rest import Client

def send_email(email_content):
  client = Client(auth=(api_key, api_secret))
  response = client.send.create(data=email_content)
  if response.status_code != 200:
    // raise the error
  else:
    print(response.status_code)
    return response.status_code
...

I wanted to mock the status code. This is what I tried in test_send_email.py :

from unittest.mock import MagicMock, patch
import unittest
import send_email

class TestClass(unittest.TestCase):
    @patch("send_email.Client")
    def test_send_email(self, _mock):
        email_content = {...}
        _mock.return_value.status_code = 200
        response = send_email(email_content)
        // assert response == 200

When I run the test, it didn't print out the status code, it prints:

<MagicMock name='Client().send.create().status_code' id='140640470579328'>

How can I mock the status_code in the test? Thanks in advance!!!!!

You are putting the attribute in the wrong place.

You mock the Client, which is correct, and put something in its return_value, which is also good.

However, the line

response = client.send.create(data=email_content)

applies on the return_value of your mock (client), the send attribute and its create method.

So what you need is to set the return_value.status_code of this create method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM