Flutter Bloc Test Problem With Comparing Expected and Actual

927 views Asked by At

I'm trying to create a test case for getting list stores data and I currently fail to implement some working tests but I got some problems when trying to compare the data list.

This is my test's code:

blocTest(
      "_getListStore - successCase",
      setUp: () {
        final body = jsonEncode('');
        final params = '?indexPage=$page&limit=10';
        when(repoMock.getAllListStore(mallId, body, params, wlServiceMock))
            .thenAnswer((_) => Future.value(data));
        _data.clear();
        _tempData.clear();
        listDatas.clear();
        var reqData = data.content.stores;
        _tempData.addAll(reqData);
        List<ContentListStore> temp = [];
        for (var i = 0; i < reqData.length; i++) {
          temp.add(reqData[i]);
          if (temp.length == 1 || i == reqData.length - 1) {
            _data.add(DataStoreList(content: temp));
            temp = [];
          }
        }
        listDatas.addAll(_data);
      },
      build: () => listStoreBloc,
      act: (bloc) => bloc.add(GetListStore(
        mallId: mallId,
        page: page,
        isScroll: true,
        isPullToRefresh: true,
      )),
      expect: () => [
        isA<ListStorePageState>()
            .having((w) => w.currentPage, 'currentPage', 0)
            .having((w) => w.submitStatus, 'submitStatus',
                FormzStatus.submissionInProgress)
            .having((w) => w.message, 'message', StatusStateList.getListData)
            .having((w) => w.isLoading, 'isLoading', true),
        isA<ListStorePageState>()
            .having((w) => w.currentPage, 'currentPage', 0)
            .having((w) => w.submitStatus, 'submitStatus',
                FormzStatus.submissionSuccess)
            .having((w) => w.isLoading, 'isLoading', false)
            .having((w) => w.isScroll, 'isScroll', true)
            .having((w) => w.isPullToRefresh, 'isPullToRefresh', true)
            .having((w) => w.filter, 'filter', null)
            .having((w) => w.contentData, 'contentData', listDatas),
      ],
    );

this error from debug console:

Expected: [
            <<Instance of 'ListStorePageState'> with `currentPage`: <0> and `submitStatus`: FormzStatus:<FormzStatus.submissionInProgress> and `message`: StatusStateList:<StatusStateList.getListData> and `isLoading`: <true>>,
            <<Instance of 'ListStorePageState'> with `currentPage`: <0> and `submitStatus`: FormzStatus:<FormzStatus.submissionSuccess> and `isLoading`: <false> and `isScroll`: <true> and `isPullToRefresh`: <true> and `filter`: <null> and `contentData`: [Instance of 'DataStoreList']>
          ]
  Actual: [Instance of 'ListStorePageState', Instance of 'ListStorePageState']
   Which: at location [1] is <Instance of 'ListStorePageState'> which has `contentData` with value [Instance of 'DataStoreList'] which at location [0] is <Instance of 'DataStoreList'> instead of <Instance of 'DataStoreList'>

==== diff ========================================

[Instance of '[-HavingMatcher<-]ListStorePageState[->-]', Instance of '[-HavingMatcher<-]ListStorePageState[->-]']

==== end diff ====================================

I tried to print the data in setUp and my bloc, this everything is the same. any suggestion to compare these list data?

1

There are 1 answers

0
Cristhian Gracia On

You can override the Class toString() function, you might print the values:

class ListStorePageState {
  final String param1;
  final String param2;

  const ListStorePageState({required this.param1, required this.p aram2});

  @override
  String toString() => '''
    param1 => $param1.
    param2 => $param2
''';
}

void main() {
  const ListStorePageState listStorePageState = ListStorePageState(param1: "Val 1", param2: "Val2");

  print(listStorePageState);
}

output:

param1 => Val 1, 
param2 => Val2

witouth @override toString() the output is:

Instance of 'ListStorePageState'