nano_mock 1.0.2 copy "nano_mock: ^1.0.2" to clipboard
nano_mock: ^1.0.2 copied to clipboard

A lightweight mock library without any dependencies or requirements to use code generation.

example/main.dart

import 'package:test/test.dart';

import '../lib/nano_mock.dart';

/// An example of an implemented function accepting a callback that is
/// optionally called.
T? _exampleImplementation<T>(int x, int y, T Function(int x, int y) callback) {
  if (x + y > 10) {
    return callback(x, y);
  } else {
    return null;
  }
}

void main() {
  group("_exampleImplementation", () {
    test("the function calls the callback when the sum is greater than 10", () {
      // Create a NanoMock instance.
      final mock = NanoMock<bool>();

      // Mock the instance to return true when called with 5 and 7.
      final verify = mock.when(true, [5, 7]);

      // Call the function with 5 and 7 and assert that true is returned.
      expect(
        _exampleImplementation(5, 7, (x, y) => mock([x, y])),
        true,
      );

      // Assert that the function has been called once.
      verify.called(1);
    });

    test("the function does not call the callback when the sum is less than 10",
        () {
      // Create a NanoMock instance.
      final mock = NanoMock<bool>();

      // Mock the instance to return false when called with 3 and 5.
      final verify = mock.when(false, [3, 5]);

      // Call the function with 3 and 5 and assert that null is returned.
      expect(
        _exampleImplementation(3, 5, (x, y) => mock([x, y])),
        null,
      );

      // Assert that the function has never been called.
      verify.neverCalled();
    });
  });
}
3
likes
130
pub points
18%
popularity

Publisher

verified publisheroborgen.se

A lightweight mock library without any dependencies or requirements to use code generation.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (LICENSE)

More

Packages that depend on nano_mock