# 类型错误：当使用可迭代对象作为mock.side_effect时，'int'对象不可调用

- **ID:** `python/unittest-mock-side-effect-type-error`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

mock的side_effect属性被设置为包含不可调用元素的可迭代对象（如列表），但mock作为函数被调用，导致尝试调用元素时出现TypeError。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |

## 解决方案

1. **Ensure all elements in the side_effect iterable are callable (e.g., functions or exceptions)** (90% 成功率)
   ```
   mock.side_effect = [lambda: 1, lambda: 2, ValueError('error')]
Now each call to mock() will call the respective lambda or raise the exception.
   ```
2. **Use side_effect with a function that generates values dynamically** (85% 成功率)
   ```
   def side_effect_func():
    return 42
mock.side_effect = side_effect_func
This ensures the mock is callable.
   ```

## 无效尝试

- **Setting side_effect to a single value instead of an iterable** — This changes the behavior; the mock will return that value for every call, which may not match the expected test scenario. (60% 失败率)
- **Using return_value instead of side_effect** — This only sets a single return value for all calls, not allowing different responses per call. (50% 失败率)
