# AssertionError: 未抛出 ValueError
在处理上述异常时，又发生了一个异常:
TypeError: 'ValueError' 对象不可调用

- **ID:** `python/unittest-mock-side-effect-not-raised`
- **领域:** python
- **类别:** type_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

mock.side_effect 被设置为异常实例而不是异常类；mock 尝试调用该实例并抛出 TypeError，而非预期的异常。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.11 | active | — | — |
| 3.12 | active | — | — |

## 解决方案

1. **** (96% 成功率)
   ```
   from unittest import mock

with mock.patch('myapp.fetch') as m:
    m.side_effect = ValueError  # class, not ValueError()
    with pytest.raises(ValueError):
        call_sut()
   ```
2. **** (94% 成功率)
   ```
   def boom(*args, **kwargs):
    raise ValueError('bad input')

with mock.patch('myapp.fetch', side_effect=boom):
    with pytest.raises(ValueError, match='bad input'):
        call_sut()
   ```

## 无效尝试

- **** — return_value returns the exception object as the call result; it is never raised, so the test still fails. (90% 失败率)
- **** — Masks the mock misconfiguration and makes the test pass for the wrong reason; the SUT never actually raised. (85% 失败率)
