RaulSamada
RaulSamada

Reputation: 1

Why the test is not matching the path

I'm trying to run some test in my Django app. But for some reason there is not matching my urlpattern

test.py

import pytest
from django.test import TestCase
from django.urls import reverse
from course.models import Course

@pytest.mark.django_db
class CourseViewTest(TestCase):

    @classmethod
    def setUpTestData(cls):
        cls.course1 = Course.objects.create(title="Curso 1", course_description="Descripción del curso")

    def test_get_all_courses(self):
        url = reverse('courses-list')
        response = self.client.get(url)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.json()), 1)
        self.assertEqual(response.json()[0]['title'], 'Curso 1')

    def test_course_creation(self):
        assert Course.objects.count() == 1
        assert Course.objects.get(title="Curso 1").course_description == "Descripción del curso"

urls.py

from django.urls import path
from .views import *

app_name = 'course'

urlpatterns = [
    path('courses/', CourseView.get_all, name="courses-list"),
]

I am receiving this error

Found 2 test(s).
Creating test database for alias 'default'...
System check identified no issues (0 silenced).
.E
======================================================================
ERROR: test_get_all_courses (course.tests.CourseViewTest.test_get_all_courses)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\Proyectos\Harvard\RaulSamada\coursie\course\tests.py", line 15, in test_get_all_courses
    url = reverse('courses-list')
          ^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Proyectos\Harvard\RaulSamada\env\Lib\site-packages\django\urls\base.py", line 88, in reverse
    return resolver._reverse_with_prefix(view, prefix, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Proyectos\Harvard\RaulSamada\env\Lib\site-packages\django\urls\resolvers.py", line 831, in _reverse_with_prefix
    raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'courses-list' not found. 'courses-list' is not a valid view function or pattern name.

----------------------------------------------------------------------
Ran 2 tests in 0.011s

FAILED (errors=1)
Destroying test database for alias 'default'...

I've tried to change the url's name, clear cache but still having the same problem

Upvotes: 0

Views: 13

Answers (0)

Related Questions