Django:模拟模型的创建方法

时间:2019-03-11 07:05:59

标签: django mocking python-unittest

我有一个名为Job的模型。我创建了一个文件services.py,将在其中放置一些方法和业务逻辑。 我正在尝试使用Mock测试它们。 问题是,如果我要模拟Job.objects.create,它会给我一个错误。

services.py

from .models import Job
class CreateJob:                                                    

    def __init__(                                                   
        self,                                                       
        title,                                                      
        email,                                                      
    ):                                                              
        self._title = title                                         
        self._email = email                                         

    def execute(self):                                              
        # Create dictionary with the keys without the first _ in the name
        params = {k[1:] if k[0] == '_' else k:v for k,v in self.__dict__.items() if v is not None}
        job = Job.objects.create(**params)
        return job

这是mi测试用例,运行正常

class TestExecute(TestCase):                     
    def setUp(self):                             
        self._use_case = CreateJob(              
            title='How to test a job creation',  
            email='john.smith@example.com',      
        )                                        

    def test_return_job_type(self):              
        result = self._use_case.execute()        
        assert isinstance(result, Job)           

但是如果我想修补创建方法,那么我就不会像这样打数据库

def create_job(params):                                     
    return Job(**params)                                    

@patch.object(Job.objects, 'create', side_effect=create_job)
class TestExecute(TestCase):                                
    def setUp(self):                                        
        self._use_case = CreateJob(                         
            title='How to test a job creation',             
            email='john.smith@example.com',                 
        )                                                   

    def test_return_job_type(self,mock_create):             
        result = self._use_case.execute()                   
        assert isinstance(result, Job)

我遇到以下错误: TypeError:create_job()得到了意外的关键字参数'title'

这是跟踪:

services.py:100: in execute
job = Job.objects.create(**params)
/usr/lib/python3.6/unittest/mock.py:939: in __call__
return _mock_self._mock_call(*args, **kwargs)
def _mock_call(_mock_self, *args, **kwargs):
    self = _mock_self
    self.called = True
    self.call_count += 1
    _new_name = self._mock_new_name
    _new_parent = self._mock_new_parent

    _call = _Call((args, kwargs), two=True)
    self.call_args = _call
    self.call_args_list.append(_call)
    self.mock_calls.append(_Call(('', args, kwargs)))

    seen = set()
    skip_next_dot = _new_name == '()'
    do_method_calls = self._mock_parent is not None
    name = self._mock_name
    while _new_parent is not None:
        this_mock_call = _Call((_new_name, args, kwargs))
        if _new_parent._mock_new_name:
            dot = '.'
            if skip_next_dot:
                dot = ''

            skip_next_dot = False
            if _new_parent._mock_new_name == '()':
                skip_next_dot = True

            _new_name = _new_parent._mock_new_name + dot + _new_name

        if do_method_calls:
            if _new_name == name:
                this_method_call = this_mock_call
            else:
                this_method_call = _Call((name, args, kwargs))
            _new_parent.method_calls.append(this_method_call)

            do_method_calls = _new_parent._mock_parent is not None
            if do_method_calls:
                name = _new_parent._mock_name + '.' + name

        _new_parent.mock_calls.append(this_mock_call)
        _new_parent = _new_parent._mock_new_parent

        # use ids here so as not to call __hash__ on the mocks
        _new_parent_id = id(_new_parent)
        if _new_parent_id in seen:
            break
        seen.add(_new_parent_id)

    ret_val = DEFAULT
    effect = self.side_effect
    if effect is not None:
        if _is_exception(effect):
            raise effect

        if not _callable(effect):
            result = next(effect)
            if _is_exception(result):
                raise result
            if result is DEFAULT:
                result = self.return_value
            return result
           ret_val = effect(*args, **kwargs)
E           TypeError: create_job() got an unexpected keyword argument 'title'

2 个答案:

答案 0 :(得分:1)

您必须为create_job方法定义可接受的参数。目前,它仅接受1个称为params的参数。您应该这样写:

def create_job(**params):                                     
    return Job(**params)

Python的常规用法是使用*args**kwargs,尽管不需要使用它们。

答案 1 :(得分:0)

Silly错误,修补create方法的函数应该是这样

def create_job(**params):                                     
    return Job(**params) 

请注意函数定义的参数上的两个**