GithubHelp home page GithubHelp logo

Comments (14)

rodrigobastosv avatar rodrigobastosv commented on May 18, 2024 2

Thanks for the reply @felangel !

from equatable.

HossamElharmeil avatar HossamElharmeil commented on May 18, 2024 1

So, I implemented the exact same class without the DateTime parameter and this time it actually worked! I guess having a parameter that is not equatable by value in the class means the class itself cannot be equatable by value regardless of the parameters passed to props?

Anyway, thank you so much for your help, Felix. At least I know how it works now xD

class ReservationTest extends Equatable {
  final String salon;
  final String userID;

  ReservationTest({this.salon, this.userID});

  Map<String, dynamic> toJson() {
    return {
      'userID': userID,
      'salon': salon,
    };
  }

  @override
  String toString() {
    return "Reservation => userID: $userID, salon: $salon";
  }

  @override
  List<Object> get props => [salon, userID];
}

from equatable.

felangel avatar felangel commented on May 18, 2024 1

@HossamElharmeil yup you can create your own EquatableDateTime by using the EquatableMixin. Closing for now but feel free to comment with additional questions/comments and I'm happy to continue the conversation 👍

from equatable.

rodrigobastosv avatar rodrigobastosv commented on May 18, 2024 1

Hi @felangel can we reopen this? Turns out i'm having the exact same problem as @HossamElharmeil.
When i instantiate the list directly on the test it works, if i initialize the list inside setUp it doesn't work.

It may not be a problem with Equatable, but this behavior it's quite strange

from equatable.

rodrigobastosv avatar rodrigobastosv commented on May 18, 2024 1

Hi @felangel , this is my repo https://github.com/rodrigobastosv/firebase_bloc

I created branchs with the problematic tests only

  • The branch passing-tests has the tests working with some workarounds
  • The branch failling-tests has the tests as i expected to be, but that are failing to pass

See that my model uses equatable with only id, so i expected to just work.

There's another test that is oddly failing if you could please help telling me why.

Thanks very much man!

from equatable.

felangel avatar felangel commented on May 18, 2024

Hi @HossamElharmeil 👋
Thanks for opening an issue!

I believe the issue is your Reservation class is not extending Equatable. Can you please confirm? Thanks!

from equatable.

HossamElharmeil avatar HossamElharmeil commented on May 18, 2024

I thought that might be the case too but it does extend equatable when I double checked :D
Here is the implementation

class Reservation extends Equatable {
  final String salon;
  final String userID;
  final DateTime dateTime;

  Reservation({this.salon, this.userID, this.dateTime});

  @override
  String toString() {
    return "Reservation => userID: $userID, salon: $salon, date: $dateTime";
  }

  Map<String, dynamic> toJson() {
    return {
      'userID': userID,
      'salon': salon,
      'dateTime': dateTime,
    };
  }

  @override
  List<Object> get props => [salon, userID, dateTime];
}

from equatable.

felangel avatar felangel commented on May 18, 2024

@HossamElharmeil what about DateTime? I think that might be the problem.

from equatable.

HossamElharmeil avatar HossamElharmeil commented on May 18, 2024

I tried changing the props list only to contain the userID parameter but that did not work either which is really interesting. I think the issue is with the reservation class as you suggested but I'll have to figure out how

class Reservation extends Equatable {
  final String salon;
  final String userID;
  final DateTime dateTime;

  Reservation({this.salon, this.userID, this.dateTime});

  @override
  String toString() {
    return "Reservation => userID: $userID, salon: $salon, date: $dateTime";
  }

  Map<String, dynamic> toJson() {
    return {
      'userID': userID,
      'salon': salon,
      'dateTime': dateTime,
    };
  }

  @override
  List<Object> get props => [userID];
}

from equatable.

HossamElharmeil avatar HossamElharmeil commented on May 18, 2024

Somehow I got it to work by instantiating the list directly inside the test instead of declaring it in the main() function of the test and then passing it as a parameter.

blocTest('should return ReservationsLoadSucess when the use case returns a list of reservationsList',

    build: () {
      when(mockGetReservations(any)).thenAnswer((_) async => Right([Reservation(user: "12345", salon: "salon", dateTime: DateTime(1998, 8,8))]));
      return ReservationBloc(getReservations: mockGetReservations);
    },
    act: (bloc) async {
      bloc.add(ReservationsRequested(user));
    },
    expect: [
      ReservationsInitial(),
      ReservationsLoadInProgress(),
      ReservationsLoadSuccess([Reservation(user: "12345", salon: "salon", dateTime: DateTime(1998, 8,8))]),
    ],
  );

Turns out that the implementation of DateTime does actually override the equals operator for value equality, it had to do with the declaration of the list which is kinda weird. Maybe I'll have to do some research about runtime and compile time stuff in dart when it comes to initializing something in a list.

from equatable.

felangel avatar felangel commented on May 18, 2024

@rodrigobastosv can you provide a link to a sample which reproduces the issue?

from equatable.

rodrigobastosv avatar rodrigobastosv commented on May 18, 2024

Sure, just let me get back at home and i'll provided it. Thanks

from equatable.

felangel avatar felangel commented on May 18, 2024

@rodrigobastosv will take a look shortly, thanks!

from equatable.

felangel avatar felangel commented on May 18, 2024

@rodrigobastosv took a look and the problem is due to the fact that you're using an immutable list for congressPeople.

import 'package:bloc_test/bloc_test.dart';
import 'package:firebase_bloc/model/congressman_model.dart';
import 'package:firebase_bloc/ui/bloc/bloc.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';

import '../mocks.dart';

void main() {
  group('CongressBloc tests', () {
    MockCongressRepository mockCongressRepository;
    final List<CongressmanModel> congressPeople = [CongressmanModel(id: 1, nome: 'Test')];

    setUp(() {
      mockCongressRepository = MockCongressRepository();
    });

    blocTest(
      'CongressFetch event results in [InitialCongressState, CongressLoadingState, CongressLoadedState]',
      build: () {
        when(mockCongressRepository.getCongressPeople())
            .thenAnswer((_) => Future.value(congressPeople));
        return CongressBloc((mockCongressRepository));
      },
      act: (bloc) {
        bloc.add(CongressFetch());
        return;
      },
      expect: [
        InitialCongressState(),
        CongressLoadingState(),
        CongressLoadedState(congressPeople)
      ],
    );

    blocTest(
      'CongressFetch event results in [InitialCongressState, CongressLoadingState, CongressLoadedFailledState] when error happens',
      build: () {
        when(mockCongressRepository.getCongressPeople())
            .thenThrow(Exception('Opsie Dasie'));
        return CongressBloc((mockCongressRepository));
      },
      act: (bloc) {
        bloc.add(CongressFetch());
        return;
      },
      expect: [
        InitialCongressState(),
        CongressLoadingState(),
        CongressLoadedFailledState('Exception: Opsie Dasie')
      ],
    );
  });
}

Also (tests still pass without this) but I noticed that you're not passing all props in CongressmanModel

@override
List<Object> get props => [id];

Hope that helps 👍

from equatable.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.