# 断言错误：期望调用：mock_function(1, 2, 3)
实际调用：mock_function(1, 2, 4)

- **ID:** `python/unittest-mock-assert-called-with-argument-mismatch`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

模拟对象被调用时使用的参数与assert_called_with等断言方法中指定的预期参数不匹配，通常是由于被测代码中的数据或逻辑略有差异。

## 版本兼容性

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

## 解决方案

1. **Debug the code under test to understand why arguments differ, then adjust the test or fix the code** (90% 成功率)
   ```
   Add print statements or use a debugger to inspect the actual arguments passed to the mock. For example: mock_function.assert_called_with(1, 2, 3) but if it fails, check the actual call via mock_function.call_args.
   ```
2. **Use `assert_called_once_with` with flexible matchers like `unittest.mock.ANY` or custom matchers for acceptable variations** (80% 成功率)
   ```
   from unittest.mock import ANY
mock_function.assert_called_once_with(1, 2, ANY)
This allows the third argument to be anything, but still verifies the first two.
   ```

## 无效尝试

- **Changing the expected arguments to match the actual call without investigating why the code under test produces different values** — This masks the real issue in the code under test, potentially allowing bugs to go undetected. (70% 失败率)
- **Using `assert_any_call` instead of `assert_called_with` to ignore argument differences** — This weakens the test by not verifying the exact arguments, which may hide important regressions. (50% 失败率)
