Django单元测试 - 如何断言自定义Manager调用UserManager.create_user方法?

时间:2014-12-22 18:10:34

标签: python django unit-testing mocking

我是Python的单元测试和模拟的新手,也是Django的新手。

这是我的模型(简化):

from django.db import models
from django.contrib.auth.models import User


class KangaUserManager(models.Manager):
    def create(self, username, email, password, last_name, first_name, request, registered=True, send_confirmation=True):
        kanga_user = KangaUser()
        kanga_user.user = User.objects.create_user(
            username=username,
            email=email,
            password=password,
            first_name=first_name,
            last_name=last_name
        )
        return kanga_user

class KangaUser(models.Model):
    objects = KangaUserManager()

    user = models.OneToOneField(User)

我想断言调用User.objects.create_user,而不实际调用它。我认为模拟库是我要使用的库,但我没有做任何事情。

我尝试过这样的事情:

def test_create(self):
    with mock.patch('django.contrib.auth.models.UserManager') as usermanager_mock:
        kangauser_manager = KangaUserManager()
        kangauser_manager.create(self.username, self.email, self.password, self.last_name, self.first_name, self.request, self.registered, self.send_confirmation)

        self.assertTrue(usermanager_mock.create_user.called)

那(以及更多我无法记住的事情:D)

def test_create(self):
    with mock.patch('django.contrib.auth.models.User') as user_mock:
        user_mock.objects = mock.MagicMock()
        user_mock.objects.create_user = mock.MagicMock()

        kangauser_manager = KangaUserManager()
        kangauser_manager.create(self.username, self.email, self.password, self.last_name, self.first_name, self.request, self.registered, self.send_confirmation)

        self.assertTrue(user_mock.objects.create_user.called)

但我总是看到该方法未被调用

我做错了什么?

(编辑1:添加了一些导入)

编辑2:

当Alex martelli提出改编时,我偶然发现了另一个错误,

ValueError: Cannot assign "<MagicMock name='User.objects.create_user()' id='55229392'>": "KangaUser.user" must be a "User" instance.

以下是改编的代码:

from django.db import models
from django.contrib.auth import models as auth_model


class KangaUserManager(models.Manager):
    def create(self, username, email, password, last_name, first_name, request, registered=True, send_confirmation=True):
        kanga_user = KangaUser()
        kanga_user.user = auth_model.User.objects.create_user(
            username=username,
            email=email,
            password=password,
            first_name=first_name,
            last_name=last_name
        )
        return kanga_user

class KangaUser(models.Model):
    objects = KangaUserManager()

    user = models.OneToOneField(auth_model.User)

2 个答案:

答案 0 :(得分:1)

使用MagicMock时需要返回一个值。

from unittest.mock import MagicMock

thing = KangaUserManager()
thing.create_user = MagicMock(return_value=True)
self.assertTrue(thing.create_user(**user_kwargs))

答案 1 :(得分:-1)

好的,多亏Alex和Aaron,我得到了我需要的功能,让它像我想的那样工作,谢谢!

所以,这是代码

from django.db import models
from django.contrib.auth import models as auth_model


class KangaUserManager(models.Manager):
    def create(self, username, email, password, last_name, first_name, request, registered=True, send_confirmation=True):
        kanga_user = KangaUser()
        kanga_user.user = auth_model.User.objects.create_user(
            username=username,
            email=email,
            password=password,
            first_name=first_name,
            last_name=last_name
        )
        return kanga_user

class KangaUser(models.Model):
    objects = KangaUserManager()

    user = models.OneToOneField(auth_model.User)

有效的测试:

def test_create(self):
    with mock.patch('django.contrib.auth.models.User') as user_mock:
        user_mock.objects = mock.MagicMock()
        user_mock.objects.create_user = mock.MagicMock()
        user_mock.objects.create_user.return_value = User()

        kangauser_manager = KangaUserManager()
        kangauser_manager.create(self.username, self.email, self.password, self.last_name, self.first_name, self.request, self.registered, self.send_confirmation)

        self.assertTrue(user_mock.objects.create_user.called)