我正在尝试测试boto.exception.EC2ResponsError的assertRaises,但在_mock_call中获取“TypeError:init()至少需要3个参数(1给定)”.
@mock_ec2@patch.object(Ec2Region,'connect')def test_ec2_get_raises(self,mock_connect): conn = boto.connect_ec2() mock_connect.return_value = conn reservation = conn.run_instances('ami-1234abcd') instance = reservation.instances[0] Ec2Instance.objects.create(region=self.region,account=self.account,instance_ID=instance.ID) mock_connect.sIDe_effect = boto.exception.EC2ResponseError self.assertRaises( boto.exception.EC2ResponseError,Ec2Instance.ec2_get,self.account,self.region)
我得到的错误是这样的:
======================================================================ERROR: test_ec2_get_raises (session_ec2.tests.test_instance.Ec2InstanceTest)----------------------------------------------------------------------Traceback (most recent call last): file "/Users/bschott/.virtualenvs/session-ec2/lib/python2.7/site-packages/moto/core/models.py",line 70,in wrapper result = func(*args,**kwargs) file "/Users/bschott/.virtualenvs/session-ec2/lib/python2.7/site-packages/mock.py",line 1201,in patched return func(*args,**keywargs) file "/Users/bschott/Source/session-ec2/session_ec2/tests/test_instance.py",line 84,in test_ec2_get_raises Ec2Instance.ec2_get,self.region) file "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/case.py",line 475,in assertRaises callableObj(*args,**kwargs) file "/Users/bschott/Source/session-ec2/session_ec2/models/instance.py",line 110,in ec2_get connection = region.connect(account) file "/Users/bschott/.virtualenvs/session-ec2/lib/python2.7/site-packages/mock.py",line 955,in __call__ return _mock_self._mock_call(*args,line 1010,in _mock_call raise effectTypeError: __init__() takes at least 3 arguments (1 given)解决方法 如 Calling部分所述,您可以在sIDe_effect初始化中使用实例或类.此外,您可以使用可调用来提升您所需的异常.
当class用于定义sIDe_effect并且所需的异常没有简单的空构造函数时,您将得到一个类似于您的异常,因为mock框架不知道如何构建该异常.
在你的情况下,你可以使用类似的东西
mock_connect.sIDe_effect = boto.exception.EC2ResponseError(400,"My reason","my body")总结
以上是内存溢出为你收集整理的如何将异常参数传递给python unittest mock副作用?全部内容,希望文章能够帮你解决如何将异常参数传递给python unittest mock副作用?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)