繁体   English   中英

Python-Mockito 'NoneType' object 没有属性 'strip'

[英]Python-Mockito 'NoneType' object has no attribute 'strip'

我正在用 mockito 编写单元测试,并且有一个 function 有一个给我带来麻烦的特定行:

score_data = json.loads(hdfs_client.read_txt_from_hdfs_with_pipe(score_path).strip())

我尝试了不同的方式 mocking 这个:

when(mock_hdfs_client).read_txt_from_hdfs_with_pipe(any()).thenReturn(score_cache_entry)

doReturn(score_cache_entry).when(self.mock_hdfs_client).read_txt_from_hdfs_with_pipe(any())

其中 'score_cache_entry' 是一个类似 json 的字符串。

运行单元测试时,我被抛出此属性错误:

    score_data = json.loads(hdfs_client.read_txt_from_hdfs_with_pipe(score_path).strip())
AttributeError: 'NoneType' object has no attribute 'strip'

在你的

when(mock_hdfs_client).read_txt_from_hdfs_with_pipe(any()).thenReturn(score_cache_entry)

any()可能是 python 内置。 如果您使用的是 python 3,请尝试使用省略号形式:

when(mock_hdfs_client).read_txt_from_hdfs_with_pipe(...).thenReturn(score_cache_entry)

暂无
暂无

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

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