我正在尝试使用Mock对一些代码进行单元测试。我想提出一个异常,并测试异常是否被捕获并在重新引发之前调用另一个函数
except exception as e:
car.create_log(car_details)
raise e
单元测试:
car = Car()
car.registrations.update = Mock()
car.registrations.update.side_effect = RegistrationError()
car.create_log = Mock()
car.register_car('123123')
car.create_log.assert_called_once()
self.assertRaises(RegistrationError)
我可以确认该方法引发错误,但是无法在重新引发该错误之前测试方法create_log
是否已被调用。
答案 0 :(得分:0)
这就是您应该使用- hosts: localhost
gather_facts: false
tasks:
- name: List the file path which doesn't contains the content
find:
paths: /mrproject/env/
recurse: yes
follow: True
patterns: config.properties
use_regex: True
contains: LOG_PATH
#contains: ^((?!LOG_PATH).)*$ this Negative lookup doesn't works
register: matched_files
- name: print matches
debug:
var: matched_files
的方式:
assertRaises
或者您可以将其传递给callable和arguments:
with self.assertRaises(RegistrationError):
car.register_car('123123')
car.create_log.assert_called_once()