mocktail icon indicating copy to clipboard operation
mocktail copied to clipboard

Unable to mock functions with functions as argument

Open Avendo07 opened this issue 1 year ago • 1 comments

Describe the bug Unable to mock and/or verify functions which take in function/callback as an argument

Reproducible Code

import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

class MockMockableClass extends Mock implements MockableClass {}

class MockableClass {
  bool inputCallback(bool Function() query) {
    bool val = query();
    return (!val);
  }
}

class TestClass {
  bool hello(MockableClass mockClass) {
    bool val = mockClass.inputCallback(() => true);
    return val;
  }
}

void main() {
  late MockMockableClass mockMockClass;
  mockMockClass = MockMockableClass();
  test("test for mocktail", () {
    when(() => mockMockClass.inputCallback((() => true))).thenReturn(false);
    TestClass testClass = TestClass();
    bool val = testClass.hello(mockMockClass);
    expect(val, false);
  });
}

Expected behavior The above code shows "type 'Null' is not a subtype of type 'bool'". Whereas it should have mocked the function. This works if I replace the function (i.e. () => true) with "any()", but then I am unable to verify if this function was triggered or not.

Am I doing something wrong or is there a workaround to mock the function and verify if the "inputCallback" function was triggered or not.

Avendo07 avatar Aug 26 '22 10:08 Avendo07