Flutter 单元测试中断言失败错误

Pri*_*ick 8 dart flutter flutter-test

我想测试的单元代码:

imports ...
class TextHolder extends StatelessWidget{
  const TextHolder({
    Key key,
    @required this.text
    }) : assert (text != null),
          super(key: Key);
  @override
  Widget build(BuildContext context) {
    return Text(text);
  }
}
Run Code Online (Sandbox Code Playgroud)

当我编写测试代码时:

imports ...
void main(){
test('Assert Null check', (){
   // tried this
    expect(TextHolder(text: null), throwsAssertionError);
   // also this 
    final Matcher throwsAssertionError = throwsA(isA<AssertionError>());
    expect(TextHolder(text: null), throwsAssertionError);
});
}
Run Code Online (Sandbox Code Playgroud)

无法Failed assertion使用 flutter_test 包执行测试。当我运行测试代码时,我在控制台中收到类似以下内容的错误: Failed assertion: line 8 pos 16: 'text != null': is not true.

我在寻找什么:

  1. @required如果有人从我的单元代码中删除注释,我的测试用例应该会失败。
  2. 如果有人向属性发送null值,我的测试用例应该失败text

Kol*_*gaR 12

test('Assert Null check', (){
    expect(() { TextHolder(text: null) }, throwsAssertionError);
});
Run Code Online (Sandbox Code Playgroud)