我想测试名为getFileAsJson的appendRole,以打开的方式读取文件。 我的问题是我不知道下一个公开赛是哪个。有很多if/elif。在
def appendRole(self, hosts=None, _newrole=None, newSubroles=None, undoRole=False, config_path=None):
""" Same as changeRole but keeps subroles """
if hosts is None:
hosts = ["127.0.0.1"]
if newSubroles is None:
newSubroles = {}
if config_path is None:
config_path = self.config_path
with self._lock:
default = {}
data = self.getFileAsJson(config_path, default)
...................
...................
...................
...................
data1 = self.getFileAsJson(self.config_path_all, {"some"})
data2 = self.getFileAsJson(self.config_path_core, {"something"})
...................
...................
...................
def getFileAsJson(self, config_path, init_value):
"""
read file and return json data
if it wasn't create. Will created.
"""
self.createFile(config_path, init_value)
try:
with open(config_path, "r") as json_data:
data = json.load(json_data)
return data
except Exception as e:
self.logAndRaiseValueError(
"Can't read data from %s because %s" % (config_path, e))
即使您可以在Python mock builtin 'open' in a class using two different files找到问题的答案,我也希望您改变您的方法,为
getFileAsJson()
编写测试,然后信任它。在要测试} 来修补
appendRole()
,请使用^{getFileAsJson()
,然后通过side_effect
属性,可以指示模拟返回测试所需的内容。在因此,在对} 来模拟
getFileAsJson()
进行一些测试之后,您可以使用^{open
内建(也许您还需要修补createFile()
)。您的appendRole()
测试如下所示:相关问题 更多 >
编程相关推荐