我之前曾询问过testing the size of a widget in Flutter。
但是,现在我正在尝试测试基础渲染对象的固有大小。
我试图这样做
testWidgets('MongolRichText has correct min instrinsic width',
(WidgetTester tester) async {
const String myString = 'A string';
await tester.pumpWidget(
Center(child: MongolText(myString)),
);
MongolRenderParagraph text = tester.firstRenderObject(find.byType(MongolRenderParagraph));
expect(text, isNotNull);
expect(text.getMinIntrinsicHeight(double.infinity), 100);
});
其中MongolText
创建一个MongolRenderParagraph
(类似于Text
最终创建一个Paragraph
的方式)。但是,出现以下错误:
══╡颤振测试框架引起的异常CA
运行测试时引发了以下StateError:
错误状态:无元素
如何获取基础渲染对象以对其进行测试?
我找到了答案,所以我将其添加为自我解答。我的答案在下面。
答案 0 :(得分:0)
您可以直接创建渲染对象,因此不需要抽取窗口小部件。
一个简单的例子在这里:
testWidgets('MongolRichText has correct min instrinsic width', (WidgetTester tester) async {
MongolRenderParagraph paragraph = MongolRenderParagraph(TextSpan(text: 'A string'));
final double textWidth = paragraph.getMaxIntrinsicWidth(double.infinity);
expect(textWidth, greaterThan(0));
});
我通过查看 paragraph_intrinsics_test.dart 的Flutter source code找到了解决方案:
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:flutter/rendering.dart';
import '../flutter_test_alternative.dart';
void main() {
test('list body and paragraph intrinsics', () {
final RenderParagraph paragraph = RenderParagraph(
const TextSpan(
style: TextStyle(height: 1.0),
text: 'Hello World',
),
textDirection: TextDirection.ltr,
);
final RenderListBody testBlock = RenderListBody(
children: <RenderBox>[
paragraph,
],
);
final double textWidth = paragraph.getMaxIntrinsicWidth(double.infinity);
final double oneLineTextHeight = paragraph.getMinIntrinsicHeight(double.infinity);
final double constrainedWidth = textWidth * 0.9;
final double wrappedTextWidth = paragraph.getMinIntrinsicWidth(double.infinity);
final double twoLinesTextHeight = paragraph.getMinIntrinsicHeight(constrainedWidth);
final double manyLinesTextHeight = paragraph.getMinIntrinsicHeight(0.0);
// paragraph
expect(wrappedTextWidth, greaterThan(0.0));
expect(wrappedTextWidth, lessThan(textWidth));
expect(oneLineTextHeight, lessThan(twoLinesTextHeight));
expect(twoLinesTextHeight, lessThan(oneLineTextHeight * 3.0));
expect(manyLinesTextHeight, greaterThan(twoLinesTextHeight));
expect(paragraph.getMaxIntrinsicHeight(double.infinity), equals(oneLineTextHeight));
expect(paragraph.getMaxIntrinsicHeight(constrainedWidth), equals(twoLinesTextHeight));
expect(paragraph.getMaxIntrinsicHeight(0.0), equals(manyLinesTextHeight));
// vertical block (same expectations)
expect(testBlock.getMinIntrinsicWidth(double.infinity), equals(wrappedTextWidth));
expect(testBlock.getMaxIntrinsicWidth(double.infinity), equals(textWidth));
expect(testBlock.getMinIntrinsicHeight(double.infinity), equals(oneLineTextHeight));
expect(testBlock.getMinIntrinsicHeight(constrainedWidth), equals(twoLinesTextHeight));
expect(testBlock.getMaxIntrinsicHeight(double.infinity), equals(oneLineTextHeight));
expect(testBlock.getMaxIntrinsicHeight(constrainedWidth), equals(twoLinesTextHeight));
expect(testBlock.getMinIntrinsicWidth(0.0), equals(wrappedTextWidth));
expect(testBlock.getMaxIntrinsicWidth(0.0), equals(textWidth));
expect(testBlock.getMinIntrinsicHeight(wrappedTextWidth), equals(twoLinesTextHeight));
expect(testBlock.getMaxIntrinsicHeight(wrappedTextWidth), equals(twoLinesTextHeight));
expect(testBlock.getMinIntrinsicHeight(0.0), equals(manyLinesTextHeight));
expect(testBlock.getMaxIntrinsicHeight(0.0), equals(manyLinesTextHeight));
// horizontal block (same expectations again)
testBlock.axisDirection = AxisDirection.right;
expect(testBlock.getMinIntrinsicWidth(double.infinity), equals(wrappedTextWidth));
expect(testBlock.getMaxIntrinsicWidth(double.infinity), equals(textWidth));
expect(testBlock.getMinIntrinsicHeight(double.infinity), equals(oneLineTextHeight));
expect(testBlock.getMinIntrinsicHeight(constrainedWidth), equals(twoLinesTextHeight));
expect(testBlock.getMaxIntrinsicHeight(double.infinity), equals(oneLineTextHeight));
expect(testBlock.getMaxIntrinsicHeight(constrainedWidth), equals(twoLinesTextHeight));
expect(testBlock.getMinIntrinsicWidth(0.0), equals(wrappedTextWidth));
expect(testBlock.getMaxIntrinsicWidth(0.0), equals(textWidth));
expect(testBlock.getMinIntrinsicHeight(wrappedTextWidth), equals(twoLinesTextHeight));
expect(testBlock.getMaxIntrinsicHeight(wrappedTextWidth), equals(twoLinesTextHeight));
expect(testBlock.getMinIntrinsicHeight(0.0), equals(manyLinesTextHeight));
expect(testBlock.getMaxIntrinsicHeight(0.0), equals(manyLinesTextHeight));
});
}