Bloc: How to test using bloc_test

Created on 18 Feb 2020  路  6Comments  路  Source: felangel/bloc

Hey guys, I need a little help about how to test, or how to implement the following scenario:

In my mapEventToState I have the following logic

 if (event is FetchExchange || event is RefreshExchange){
        final Future<Exchange> futureExchange =  exchangeRepository.getExchange();
final Future<Exchange> futureExchange =  exchangeRepository.getExchange();
        final Future<Exchange> futureYesterdayExchange =  exchangeRepository.getExchange(date: _getYesterdayDate());

        final Exchange today = await _exchangeValues(await futureExchange, await futureYesterdayExchange);

        yield ExchangeLoaded(exchange: today);
}

my method _exchangeValues, iterate over the two futureExchange and set the "isPositive" flag, returning a new Future.

Future<Exchange> _exchangeValues(Exchange today, Exchange yesterday) async {
    for (final rate in today.rates) {
      rate.isPositive = yesterday.rates.any((f) => rate.rateAbbreviation == f.rateAbbreviation && rate.rate > f.rate );
    }
    return today;
  }

My ExchangeLoaded(exchange: today); returns this new Exchange object.

I want in the blocTest check the value of this flag. This is possible?

Thats my blocTest block:

blocTest(
            'emits [ExchangeLoading, ExchangeLoaded] when exchange returns today',
            build: () {
              when(exchangeRepository.getExchange())
                  .thenAnswer((_) => Future.value(futureExchange));
              when(exchangeRepository.getExchange(date: '2020-02-17'))
                  .thenAnswer((_) => Future.value(futureExchangeYesterday));
              return exchangeBloc;
            },
            act: (bloc) => bloc.add(FetchExchange()),
            expect: [
              ExchangeLoading(),
              ExchangeLoaded(exchange: futureExchange)
            ],
          );

but my futureExchange have the flags null. How can I get the "today" exchange?

If I need to change the architecture like, create a business layer, just tell me! :D

Thanks all!!

bloc_test question

Most helpful comment

Hello @felangel, this really works! :D

Thanks you so much! Now I will study the changes!
Have a good day!

All 6 comments

Hi @evandrom 馃憢
Thanks for opening an issue!

Are you able to share a link to the repository? Thanks!

Sure! Did it! :D Thank you!

https://github.com/evandrom/currency_exchange

Opened a pull request with the fixes required to make it work. Let me know if you have any questions 馃憤

Hello @felangel ! First of all, thanks you for the refactor. I will accept the changes!

But I still with one doubt. Basically, I wanna to test the following logic:

final newRates = today.rates.map((rate) {
      return rate.copyWith(
        isPositive: yesterday.rates.any((f) =>
            rate.rateAbbreviation == f.rateAbbreviation && rate.rate > f.rate),
      );
    }).toList();
    return today.copyWith(rates: newRates);

Considering that this method is private and inside the mapEventToState. How can I assert what rates are with the isPositive flag equals true?

Thanks you!

Hey @evandrom, no problem!

That is being asserted already in the expect of the blocTest at https://github.com/evandrom/currency_exchange/pull/4/files#diff-2200c2ceda4cd2f4fb58f98d6a89e54dR37.

Hello @felangel, this really works! :D

Thanks you so much! Now I will study the changes!
Have a good day!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abinvp picture abinvp  路  3Comments

RobPFarley picture RobPFarley  路  3Comments

clicksocial picture clicksocial  路  3Comments

tigranhov picture tigranhov  路  3Comments

nhwilly picture nhwilly  路  3Comments