Django测试DoesNotExist

时间:2015-09-23 05:09:15

标签: python django django-testing

我正在尝试在视图中测试撤销功能:

@login_required
def revoke(request, id):
    """
    Delete access token
    """
    obj = Hiren.objects.get(pk=id)
    obj.delete()
    return redirect('/hiren')

这是我的测试代码抛出错误:

class TestRevokeView(TestCase):

    def setUp(self):
        User.objects.create_superuser(
            username='admin', password='admin', email='admin@admin.lol')
        item = Hiren()
        item.access_token = "bla bla"
        item.authorized = True
        item.save()

    def test_logged_in_user_can_delete_object(self):
        self.client.login(username='admin', password='admin')
        count = Hiren.objects.all().count()
        self.assertEqual(count, 1)
        response = self.client.get('/revoke/1/', follow=True)
        self.assertRedirects(response, '/hiren/')

以下是错误代码:

Traceback (most recent call last):
  File ".....tests.py", line 109, in test_logged_in_user_can_delete_object
    response = self.client.get('/revoke/1/', follow=True)
 ....................
  self.model._meta.object_name
github.models.DoesNotExist: Hiren matching query does not exist.

所以我的问题是我在这里缺少的。

2 个答案:

答案 0 :(得分:3)

您需要确保在int dim[3] = { 5,5,25 }; Mat A(3, dim, CV_32FC1, Scalar::all(0)); for (int m = 0; m < 5; m++) { for (int n = 0; n < 5; n++) { for (int a = 0; a < 25; a++) // no of channels { cout << A.at<cv::Vec3f>(m,n)[a] << endl; } } } 方法中创建的/** @test **/ public function add_this() { $this->storeInput('company[0][name]' , 'New Company Here On Nepal' ,true); } public function storeInput($element, $text, $force = false) { if ($force) { $this->inputs[$element] = $text; return $this; } else { return parent::storeInput($element, $text); } } 个实例function openCSV() { //Buscamos el archivo var files = DriveApp.getFilesByName("ts.csv"); //Cada nombre de archivo es unico, por eso next() lo devuelve. var file = files.next(); fileID = file.getId(); //Abrimos la spreadsheet, seleccionamos la hoja var spreadsheet = SpreadsheetApp.openById(fileID); var sheet = spreadsheet.getSheets()[0]; //Seleccionamos el rango var range = sheet.getRange("A1:F1"); values = range.getValues(); //Guardamos un log del rango //Logger.log(values); //Seleccionamos la hoja de destino, que es la activeSheet var ss = SpreadsheetApp.getActiveSpreadsheet(); var SSsheet = ss.getSheets()[0]; //Seleccionamos el mismo rango y le asignamos los valores var ssRange = SSsheet.getRange("A1:F1"); ssRange.setValues(values); } 等于Hiren

为避免这种情况,请将setUp id实例设置为类变量,然后在测试方法中使用它。

ID

答案 1 :(得分:2)

很明显,pkHiren不是1。

class TestRevokeView(TestCase):

    def setUp(self):
        User.objects.create_superuser(
            username='admin', password='admin', email='admin@admin.lol')
        self.item = Hiren()
        self.item.access_token = "bla bla"
        self.item.authorized = True
        self.item.save()

    def test_logged_in_user_can_delete_object(self):
        self.client.login(username='admin', password='admin')
        count = Hiren.objects.all().count()
        self.assertEqual(count, 1)
        response = self.client.get('/revoke/{0}/'.format(self.item.pk), follow=True)
        self.assertRedirects(response, '/hiren/')