gpt4 book ai didi

python - 模拟与魔术模拟

转载 作者:IT老高 更新时间:2023-10-28 21:05:04 25 4
gpt4 key购买 nike

我的理解是,MagicMockMock 的超集,它会自动执行“魔术方法”,从而无缝地提供对列表、迭代等的支持......然后呢普通 Mock 存在的原因是什么?这不只是 MagicMock 的精简版,实际上可以忽略吗? Mock 类是否知道任何 MagicMock 中不可用的技巧?

最佳答案

plain Mock 存在的原因是什么?

Mock 的作者 Michael Foord,地址为 a very similar question at Pycon 2011 (31:00) :

Q: Why was MagicMock made a separate thing rather than just folding the ability into the default mock object?

A: One reasonable answer is that the way MagicMock works is that it preconfigures all these protocol methods by creating new Mocks and setting them, so if every new mock created a bunch of new mocks and set those as protocol methods and then all of those protocol methods created a bunch more mocks and set them on their protocol methods, you've got infinite recursion...

What if you want accessing your mock as a container object to be an error -- you don't want that to work? If every mock has automatically got every protocol method, then it becomes much more difficult to do that. And also, MagicMock does some of this preconfiguring for you, setting return values that might not be appropriate, so I thought it would be better to have this convenience one that has everything preconfigured and available for you, but you can also take a ordinary mock object and just configure the magic methods you want to exist...

The simple answer is: just use MagicMock everywhere if that's the behavior you want.

关于python - 模拟与魔术模拟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17181687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com