Why expect(Future.value(true), Future.value(true)) has fail result in flutter test?


So I feel a bit confused with this matcher.

Why does this test returning fail?


I thought it was the same value and the matcher should be returning success for that test.


They are different objects which are compared using the hashcode operator ==, not by hashCode. However, the default operator == implementation compares object identity. Which will be different because they are not the same object.

If you await the futures the values will be compared and it will pass.

  test('test', () async {
    expect(await Future.value(true), await Future.value(true));


To answer your other question:

Do you know how to compare 2 same classes? I just try expect(ClassA(), ClassA()) still returning fail test. How to make expect(ClassA(), ClassA()) to pass?

You’re kind of using the expect in the wrong way. If you want to know if the class is a specific kind you can do this:

test('test', () async {
    final ClassA classAInstance = ClassA();
    expect(classAInstance, isA<ClassA>());
    expect(classAInstance == classAInstance, true);

Thank you jamesdlin for correcting me on the hashcode.

Answered By – Er1

Answer Checked By – Timothy Miller (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *