# AssertionError: 预期 'mock_method' 被调用一次。实际调用 2 次。

- **ID:** `python/pytest-mock-not-reset-between-tests`
- **领域:** python
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

mock 对象在测试之间未重置，因此调用计数从先前测试累积。这发生在 mock 定义在模块级别或通过类属性共享时。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   @pytest.fixture
def mock_service():
    return MagicMock()

def test_one(mock_service):
    mock_service.method()
    mock_service.method.assert_called_once()
   ```
2. **** (93% 成功率)
   ```
   def test_one(mocker):
    mock = mocker.patch('module.func')
    mock()
    mock.assert_called_once()
   ```

## 无效尝试

- **** — If a test fails before reaching the reset line, the mock remains dirty for the next test. Also error-prone to maintain. (75% 失败率)
- **** — This changes the assertion but does not fix the accumulated call count. The assertion will still fail. (85% 失败率)
