Dart mocking function

How to check that the bullying function is triggered?

I found this example in Mocking with Dart - how can I verify that a function passed as a parameter was called? and tried to expand it to check if this function was called.

library test2;

import "package:unittest/unittest.dart";
import "package:mock/mock.dart";

class MockFunction extends Mock {
  call(int a, int b) => a + b;
}

void main() {
  test("aa", () {

    var mockf = new MockFunction();
    expect(mockf(1, 2), 3);
    mockf.getLogs(callsTo(1, 2)).verify(happenedOnce);
  });
}

It looks like the mockf.getLogs () structure is empty ...

+2
source share
1 answer

You must scoff at the methods and indicate their names in the log. Here's the working code:

library test2;

import "package:unittest/unittest.dart";
import "package:mock/mock.dart";

class MockFunction extends Mock {
  MockFunction(){
    when(callsTo('call')).alwaysCall(this.foo);
  }
  foo(int a, int b) {
    return a + b;
    }
}

void main() {
  test("aa", () {    
    var mockf = new MockFunction();
    expect(mockf(1, 2), 3);
    mockf.calls('call', 1, 2).verify(happenedOnce);
  });
}

change answer a similar question: Dart How to mock a procedure

+1
source

Source: https://habr.com/ru/post/1542445/


All Articles