# AttributeError: Mock object has no attribute 'commit'

- **ID:** `python/unittest-mock-spec-missing-attribute`
- **Domain:** python
- **Category:** type_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

A Mock created with spec= or autospec= restricts attributes to the spec target. The code under test accesses an attribute not present on the spec, raising AttributeError.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 3.11 | active | — | — |
| 3.12 | active | — | — |

## Workarounds

1. **** (90% success)
   ```
   from unittest.mock import patch
@patch('app.repo.Connection', autospec=True)
def test_save(mock_conn_cls):
    inst = mock_conn_cls.return_value
    inst.commit.return_value = None
    save()
    inst.commit.assert_called_once()
   ```
2. **** (88% success)
   ```
   from unittest.mock import create_autospec
mock_conn = create_autospec(Connection, instance=True)
mock_conn.commit()  # allowed because Connection.commit exists
   ```

## Dead Ends

- **** — Disables typo detection and signature validation; tests pass with wrong method names, hiding real bugs. (70% fail)
- **** — Violates spec contract; the mock no longer reflects the real interface and future signature changes go unnoticed. (60% fail)
