0

我有一个失败的测试:

======================================================================  
    FAIL: test_register_should_create_UserProfile (APP.forum.tests.test_views.UserTestCAse)  
    ----------------------------------------------------------------------  
    Traceback (most recent call last):  
      File "/Users/Bryan/work/app/../app/forum/tests/test_views.py", line 25, in test_register_should_create_UserProfile  
        self.assertEqual(response.status_code, 200)  
    AssertionError: 404 != 200

这是测试:

class UserTestCAse(TestCase):  
  def test_register_should_create_UserProfile(self):  
    from django.test.client import Client  
    c = Client()  
    # I'm skipping account/signin/ because that requires me to visit Google.  
    response = c.post('account/signin/complete/', {'username': 'john', "email":'john@beatles.com', u'bnewaccount': 'Signup'}) 
    # request.POST from pdb() session with breakpoint in register()
    # <QueryDict: {u'username': [u'john'], u'email': [u'john@beatles.com'], u'bnewaccount': [u'Signup']}>

    #How do I inspect what is breaking in the test case?  
    #How do I run the test client in the shell?  
    self.assertEqual(response.status_code, 200)  

    user = User.objects.get(username ='john')  
    self.assertTrue(user.get_profile())  

无论如何我可以看到为什么这个响应没有返回 200?

我尝试在 shell 中使用 TestClient() ,但这不起作用:

In [1]: from django.test.client import Client  

In [2]: c = Client()  

In [3]: response = c.post('account/signin/complete/', {'username': 'john', "email":'john@beatles.com', u'bnewaccount': 'Signup'})  
---------------------------------------------------------------------------  

KeyError: 'tried'  
4

2 回答 2

3

这看起来不对。

user = User.objects.get('username'=='john')  

如果要查询,则必须按照教程中所示的样式编写查询

http://docs.djangoproject.com/en/1.1/topics/db/queries/#topics-db-queries

user = User.objects.get( username = 'john' ) 

例如。

要调试,您可以在命令行中运行。这就是我们所做的。Django 教程显示了所有示例,就好像它们是在命令行中以交互方式键入的一样。

>>> from myapp.models import Client
>>> Client.objects.get( name = 'value' )

等等

您通常不会尝试从命令行运行单元测试。这很难做到,因为单元测试框架为你做了所有的事情。

通常,您只需一次一个地浏览应用程序视图函数语句,以确保您的视图函数能够实际工作。

于 2010-02-23T22:05:19.697 回答
2

如果您收到意外的 404,Django 错误页面会显示错误(假设 DEBUG 为 True)。在这种情况下,您可以只print response.content显示该回溯,并将其剪切并粘贴到浏览器中。

此外,不要忘记您可以使用交互式调试器进入运行测试,pdb就像任何其他形式的运行代码一样,因此您可以动态检查响应。甚至,将断点放在帖子之前,以便您可以单步执行视图。

但是,再看一遍,我怀疑您的 url 不匹配,因为您缺少首字母\.

于 2010-02-23T23:09:16.393 回答