2011-04-20 43 views
1

我只是在django上啓動一個小應用程序。目前,它的目標僅僅是管理測試人員(即用戶)和團隊。這裏是我的model.py:Django和nosetests:DoesNotExist:<object>匹配查詢不存在

from django.db import models 

class Team(models.Model): 
    name = models.CharField(max_length=200) 
    def __unicode__(self): 
     return self.name 
    def get_testers(self): 
     return self.tester_set.all() 

class Tester(models.Model): 
    team = models.ForeignKey(Team) 
    visa = models.CharField(max_length=3) 
    privileged = models.BooleanField() 
    def __unicode__(self): 
     return self.visa 

我想寫一個測試「get_testers」函數。 這就是:

from models import Team, Tester 
def testTeamGetTesters_test(): 
    t = list(Team.objects.get(id=2L).get_testers()) 

    a = Tester(visa = 'a', privileged = True) 
    b = Tester(visa = 'b', privileged = True) 

    assert(t[0].visa == a.visa and t[0].privileged == a.privileged and t[1].visa == b.visa and t[1].privileged == b.privileged) 

但是當我運行:

$ python manage.py test tmg 

我得到這個錯誤:

nosetests --verbosity 1 tmg 
E 
====================================================================== 
ERROR: tempsite.tmg.tests.testTeamGetTesters_test 
---------------------------------------------------------------------- 
Traceback (most recent call last): 
    File "/usr/lib/pymodules/python2.6/nose/case.py", line 183, in runTest 
    self.test(*self.arg) 
    File "/home/charlie/code/tempsite/../tempsite/tmg/tests.py", line 8, in testTeamGetTesters_test 
    t = list(Team.objects.get(id=2L).get_testers()) 
    File "/usr/lib/pymodules/python2.6/django/db/models/manager.py", line 132, in get 
    return self.get_query_set().get(*args, **kwargs) 
    File "/usr/lib/pymodules/python2.6/django/db/models/query.py", line 341, in get 
    % self.model._meta.object_name) 
DoesNotExist: Team matching query does not exist. 

所以,我寫了幾乎相同的測試,而是直接可運行:

from models import Team, Tester 

t = list(Team.objects.get(id=2L).get_testers()) 

a = Tester(visa = 'a', privileged = True) 
b = Tester(visa = 'b', privileged = True) 

print "%r" % (t[0].visa == a.visa and t[0].privileged == a.privileged and t[1].visa == b.visa and t[1].privileged == b.privileged) 

...和w母雞我運行它:

$ python tests.py 
True 

這是非常混亂。我檢查了數據庫,對象都完全恢復,但我仍然得到這個錯誤...

回答

1

你創建團隊對象在你的測試中有id 2的地方?不要忘記測試從一個空白數據庫開始。