gpt4 book ai didi

unit-testing - 如何在 Flutter 中测试(单元测试)BehaviorSubject?

转载 作者:IT王子 更新时间:2023-10-29 07:03:07 27 4
gpt4 key购买 nike

我想测试管理我的表单的 Controller 流。我在这里尝试测试连接到验证器的电子邮件 Controller 。

我设法通过 sink 方法添加 Controller 值并通过 stream 方法恢复。

声明

  final emailController = BehaviorSubject<String>();

Stream<String> get email => emailController.stream.transform(validateEmail);

Function(String) get changeEmail => emailController.sink.add;

要测试的验证器

final validateEmail =
StreamTransformer<String, String>.fromHandlers(handleData: (email, sink) {
final emailRegExp = new RegExp(
r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?)*$",
caseSensitive: false,
multiLine: false,
);

emailRegExp.hasMatch(email).toString();
if (email.contains('@')) {
sink.add(email);
} else {
sink.addError('Enter a valid email');
}
});

单元测试

void main() {
UserProfilEditBloc userProfilEditBloc = new UserProfilEditBloc();

test('Email check BehaviorSubject', () {
userProfilEditBloc.changeEmail('testtest'); //Expected @

expect(userProfilEditBloc.email, emits('Enter a valid email'));
});
}

错误信息

00:02 +0 -1: Email check BehaviorSubject [E]
Enter a valid email
dart:async _StreamController.addError
package:async/src/result/error.dart 30:10 ErrorResult.addTo
package:async/src/stream_queue.dart 959:29 _TransactionRequest.update
package:async/src/stream_queue.dart 426:31 StreamQueue._updateRequests
package:async/src/stream_queue.dart 514:5 StreamQueue._addResult
package:async/src/stream_queue.dart 486:9 StreamQueue._ensureListening.<fn>
dart:async _EventSinkWrapper.addError
package:nexo/src/blocs/UserProfilEditBloc.dart 29:12 new _UserProfilEditBloc&Object&InputValidators.<fn>
===== asynchronous gap ===========================
dart:async _BoundSinkStream.listen
package:rxdart/src/observables/observable.dart 1724:20 Observable.listen
package:async/src/stream_queue.dart 483:31 StreamQueue._ensureListening
package:async/src/stream_queue.dart 542:7 StreamQueue._addRequest
package:async/src/stream_queue.dart 299:5 StreamQueue.startTransaction
package:test_api expect
package:flutter_test/src/widget_tester.dart 196:3 expect
test/register_form_test.dart 15:5 main.<fn>

00:02 +0 -1: Some tests failed.

如果我忘记@,验证器填充sink.addError()会导致这个错误。当验证器执行 sink.add() 时一切顺利。

最佳答案

我相信,您所缺少的只是:emitsErrorBehaviourSubject 有一个缓存值,所以你将如何测试它:

  test('Email check BehaviorSubject', () {
expectLater(userProfileEditBloc.email, emitsInOrder([
emits(anything), // current email if you want to check for that
emitsError('Enter a valid email'),\
// emitsDone // in case you also close the stream with the error
]));

userProfilEditBloc.changeEmail('testtest'); //Expected @
});

rxDart test files通常会为如何测试功能提供一个很好的起点。

关于unit-testing - 如何在 Flutter 中测试(单元测试)BehaviorSubject?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56405234/

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