gpt4 book ai didi

flutter - 测试小部件时,在 BlocListener 上测试失败

转载 作者:行者123 更新时间:2023-12-04 11:02:33 24 4
gpt4 key购买 nike

我有两个测试,用于检查在使用 BlocListener 流式传输和处理登录状态时是否显示 snackbar 。

void main() async {
AuthenticationRepositoryMock _authenticationRepositoryMock;
LoginBlocMock _loginBloc;
final fireBaseUserMock = FirebaseUserMock();
final randomValidPassword = "password";
final buttonFinder = find.byKey(Key('credentials_button'));
final snackBarFailureFinder = find.byKey(Key("snack_bar_failure"));
final snackBarLoadingFinder = find.byKey(Key("snack_bar_loading"));
final emailFieldFinder = find.byKey(Key('email_field'));
final passwordFieldFinder = find.byKey(Key('password_field'));

Widget makeTestableWidget() {
return BlocProvider<LoginBloc>(
builder: (context) => _loginBloc,
child: MaterialApp(
home: Scaffold(
body: LoginPage(),
)
),
);
}

setUp((){
_authenticationRepositoryMock = AuthenticationRepositoryMock();
_loginBloc = LoginBlocMock(authenticationRepository: _authenticationRepositoryMock);
});

testWidgets('Show snack bar when state is LoginFailure', (WidgetTester tester) async {

//Arrange
var expectedStates = [
LoginInitial(),
LoginFailure(error: "Could not find user. Please try different credentials")
];

whenListen(_loginBloc, Stream.fromIterable(expectedStates));

//Act
await tester.pumpWidget(makeTestableWidget());

expect(snackBarFailureFinder, findsNothing);

await tester.enterText(emailFieldFinder, fireBaseUserMock.email);
await tester.pumpAndSettle();

await tester.enterText(passwordFieldFinder, randomValidPassword);
await tester.pumpAndSettle();

await tester.tap(buttonFinder);
await tester.pumpAndSettle();

//Assert
expect(snackBarFailureFinder, findsOneWidget);
});

//FAILING FOR NO REASON!
testWidgets('Show snack bar when state is LoginLoading', (WidgetTester tester) async {

//Arrange
var expectedStates = [
LoginInitial(),
LoginLoading()
];

whenListen(_loginBloc, Stream.fromIterable(expectedStates));

//Act
await tester.pumpWidget(makeTestableWidget());

expect(snackBarLoadingFinder, findsNothing);

await tester.enterText(emailFieldFinder, fireBaseUserMock.email);
await tester.pumpAndSettle();

await tester.enterText(passwordFieldFinder, randomValidPassword);
await tester.pumpAndSettle();

await tester.tap(buttonFinder);
await tester.pumpAndSettle();

//Assert
expect(snackBarLoadingFinder, findsOneWidget);
});
}

这两个测试正在测试页面上的以下小部件:

BlocListener<LoginBloc, LoginState>(
listener: (context, state){
if(state is LoginFailure){
Scaffold.of(context)
.showSnackBar(SnackBar(
key: Key("snack_bar_failure"),
content: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [Text('Login Failure'), Icon(Icons.error)],
),
backgroundColor: Colors.redAccent
));
}

if (state is LoginLoading) {
Scaffold.of(context)
.showSnackBar(SnackBar(
key: Key("snack_bar_loading"),
content: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [Text('Logging in...'), Spinner()],
),
backgroundColor: Colors.blueAccent
));
}
}

查找带有键“snack_bar_failure”的 snackbar 的第一个测试通过了,但第二个测试没有通过。这实际上是相同的测试,相同的设置,只是预期状态不同, snackbar 的键不同 snack_bar_loading

第二个测试失败并显示以下错误消息:

The following TestFailure object was thrown running a test:
Expected: exactly one matching node in the widget tree
Actual: ?:<zero widgets with key [<'snack_bar_loading'>] (ignoring offstage widgets)>
Which: means none were found but one was expected

我错过了什么吗?

最佳答案

问题已在问题(https://github.com/felangel/bloc/issues/655)中得到解答。希望对您有所帮助!

关于flutter - 测试小部件时,在 BlocListener 上测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58709789/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com