# TestFailure: Golden 图像尺寸不匹配：期望 1080x1920，实际 720x1280

- **ID:** `flutter/golden-test-image-size-mismatch`
- **领域:** flutter
- **类别:** test_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

用于图像比较的 golden 文件与渲染的小部件尺寸不同，通常是由于屏幕尺寸、小部件布局或测试设备配置发生变化。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Flutter 3.10.0 | active | — | — |
| Flutter 3.19.0 | active | — | — |
| Dart 3.0.0 | active | — | — |

## 解决方案

1. ```
   通过使用 '--update-goldens' 标志运行测试来重新生成 golden 文件。示例：
  flutter test --update-goldens test/widget_test.dart
这将用当前渲染输出覆盖 golden 文件。
   ```
2. ```
   确保测试小部件以与 golden 相同的尺寸渲染。在测试中，使用以下方法设置固定表面尺寸：
  testWidgets('golden test', (tester) async {
    await tester.binding.setSurfaceSize(Size(1080, 1920));
    await tester.pumpWidget(MyWidget());
    await expectLater(find.byType(MyWidget), matchesGoldenFile('golden.png'));
  });
   ```
3. ```
   检查可能改变小部件尺寸的响应式布局更改；在测试中使用固定大小的 SizedBox 包装小部件以匹配 golden。
   ```

## 无效尝试

- **Update the golden file manually by copying the actual output to the golden directory** — If the layout change is unintended, copying the output introduces a false positive; the test passes but the UI is broken. (60% 失败率)
- **Set a fixed screen size in the test using TestWidgetsFlutterBinding** — Fixing the screen size may not match the golden's expected dimensions; the dimensions must align exactly. (50% 失败率)
- **Use 'matchGoldenFile' with a tolerance parameter to ignore size differences** — The 'matchGoldenFile' method does not have a tolerance parameter for dimensions; it only compares pixel data after resizing, which can cause blurry comparisons. (80% 失败率)
