[英]How to test state in flutter?
So I have a simple counter app,所以我有一个简单的计数器应用程序,
class CounterApp extends StatefulWidget {
const CounterApp({Key? key}) : super(key: key);
@override
_CounterAppState createState() => _CounterAppState();
}
class _CounterAppState extends State<CounterApp> {
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Text(_counter.toString()),
),
);
}
}
So how do I test the _counter
state?那么如何测试
_counter
状态呢?
I tried doing this,我试过这样做,
testWidgets("counter", (tester) async {
const key = Key("counter");
await tester.pumpWidget(const CounterApp(key: key));
final state = tester.state(find.byKey(key));
expect(state._counter, 0);
});
But I get error Error: The getter '_counter' isn't defined for the class
.但我收到错误
Error: The getter '_counter' isn't defined for the class
。 Are we even supposed to test state?我们甚至应该测试状态吗?
First, you need to specify the type while using state
method to avoid compiling errors:首先,您需要在使用
state
方法时指定类型以避免编译错误:
final _CounterAppState state = tester.state(find.byKey(key));
Second, _CounterAppState
and _counter
are private and you shouldn't test private classes/variables directly.其次,
_CounterAppState
和_counter
是私有的,您不应该直接测试私有类/变量。 You can make the class public and provide a public getter for the private variable:您可以将类设为公共并为私有变量提供公共 getter:
int get testCounter => _counter;
However, there is a way to access private declarations which I wouldn't recommend.但是,有一种方法可以访问我不推荐的私有声明。 Annotating your private variable/class with
@visibleForTesting
will make it public to make code testable.使用
@visibleForTesting
注释您的私有变量/类将使其公开以使代码可测试。 Don't forget to import foundation or meta library.不要忘记导入基础或元库。
Used to annotate a declaration that was made public, so that it is more visible than otherwise necessary, to make code testable.
用于注释公开的声明,使其比其他必要的更明显,使代码可测试。
Tools, such as the analyzer, can provide feedback if
分析器等工具可以提供反馈,如果
- the annotation is associated with a declaration not in the lib folder of a package, or a private declaration, or a declaration in an unnamed static extension, or
注释与不在包的 lib 文件夹中的声明、私有声明或未命名静态扩展中的声明相关联,或
- the declaration is referenced outside of its defining library or a library which is in the test folder of the defining package.
该声明在其定义库或定义包的测试文件夹中的库之外被引用。
Here is the implementation:这是实现:
// Import the foundation library
import 'package:flutter/foundation.dart';
class CounterApp extends StatefulWidget {
const CounterApp({Key? key}) : super(key: key);
@override
_CounterAppState createState() => _CounterAppState();
}
// Add the annotation above the class
@visibleForTesting
class _CounterAppState extends State<CounterApp> {
// Add the annotation above the variable
@visibleForTesting
int _counter = 0;
void _incrementCounter() {
setState(() {
_counter++;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Text(_counter.toString()),
),
);
}
}
You might want to remove the annotation after testing.您可能希望在测试后删除注释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.