python
runtime_error
ai_generated
true
AssertionError: Expected 'mock.method' to have been called once. Called 2 times.
ID: python/unittest-mock-assert-call-order
80%Fix Rate
88%Confidence
0Evidence
2025-04-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 3.9 | active | — | — | — |
| 3.10 | active | — | — | — |
Root Cause
使用 assert_called_once 但 mock 方法被多次调用,可能由于代码逻辑或测试重复执行。
generic中文
使用 assert_called_once 但 mock 方法被多次调用,可能由于代码逻辑或测试重复执行。
Workarounds
-
90% success
检查最后一次调用参数,`mock.method.assert_called_with(参数)`。
-
95% success
`assert mock.method.call_count == 1` 更精确。
Dead Ends
Common approaches that don't work:
-
50% fail
可能改变被测代码行为。
-
60% fail
不验证调用次数,可能掩盖问题。