Seth Ladd Seth Ladd - 7 months ago 42
Dart Question

How do I mock or verify a call to print, in Dart unit tests?

In my Dart unit tests, how do I verify that print was called?

I'm writing sample code for tutorials, and I want to test it. Many samples using

for simplicity. I'd like my unit tests to verify that print is called with the right input.



I don't think unittest adds anything specific for this, but you can override any top-level function in the scope of your test and capture calls to a log, for example:

var printLog = [];
void print(String s) => printLog.add(s);

main() {
  test('print', () {
    expect(printLog.length, 2);
    expect(printLog[0], contains('hello'));
    // etc...