2012-06-19 11 views
90

Sto provando a creare un test unitario per verificare che un oggetto sia stato cancellato.Come posso importare l'eccezione DjNo DoesNotExist?

from django.utils import unittest 
def test_z_Kallie_can_delete_discussion_response(self): 
    ...snip... 
    self._driver.get("http://localhost:8000/questions/3/want-a-discussion") 
    self.assertRaises(Answer.DoesNotExist, Answer.objects.get(body__exact = '<p>User can reply to discussion.</p>')) 

continuo a ricevere l'errore:

DoesNotExist: Answer matching query does not exist. 
+0

Estranei a mia risposta qui sotto, è quella chiamata get() cancellando la risposta in questione? Se è così, questo dovrebbe davvero essere un DELETE, non un GET. –

risposta

104

Non è necessario importarlo - come hai già scritto correttamente, DoesNotExist è una struttura del modello stesso, in questo caso Answer.

Il problema è che si sta chiamando il metodo get - che solleva l'eccezione - prima che venga passato a assertRaises. È necessario separare gli argomenti dal callable, come descritto nella unittest documentation:

self.assertRaises(Answer.DoesNotExist, Answer.objects.get, body__exact='<p>User can reply to discussion.</p>') 

o meglio:

with self.assertRaises(Answer.DoesNotExist): 
    Answer.objects.get(body__exact='<p>User can reply to discussion.</p>') 
+1

Buona risposta, solo il primo dei frammenti sopra sarà preso come sintassi non valida (almeno da Python 2.7)., Dovrebbe essere 'self.assertRaises (Answer.DoesNotExist, Answer.objects.get, body__exact = '

L'utente può rispondere alla discussione:

') '- cioè con gli argomenti di' get' aggiunti come singoli kw args, non all'interno di '()'. –

+1

Augh, ovviamente! Mi sento come Dorothy qui. Stavo cercando in alto e in basso, solo per scoprire che era con me tutto il tempo! –

8

DoesNotExist è sempre una proprietà del modello che non esiste. In questo caso sarebbe Answer.DoesNotExist.

140

È anche possibile importare ObjectDoesNotExist da django.core.exceptions, se si desidera un modello indipendente dal modo generico per intercettare l'eccezione:

from django.core.exceptions import ObjectDoesNotExist 

try: 
    SomeModel.objects.get(pk=1) 
except ObjectDoesNotExist: 
    print 'Does Not Exist!' 
0

Questo è come lo faccio un test del genere.

from foo.models import Answer 

def test_z_Kallie_can_delete_discussion_response(self): 

    ...snip... 

    self._driver.get("http://localhost:8000/questions/3/want-a-discussion") 
    try: 
     answer = Answer.objects.get(body__exact = '<p>User can reply to discussion.</p>'))  
     self.fail("Should not have reached here! Expected no Answer object. Found %s" % answer 
    except Answer.DoesNotExist: 
     pass # all is as expected 
2

Una cosa da guardare fuori per è che il secondo parametro di assertRaisesbisogno a essere un callable - non solo una proprietà. Per esempio, ho avuto difficoltà con questa dichiarazione:

self.assertRaises(AP.DoesNotExist, self.fma.ap) 

ma questo ha funzionato bene:

self.assertRaises(AP.DoesNotExist, lambda: self.fma.ap) 
1
self.assertFalse(Answer.objects.filter(body__exact='<p>User...discussion.</p>').exists()) 
Problemi correlati