Giter Site home page Giter Site logo

mocktail's Introduction

๐Ÿน mocktail

Pub build coverage License: MIT


Mock library for Dart inspired by mockito.

Mocktail focuses on providing a familiar, simple API for creating mocks in Dart (with null-safety) without the need for manual mocks or code generation.

Creating a Mock

import 'package:mocktail/mocktail.dart';

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

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

void main() {
  // Create a Mock Cat instance
  final cat = MockCat();
}

Stub and Verify Behavior

The MockCat instance can then be used to stub and verify calls.

// Stub the `sound` method.
when(cat).calls(#sound).thenReturn('meow');

// Verify no interactions have occurred.
verify(cat).called(#sound).never();

// Interact with the mock cat instance.
cat.sound();

// Verify the interaction occurred.
verify(cat).called(#sound).once();

// Interact with the mock instance again.
cat.sound();

// Verify the interaction occurred twice.
verify(cat).called(#sound).times(2);

Additional Usage

// Stub a method before interacting with the mock.
when(cat).calls(#sound).thenReturn('purrr!');
expect(cat.sound(), 'purrr!');

// You can interact with the mock multiple times.
expect(cat.sound(), 'purrr!');

// You can change the stub.
when(cat).calls(#sound).thenReturn('meow');
expect(cat.sound(), 'meow');

// You can stub getters.
when(cat).calls(#lives).thenReturn(10);
expect(cat.lives, 10);

// You can stub a method for specific arguments.
when(cat).calls(#likes).withArgs(
  positional: ['fish'],
  named: {#isHungry: false},
).thenReturn(true);
expect(cat.likes('fish'), isTrue);

// You can verify the interaction for specific arguments.
verify(cat).called(#likes).withArgs(
  positional: ['fish'],
  named: {#isHungry: false},
).times(1);

// You can stub a method using argument matchers: `any` or `anyThat`.
when(cat).calls(#likes).withArgs(
  positional: [any],
  named: {#isHungry: anyThat(isFalse)},
).thenReturn(true);
expect(cat.likes('fish'), isTrue);

// You can stub a method to throw.
when(cat).calls(#sound).thenThrow(Exception('oops'));
expect(() => cat.sound(), throwsA(isA<Exception>()));

// You can calculate stubs dynamically.
final sounds = ['purrr', 'meow'];
when(cat).calls(#sound).thenAnswer((_) => sounds.removeAt(0));
expect(cat.sound(), 'purrr');
expect(cat.sound(), 'meow');

// You can capture any argument.
when(cat).calls(#likes).thenReturn(true);
expect(cat.likes('fish'), isTrue);
final captured = verify(cat).called(#likes)
  .withArgs(positional: [captureAny]).captured;
expect(captured.last, equals(['fish']));

// You can capture a specific argument based on a matcher.
when(cat).calls(#likes).thenReturn(true);
expect(cat.likes('fish'), isTrue);
expect(cat.likes('dog food'), isTrue);
final captured = verify(cat).called(#likes)
  .withArgs(positional: [captureAnyThat(startsWith('d'))]).captured;
expect(captured.last, equals(['dog food']));

Resetting Mocks

reset(cat); // Reset stubs and interactions

mocktail's People

Contributors

felangel avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.