mardi 26 novembre 2019

How to test the intrinsic size of a render object in Flutter

I have previously asked about testing the size of a widget in Flutter.

However, now I am trying to test the intrinsic size of the underlying render object.

I tried to do this

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);
});

where MongolText creates a MongolRenderParagraph (similarly to how Text ends up creating a Paragraph). However, I get the following error:

══╡ EXCEPTION CAUGHT BY FLUTTER TEST FRAMEWORK ╞════════════
The following StateError was thrown running a test:
Bad state: No element

How do I get the underlying render object to run tests on it?

I found the answer so I am adding this as a self answer Q&A. My answer is below.

Aucun commentaire:

Enregistrer un commentaire