【问题标题】:The problem with .addError in the GetX StreamsGetX Streams 中的 .addError 问题
【发布时间】:2021-11-06 13:44:18
【问题描述】:

错误:

NoSuchMethodError: Closure call with mismatched arguments: function '_ExpectedFunction.max1'
Receiver: Closure: ([Object?]) => void from Function 'max1':.
Tried calling: _ExpectedFunction.max1("Algo errado aconteceu. Tente novamente em breve.", Instance of '_StringStackTrace')
Found: _ExpectedFunction.max1([Object?]) => T
dart:core                                                                 _objectNoSuchMethod
GetStream._notifyError
package:get/…/rx_stream/get_stream.dart:60
GetStream.addError
package:get/…/rx_stream/get_stream.dart:104
GetxSurveysPresenter.loadData
package:fordev/…/presenters/getx_surveys_presenter.dart:31
main.<fn>
test\…\presenters\get_surveys_presenter_test.dart:89
main.<fn>
test\…\presenters\get_surveys_presenter_test.dart:82

我不明白为什么测试不起作用! 这两种形式我都试过了,都不行!

  test('Should emit correct events on failure', () async {
    // arrange
    mockLoadSurveysError();
    // assert
    sut.surveysStream.listen(null, onError: expectAsync1((error) =>
      expect(error, 
       StackTrace.fromString(UIError.unexpected.description))));
    // act
    await sut.loadData();
  });

  test('Should emit correct events on failure', () async {
    // arrange
    mockLoadSurveysError();
    // assert
    sut.surveysStream.listen(null, onError: expectAsync1((error) =>
      expect(error, UIError.unexpected.description)));
    // act
    await sut.loadData();
  });

我认为 StackTrace.empty 是原因。

class GetxSurveysPresenter implements SurveysPresenter {
  final LoadSurveys loadSurveys;

  GetxSurveysPresenter({@required this.loadSurveys});

  final _surveys = Rx<List<SurveyViewModel>>(null);

  Stream<List<SurveyViewModel>> get surveysStream => _surveys.stream;

  Future<void> loadData() async {
    try {
      final surveys = await loadSurveys.load();
      _surveys.value = surveys
        .map((survey) => SurveyViewModel(
          id: survey.id, 
          question: survey.question, 
          date: DateFormat('dd MMM yyyy').format(survey.dateTime), 
          didAnswer: survey.didAnswer
        )).toList();
    } on DomainError {
      _surveys.subject.addError(UIError.unexpected.description, StackTrace.empty);
    }
  }
}

错误:https://i.stack.imgur.com/5UKVh.png

测试代码:https://i.stack.imgur.com/PSw7T.png

【问题讨论】:

    标签: flutter dart testing stream flutter-getx


    【解决方案1】:
    test('Should emit correct events on failure', () async {
      // arrange
      mockLoadSurveysError();
      // assert
      sut.surveysStream.listen(null, onError: expectAsync2((error, stack) =>
        expect(error, UIError.unexpected.description)));
      // act
      await sut.loadData();
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-22
      • 2020-04-28
      • 2021-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-30
      • 2018-03-04
      相关资源
      最近更新 更多