# PHPUnit 10.5：快照断言失败异常：快照 "tests/__snapshots__/UserProfileTest__test_render__1.snap" 与预期快照不匹配。使用 --update-snapshots 进行更新。

- **ID:** `php/phpunit-snapshot-assertion-failure`
- **领域:** php
- **类别:** test_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

磁盘上的快照文件与当前测试输出不一致，通常是由于有意的UI更改、依赖项更新导致渲染HTML变化，或快照文件被意外修改。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| phpunit/phpunit:10.5.0 | active | — | — |
| spatie/phpunit-snapshot-assertions:5.2.0 | active | — | — |

## 解决方案

1. ```
   如果更改是有意的，使用 --update-snapshots 标志运行测试套件以覆盖快照文件：'vendor/bin/phpunit --update-snapshots'
   ```
2. ```
   使用 PHPUnit 默认输出或 'git diff tests/__snapshots__/' 等工具检查差异，确认更改正确后，提交更新后的快照文件。
   ```
3. ```
   如果快照损坏，从版本控制中恢复：'git checkout -- tests/__snapshots__/UserProfileTest__test_render__1.snap'
   ```

## 无效尝试

- **** — Deleting the entire __snapshots__ directory and rerunning tests causes all snapshots to be recreated, but this discards the intended baseline for other tests, leading to false positives or missed regressions. (70% 失败率)
- **** — Manually editing the snapshot file to match expected output often introduces formatting inconsistencies (e.g., trailing whitespace, line endings), which can cause the assertion to still fail or produce a different diff. (60% 失败率)
- **** — Running tests with --verbose or --debug without --update-snapshots does not resolve the mismatch; it only provides more output details but does not update the file. (100% 失败率)
