Dart Unit Test – Always passing

Issue

All,

Here is a unit test for checking the size of a collection

main() {
  test("Resource Manager Image Load", () {
    ResourceManager rm = new ResourceManager();
    int WRONG_SIZE = 1000000;

    rm.loadImageManifest("data/rm/test_images.yaml").then((_){
      print("Length="+ rm.images.length.toString()); // PRINTS '6' - WHICH IS CORRECT
      expect(rm.images, hasLength(WRONG_SIZE));
    });
  });
}

I am running this from a browser (client-side Dart libraries are in use) and it ALWAYS passes, no matter what the value of WRONG_SIZE.

Help appreciated.

Solution

In such simple cases you can just return the future. The unit test framework recognizes it and waits for the future to complete. This also works for setUp/tearDown.

main() {
  test("Resource Manager Image Load", () {
    ResourceManager rm = new ResourceManager();
    int WRONG_SIZE = 1000000;

    return rm.loadImageManifest("data/rm/test_images.yaml").then((_) {
    //^^^^
      print("Length="+ rm.images.length.toString()); // PRINTS '6' - WHICH IS CORRECT
      expect(rm.images, hasLength(WRONG_SIZE));
    });
  });
}

Answered By – Günter Zöchbauer

Answer Checked By – Dawn Plyler (FlutterFixes Volunteer)

Leave a Reply

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