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

- **ID:** `python/unittest-mock-assertion-failure`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

mock 的 assert_called_once() 失败，因为被补丁的函数从未被调用，通常由于补丁指向了错误的模块引用、被测代码提前返回或调用发生在不同线程。

## 版本兼容性

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

## 解决方案

1. **** (92% 成功率)
   ```
   Patch the name where it is used and assert against the same object:

with patch('app.orders.send_email') as m:
    place_order(order)
    m.assert_called_once_with('user@example.com')
   ```
2. **** (88% 成功率)
   ```
   Debug by printing the mock's call list:

print(m.mock_calls)
print(m.call_args_list)
   ```

## 无效尝试

- **** — This only checks that it was called at least once, but the count is 0, so it still fails. (90% 失败率)
- **** — create=True creates a mock attribute but does not make the real code call it. (85% 失败率)
