mardi 30 juin 2015

Mock image for tests in Django

I want to use mock-image for creating an object in Django.

tests.py:

from django.test import TestCase
from .models import MyModel
from unittest import mock
from django.core.files import File


class MyModelTestCase(TestCase):

    def setUp(self):
        file_mock = mock.Mock(spec=File, name='FileMock')
        file_mock.name = 'test.png'
        MyModel.objects.create(img=file_mock, name='123',)

    def test_creation(self):
        obj =  MyModel.objects.get(name='123')
        self.assertEqual(obj, '1, 123')

But I have an error:

TypeError: 'Mock' does not support the buffer interface

I use spec-argument, how it described in documentation.

Why it does not work and how to fix that?

Thanks!

Aucun commentaire:

Enregistrer un commentaire