# AssertionError: 期望 'mock_method' 被调用一次。实际调用 0 次。

- **ID:** `python/pytest-mock-patch-not-applied`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

mock 应用到了错误的命名空间，或被测代码直接导入函数，绕过了 patch。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   If my_module.py does 'from other_module import my_func', patch 'my_module.my_func' instead of 'other_module.my_func'.
   ```
2. **** (90% 成功率)
   ```
   def test_my_func(monkeypatch):
    monkeypatch.setattr('my_module.my_func', lambda: None)
    # test code
   ```
3. **** (85% 成功率)
   ```
   import my_module; print(my_module.my_func.__module__) to see where it's defined, then patch accordingly.
   ```

## 无效尝试

- **** — The test then passes but does not verify the intended behavior; the mock is still not applied. (95% 失败率)
- **** — If the target is incorrect, patch.object fails similarly. (90% 失败率)
