I have an endpoint on my localhost to get time from different time zones. I type the time zone code in the URL and it shows the time for that time zone. This program uses an external API to get the time. I need to mock this call to the external API and return a hard coded value.
I was following this tutorial from Here
With the program to be mocked as:
import requests
class Blog:
def __init__(self, name):
self.name = name
def posts(self):
response = requests.get("https://jsonplaceholder.typicode.com/posts")
return response.json()
def __repr__(self):
return '<Blog: {}>'.format(self.name)
And the test code as:
from unittest import TestCase
from unittest.mock import patch, Mock
class TestBlog(TestCase):
@patch('main.Blog')
def test_blog_posts(self, MockBlog):
blog = MockBlog()
blog.posts.return_value = [
{
'userId': 1,
'id': 1,
'title': 'Test Title',
'body': 'Far out in the uncharted backwaters of the unfashionable end of the western spiral arm of the Galaxy\ lies a small unregarded yellow sun.'
}
]
response = blog.posts()
self.assertIsNotNone(response)
self.assertIsInstance(response[0], dict)
Now my question is does this example actually mock the call the external source, which in this case is the JsonPlaceholder. And does this test code hard code the response for the original program?
Sorry if this a simple question, I am struggling to find an example I can actually understand, the python docs are way over my head.