HomeAlone
HomeAlone

Reputation: 183

Test in Django, reverse with no arguments not found

I want to test two views in Django:

class TestSubstitute(TestCase):
    def test_Substitute(self):
        url = reverse('finder:substitute')
        url_with_param = "{}/{}/".format(url, 1)
        self.response = self.client.post(url_with_param)
        self.assertEqual(self.response.status_code, 200)


class TestDetails(TestCase):
    def test_Details(self):
        url = reverse('finder:detail')
        url_with_param = "{}/{}".format(url, 1)
        self.response = self.client.post(url_with_param)
        self.assertEqual(self.response.status_code, 200)

But whenever I ran those tests, I get:

django.urls.exceptions.NoReverseMatch: Reverse for 'detail' with no arguments not found. 1 pattern(s) tried: ['finder/(?P<product_id>[0-9]+)/$']


django.urls.exceptions.NoReverseMatch: Reverse for 'substitute' with no arguments not found. 1 pattern(s) tried: ['finder/substitute/(?P<product_id>[0-9]+)/$']

My URLS:

app_name = 'finder'
path('<int:product_id>/', views.detail, name='detail'),
path('substitute/<int:product_id>/', views.substitute, name='substitute'),

I've tried to pass '1' and 1 to my url but with no luck. I've made a setup with temporary products with their id.

Upvotes: 2

Views: 1621

Answers (1)

ImustAdmit
ImustAdmit

Reputation: 507

You need to pass id in arguments.

class TestSubstitute(TestCase):
    def test_Substitute(self):
        url = reverse('finder:substitute', args=['1'])
        self.response = self.client.get(url)
        self.assertEqual(self.response.status_code, 200)

Upvotes: 1

Related Questions