# AttributeError: Mock object has no attribute 'foo' when using spec=MyClass

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

## Root Cause

Creating a mock with spec=MyClass restricts the mock to only have attributes that exist on MyClass, and accessing a non-existent attribute raises an AttributeError.

## Version Compatibility

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

## Workarounds

1. **** (80% success)
   ```
   Add the missing attribute to MyClass or use a mock with spec=MyClass and then add the attribute dynamically: mock.foo = MagicMock()
   ```
2. **** (85% success)
   ```
   Use autospec=True instead of spec to automatically create a mock that matches the class's interface.
   ```

## Dead Ends

- **** — Using spec_set=True instead of spec still raises the same error for non-existent attributes. (50% fail)
- **** — Removing the spec parameter entirely allows any attribute but may hide API mismatches. (60% fail)
