为什么我在使用 mock 的副作用时会收到“StopIteration”错误?

Why am I getting a 'StopIteration' error when using mock's side effect?

提问人:ifrj 提问时间:7/16/2023 最后编辑:quamranaifrj 更新时间:7/16/2023 访问量:96

问:

请参阅下面的我的函数,它使用函数,我试图模拟并使用它为两个函数调用分配不同的值。Pathside_effect

def some_function(input_file, file): 
    compressed_file = Path(input_file)
    non_compressed_file = Path(file)



@mock.patch('exchanges.impl.bmex.preprocessing.Path')
def test_choose_MFII_files(mock_Path):
    mock_Path.side_effect = [Path(pd.jobserve_config[BMLL_SECTION][WORK_DIR]+ '/MDPRECVOLTICK_MC_20230703.ZIP'),
                              Path(pd.jobserve_config[BMLL_SECTION][WORK_DIR] + '/MERGED_MDPRECVOLTICK_MC_20230703.ZIP')]

当我运行上述操作时,我得到:

_mock_self = <MagicMock name='Path' id='140495179866752'>, args = (PosixPath('/tmp'), 'MERGED_MDPRECVOLTICK_MC_20230703.ZIP'), kwargs = {}
self = <MagicMock name='Path' id='140495179866752'>, effect = <list_iterator object at 0x7fc7954ae220>

 def _execute_mock_call(_mock_self, *args, **kwargs):
        self = _mock_self
        # separate from _increment_mock_call so that awaited functions are
        # executed separately from their call, also AsyncMock overrides this method
    
        effect = self.side_effect
        if effect is not None:
            if _is_exception(effect):
                raise effect
            elif not _callable(effect):
>               result = next(effect)
E               StopIteration

../../anaconda3/envs/jobserveee/lib/python3.8/site-packages/mock/mock.py:1163: StopIteration

知道为什么会这样吗?

嘲笑 副作用

评论

0赞 slothrop 7/16/2023
如果你给一个可迭代对象,它“必须在每次调用时产生一个值”docs.python.org/3/library/......在测试中,修补后的函数是否被调用了两次以上?如果是这样,您将收到错误,因为您的迭代对象只有两个项目。side_effect
0赞 ifrj 7/16/2023
谢谢你,我的朋友。它被叫了3次!

答: 暂无答案