dimanche 3 avril 2016

Issues with get_absolute_url in Django Tests

I've written a django model that has a get_absolute_url method, with unit tests to make sure everything is kosher. The get_absolute_url tests pass no problem. The get_absolute_url method is written as so

def get_absolute_url(self):
    return reverse("scheduling:klass", args=[self.pk])

This is the url routing to give context about how the urls flow.

#main.urls
urlpatterns = [
    ...
    url(r'^scheduling/', include('scheduling.urls', namespace="scheduling")),
    ...
]

#scheduling.urls
urlpatterns = [
    url(r'add-class/$', views.add_klass, name="add_klass"),
    url(r'class/(?P<pk>\d+)/$', views.klass, name="klass"),
]

I'm testing the klass view (it's basically just a klass detail view) as follows

def test_klass_detail_view_template_renders(self):
    print(self.klass.pk)
    response = self.client.get(reverse(self.klass))
    self.assertTemplateUsed(response, "scheduling/klass.html")

The test fails with the following stacktrace.

======================================================================
ERROR: test_klass_detail_template_renders (scheduling.tests.test_views.KlassViewTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/me/Documents/Development/myproject/src/scheduling/tests/test_views.py", line 57, in test_klass_detail_template_renders
    response = self.client.get(reverse(self.klass))
  File "/Users/me/.virtualenvs/myproject/lib/python3.5/site-packages/django/core/urlresolvers.py", line 600, in reverse
    return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
  File "/Users/me/.virtualenvs/myproject/lib/python3.5/site-packages/django/core/urlresolvers.py", line 508, in _reverse_with_prefix
    (lookup_view_s, args, kwargs, len(patterns), patterns))
django.core.urlresolvers.NoReverseMatch: Reverse for 'Test Klass' with arguments '()' and keyword arguments '{}' not found. 0 pattern(s) tried: []

It implies that that get_absolute_url is passing the pk value of the klass model to the url pattern but it works when I test it manually and it works in literally every other situation.

This...is...a...head scratcher. Any help is appreciated.

Aucun commentaire:

Enregistrer un commentaire