How to test Django view that have a request to API?
I Have a view post that gets post by its id.
In the test, I would like to create a new post and then check via API, if the post is available.
The problem that I have is that post is being added to the test database but when executing the GET request it makes a request to the development database.
views.py
def post(request: HttpRequest, id: str):
context = {"form": PostForm}
url = env("BASE_URL")
if id:
post = requests.get(f"{url}/api/v1/post/{id}").json()
context["post"] = post
return render(request, "post.html", context)
test_view.py
class BlogPageTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.user = UserProfile.objects.create(
name="TestUser", email="test@mail.com", password="pass"
)
def test_post_render(self):
client = APIClient()
client.force_authenticate(user=self.user)
post = Post.objects.create(
title="Test post",
body="Test post data",
user=self.user,
)
post.save()
response = client.get(reverse("post", kwargs={"id": str(post.id)}))