mocktail 1.0.4 copy "mocktail: ^1.0.4" to clipboard
mocktail: ^1.0.4 copied to clipboard

A Dart mock library which simplifies mocking with null safety support and no manual mocks or code generation.

example/main.dart

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

class Food {}

class Chicken extends Food {}

class Tuna extends Food {}

// A Real Cat class
class Cat {
  String sound() => 'meow!';
  bool likes(String food, {bool isHungry = false}) => false;
  void eat<T extends Food>(T food) {}
  final int lives = 9;
}

// A Mock Cat class
class MockCat extends Mock implements Cat {}

void main() {
  group('Cat', () {
    setUpAll(() {
      // Register fallback values when using
      // `any` or `captureAny` with custom objects.
      registerFallbackValue(Chicken());
      registerFallbackValue(Tuna());
    });

    late Cat cat;

    setUp(() {
      cat = MockCat();
    });

    test('example', () {
      // Stub a method before interacting with the mock.
      when(() => cat.sound()).thenReturn('purr');

      // Interact with the mock.
      expect(cat.sound(), 'purr');

      // Verify the interaction.
      verify(() => cat.sound()).called(1);

      // Stub a method with parameters
      when(
        () => cat.likes('fish', isHungry: any(named: 'isHungry')),
      ).thenReturn(true);
      expect(cat.likes('fish', isHungry: true), isTrue);

      // Verify the interaction.
      verify(() => cat.likes('fish', isHungry: true)).called(1);

      // Interact with the mock.
      cat
        ..eat(Chicken())
        ..eat(Tuna());

      // Verify the interaction with specific type arguments.
      verify(() => cat.eat<Chicken>(any())).called(1);
      verify(() => cat.eat<Tuna>(any())).called(1);
      verifyNever(() => cat.eat<Food>(any()));
    });
  });
}
1.1k
likes
160
points
2.76M
downloads

Publisher

verified publisherfelangel.dev

Weekly Downloads

A Dart mock library which simplifies mocking with null safety support and no manual mocks or code generation.

Homepage
Repository (GitHub)
View/report issues

Topics

#mock #testing

Documentation

API reference

License

MIT (license)

Dependencies

collection, matcher, test_api

More

Packages that depend on mocktail