# AssertionError: Expected mock to have been called once. Called 2 times.

- **ID:** `python/unittest-mock-call-order-assertion`
- **Domain:** python
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## 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.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.8 | active | — | — |
| 3.9 | active | — | — |

## Workarounds

1. **Debug the code under test to understand why the mock is called multiple times** (90% success)
   ```
   Add print statements or use a debugger to trace calls. For example:
mock.assert_has_calls([call(1), call(2)])
Then adjust the code to ensure it calls the mock only once.
   ```
2. **Use assert_called_with to verify the arguments of the first call, then assert the total call count** (85% success)
   ```
   mock.assert_called_with(1)
assert mock.call_count == 1
This verifies both the arguments and the count.
   ```

## Dead Ends

- **Changing assert_called_once to assert_called to ignore the count** — This weakens the test by not verifying the exact number of calls, which may hide regressions. (60% fail)
- **Adding a sleep before the assertion to allow time for calls to complete** — This does not fix the root cause; the mock is still called multiple times. (80% fail)
