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

时间:2019-06-01 08:55:51

标签: unit-testing flutter dart behaviorsubject

我想测试管理我的表单的控制器流。在这里,我正在尝试测试连接到验证程序的电子邮件控制器。

我设法通过接收器方法增加了控制器的值,并通过流方法进行了恢复。

声明

  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()时,一切都会顺利进行。

1 个答案:

答案 0 :(得分:0)

我相信,您所缺少的只是:(from res in await catalogRepository.SearchCatalog(request) let categoryIds = GetCategoriesById(res.CatalogId, 0, new List<int>()) select new Contracts.Models.GetSearchCatalogResponse() { Id = categoryIds }).ToList(); emitsError具有缓存的值,因此测试该方法的方法是:

BehaviourSubject

rxDart test files通常提供一个很好的起点来测试功能。