简体   繁体   English

flutter 在扩展 EditableText “坏 state:无元素”的小部件上测试 enterText:构建可编辑文本以供提及

[英]flutter test enterText on widget extending EditableText “Bad state: No element” : building editable text for mention

I have a class that extends EditableText, which provides styled editing - colour spans and so on.我有一个扩展 EditableText 的 class,它提供样式编辑 - 颜色范围等。

The parent widget has a bool flag to use default TextInput or the custom one that extends EditableText.父小部件有一个 bool 标志来使用默认 TextInput 或扩展 EditableText 的自定义标志。

Testing works fine for the default one:测试适用于默认测试:

final String dummyDesc = 'dummy desc';
final Finder mention = find.byType(Mention);
await tester.enterText(mention, dummyDesc);

however, if I do the same on a page where Mention uses the custom input, the test fails as it cannot find a TextInput - as there isn't one.但是,如果我在 Mention 使用自定义输入的页面上执行相同操作,则测试将失败,因为它找不到 TextInput - 因为没有。

Error:错误:

Bad state: No element

How Do you use enterText on a widget that extends EditableText?如何在扩展 EditableText 的小部件上使用 enterText? Or something similar?或者类似的东西?

This appears to be due to the showKeyboard method that enterText calls internally: https://github.com/flutter/flutter/blob/3b067049adc14ffb300e78d9b0d0adf3d581de7c/packages/flutter_test/lib/src/widget_tester.dart#L854这似乎是由于enterText内部调用的showKeyboard方法: https://github.com/flutter/flutter/blob/3b067049adc14ffb300e78d9b0d0adf3d581de7c/packages/flutter_test/lib/src/widget_tester.dart#L84

It is checking that the finder matches or is a descendent of EditableText , which does it's matching based on the runtimeType.它正在检查查找器是否匹配或者是EditableText的后代,它是否根据 runtimeType 进行匹配。

It's a bit hacky, but you could provide a runtimeType getter, which returns EditableText and will allow it to work.这有点 hacky,但您可以提供一个 runtimeType getter,它返回EditableText并允许它工作。

class Mention extends EditableText {
  // ...

  Type get runtimeType => EditableText;

  // ...
}

Caveat: This will mean that you will not be able to find.byType(Mention) any more.警告:这意味着您将无法再找到find.byType(Mention) You can work around this by writing a custom finder, or using the predicate finder find.byElementPredicate((Element element) => element.widget is Mention) .您可以通过编写自定义查找器或使用谓词查找器find.byElementPredicate((Element element) => element.widget is Mention)来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM