1

Dart test code:

_doSomething2(callback(int x, int y)) {
    callback(1, 2);
}

test('async test, check a function with 2 parameters', () {
    new Timer(new Duration(milliseconds:100), _doSomething2(expectAsync2((x, y) {
        expect(x, equals(1));
        expect(y, equals(2));
    })));
});

When I run it in Intellij-IDEA as "unittest", it passed, but there is some error message shown:

Testing started at PM11:08 ...
Unhandled exception:
The null object does not have a method 'call'.

NoSuchMethodError : method not found: 'call'
Receiver: null
Arguments: []
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:45)
#1      _createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:11)
#2      _handleTimeout (timer_impl.dart:283)
#3      _handleTimeout (timer_impl.dart:292)
#4      _handleTimeout (timer_impl.dart:292)
#5      _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:115)

Process finished with exit code 255

Where is wrong?

1 Answer 1

2

The test ends before the code inside the new Timer() is executed.

void main(List<String> args) {
  test('async test, check a function with 2 parameters', () {
      var callback = expectAsync0(() {});
      new Timer(new Duration(milliseconds:100), () {
          _doSomething2((x, y) {
          expect(x, equals(1));
          expect(y, equals(2));
          callback();
      });
    });
  });
}

This way the test doesn't end until callback is called.

4
  • Sorry I missed that you already use expectAsnyc2. I'll take a look. Jan 29, 2014 at 10:30
  • @Freewind I updated my answer. Now I tested it, and it works. The way you did it, the callback is only created after the delay but then the test may already have ended. Jan 29, 2014 at 10:37
  • Thank you, it's worked now. But it's funny that I was trying the method expectAsync2 when I write this code.
    – Freewind
    Jan 30, 2014 at 15:04
  • New question for this: stackoverflow.com/questions/21460073/…
    – Freewind
    Jan 30, 2014 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.