简体   繁体   English

模拟以Django形式调用的函数

[英]Mocking a function called in django form

I have two modules. 我有两个模块。

a.py py

def get_resource(arg1, arg2):
   return Modelobject based on arg1 and arg2 or None

b.py (form) b.py(窗体)

from a import get_resource
class A(forms.Form):
 arg1 = forms.CharField()
 arg2 = forms.CharField()
 def clean(self):
   res = get_resource(arg1, arg2)
   if res is None:
    validationerror
   else:
    cleaned_data.update(res_key=res)

Now I need to mock the get_resource part so that I donot need any database but I couldnot get it to work. 现在,我需要模拟get_resource部分,以便不需要任何数据库,但无法使其正常工作。

Here is what I tried but it doesnot work. 这是我尝试过的方法,但是没有用。 What am I doing wrong ? 我究竟做错了什么 ?

class Test(TestCase):
  def test_form_a(self):
    with patch('b.get_resource') as mock_tool:
      mock_tool.return_value = MagicMock(spec=MusicModel)
      form_data = {'arg1': '1', 'arg2': 'Music'}
      form = A(data=form_data)

Also I tried side_effects with a function 我也尝试了一个函数side_effects

def my_side_effect(*args, **kwargs):
  return value based on arg[0] and arg[1]

mock_tool.side_effect = my_side_effect

Since I am quite novice at mock and testing, Can anyone show me right direction ? 由于我是模拟和测试的新手,所以有人可以告诉我正确的方向吗?

I'm not familiar with django form, so basically I modified your script in a proper testable way. 我对Django表单不熟悉,因此基本上我以一种可测试的方式修改了您的脚本。 It seems the tests are all passed, maybe you omit some cause-the-error codes in the question? 似乎所有测试都通过了,也许您在问题中省略了一些错误原因代码? However the following is a working example for your reference. 但是,以下是一个可供参考的工作示例。

% nosetests test.py test_no_patch_a (test.Test) ... ok test_patch_a (test.Test) ... ok %鼻子测试test.py test_no_patch_a(test.Test)...确定test_patch_a(test.Test)...确定

a.py py

def get_resource(arg1, arg2):
 return arg1

b.py b.py

from a import get_resource
class A(object):
 arg1 = 'arg1'
 arg2 = 'arg2'
 res = None
 def __init__(self, data):
   self.res = get_resource(data['arg1'], data['arg2'])

 def bow(self):
   if self.res is None:
    return 'validationerror'
   else:
    return self.res

test.py test.py

import unittest
from mock import patch
from b import A

class Test(unittest.TestCase):
  def test_patch_a(self):
    with patch('b.get_resource') as mock_tool:
      mock_tool.return_value = 'patched'
      data = {'arg1': '1', 'arg2': 'Music'}
      form = A(data=data)
      self.assertEqual('patched', form.bow())

  def test_no_patch_a(self):
    data = {'arg1': '1', 'arg2': 'Music'}
    form = A(data=data)
    self.assertEqual('1', form.bow())

UPDATE: I guess I found out what the issue is. 更新:我想我发现了问题所在。 Although I have a hard time understanding why this happens it all depends on the imports. 尽管我很难理解为什么会这样,但这完全取决于进口。 Apparently, I need to import the form class inside the patch and define the return value after I instantiate the form so: 显然,我需要在补丁内部导入表单类,并在实例化表单后定义返回值,如下所示:

class Test(TestCase):
  def test_form_a(self):
    with patch('b.get_resource') as mock_tool:
      from b import A
      form_data = {'arg1': '1', 'arg2': 'Music'}
      form = A(data=form_data)
      mock_tool.return_value = MagicMock(spec=MusicModel)

I guess its all due to module loading. 我猜都是由于模块加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM