0

I'm not sure what I'm doing wrong. I'm trying to follow this example: https://docs.djangoproject.com/en/1.6/topics/testing/advanced/#module-django.test.client

I've created my test and the return is odd.

tests.py:

from django.contrib.auth.models import User
from django.test import TestCase
from django.test.client import RequestFactory
from project_name.app_name.views import ViewName

class UrlsViewsTest(TestCase):
    def setUp(self):
        # every test needs access to the request RequestFactory
        self.factory = RequestFactory()
        self.user = User.objects.create_user(username='dave', email='dave@mail.com', password='top_secret')

    def tearDown(self):
        # Delete those objects that are saved in setup
        self.user.delete()

    def test_view_name(self):
        #Create an instance of a GET request
        request = self.factory.get('/app/')

        # Recall that middleware are not suported. You can simulate a
        # logged-in user by setting request.user manually.
        request.user = self.user

        # Test ViewName() as if it were deployed at /app/
        response = ViewName(request)
        self.assertEqual(response.status_code, 200)

Results:

Creating test database for alias 'default'...
E
======================================================================
ERROR: test_view_name (project_name.app_name.tests.UrlsViewsTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/dave/sandbox/project_name/project_name/app_name/tests.py", line 25, in test_view_name
    response = ViewName(request)
TypeError: __init__() takes exactly 1 argument (2 given)

----------------------------------------------------------------------
Ran 1 test in 0.168s

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

I'm not able to figure out what the following means:

TypeError: __init__() takes exactly 1 argument (2 given)

How do I sort out what that means and how do I fix it?

I've been looking in the Django Google Groups and here on SO. I'm not seeing examples.

Dave Merwin
  • 1,382
  • 2
  • 22
  • 44
  • Something wrong with the call `response = ViewName(request)` Is that conflicting with a classname ? – karthikr Feb 10 '14 at 19:03
  • What is ViewName? Is it a function it a class based view? And why don't you want to call your view with the test client? – Daniel Roseman Feb 10 '14 at 19:14
  • 1
    Try `ViewName.as_view()(request)` – sneawo Feb 10 '14 at 19:52
  • @DanielRoseman ViewName is a class based view. I'm not sure I understand your second question. – Dave Merwin Feb 10 '14 at 21:13
  • @sneawo the response is now `as_view() takes exactly 1 argument (2 given)` – Dave Merwin Feb 10 '14 at 21:14
  • @karthikr I'm not sure i understand. It is a class name. Class based view. – Dave Merwin Feb 10 '14 at 21:15
  • @DaveMerwin you didn't pay attention to sneawo's exact syntax. There are two sets of parens there, the first one is empty. And as I said, the usual way of testing views is to use the test client, as [fully explained in the documentation](https://docs.djangoproject.com/en/1.6/topics/testing/tools/#module-django.test.client). – Daniel Roseman Feb 10 '14 at 21:24
  • Thanks all. @DanielRoseman, your right I missed the (). When I added those all was well. I'm a little confused, I thought that I was using the test client. Forgive my ignorance if I'm doing this all wrong. – Dave Merwin Feb 10 '14 at 21:32
  • Ok. Now I se why to use the Test Client and that I was not, in fact, using the Test Client. Thanks @DanielRoseman – Dave Merwin Feb 10 '14 at 22:14

1 Answers1

0

You don't need to delete objects in teardown, the test database will reset itself for each test definition in a TestCase class. The teardown is only needed for things like mock and mox that define new code.

Here's a summary of the message thread so this question can be logged as answered:

Solution1:

response = ViewName.as_view()(request)

Solution2:

# ignore importing ViewName and RequestFactory
response = self.client.login_as(user=self.user)
response = self.client.get('/app/')

Solution3: Direct unit testing only the functions you've written

self.view = ViewName()
output = self.view.new_function(input)
self.assertEqual(output, expected)
Renoc
  • 419
  • 4
  • 13