{
  "id": "python/unittest-mock-call-order-assertion",
  "signature": "AssertionError: Expected mock to have been called once. Called 2 times.",
  "signature_zh": "断言错误：期望模拟对象被调用一次。实际被调用2次。",
  "regex": "AssertionError:\\ Expected\\ mock\\ to\\ have\\ been\\ called\\ once\\.\\ Called\\ 2\\ times\\.",
  "domain": "python",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "The mock object was called more times than expected, often due to the code under test calling the mocked function multiple times when it should only be called once.",
  "root_cause_type": "generic",
  "root_cause_zh": "模拟对象被调用的次数超过预期，通常是由于被测代码多次调用被模拟的函数，而预期只应调用一次。",
  "versions": [
    {
      "version": "3.8",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "3.9",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Changing assert_called_once to assert_called to ignore the count",
      "why_fails": "This weakens the test by not verifying the exact number of calls, which may hide regressions.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "Adding a sleep before the assertion to allow time for calls to complete",
      "why_fails": "This does not fix the root cause; the mock is still called multiple times.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Debug the code under test to understand why the mock is called multiple times",
      "success_rate": 0.9,
      "how": "Add print statements or use a debugger to trace calls. For example:\nmock.assert_has_calls([call(1), call(2)])\nThen adjust the code to ensure it calls the mock only once.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use assert_called_with to verify the arguments of the first call, then assert the total call count",
      "success_rate": 0.85,
      "how": "mock.assert_called_with(1)\nassert mock.call_count == 1\nThis verifies both the arguments and the count.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": null,
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.87,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2025-06-05",
  "last_confirmed": "2025-01-01",
  "last_updated": "2025-01-01",
  "evidence_count": 0,
  "tags": [],
  "locale": "en",
  "aliases": []
}