2

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.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
NullPumpkinException
  • 1,396
  • 1
  • 18
  • 22

2 Answers2

2

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));
    });
  });
}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
1

The problem is that your code returns a Future, and your test completes before the code in the Future has finished, so there's nothing to cause it to fail.

Check out the Asynchronous Tests section on the Dart site. There are methods like expectAsync that allow the future to be passed to the test framework so that it can wait for them to complete and handle the result correctly.

Here's an example (note the expect call is now inside the function passed to expectAsync)

test('callback is executed once', () {
  // wrap the callback of an asynchronous call with [expectAsync] if
  // the callback takes 0 arguments...
  var timer = Timer.run(expectAsync(() {
    int x = 2 + 3;
    expect(x, equals(5));
  }));
});
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
Danny Tuppeny
  • 40,147
  • 24
  • 151
  • 275