Fake class

Extend or mixin this class to mark the implementation as a Fake.

A fake has a default behavior for every field and method of throwing UnimplementedError. Fields and methods that are excersized by the code under test should be manually overridden in the implementing class.

A fake does not have any support for verification or defining behavior from the test, it cannot be used as a Mock.

In most cases a shared full fake implementation without a noSuchMethod is preferable to extends Fake, however extends Fake is preferred against extends Mock mixed with manual @override implementations.

Example use:

// Real class.
class Cat {
  String meow(String suffix) => 'Meow$suffix';
  String hiss(String suffix) => 'Hiss$suffix';
}

// Fake class.
class FakeCat extends Fake implements Cat {
  @override
  String meow(String suffix) => 'FakeMeow$suffix';
}

void main() {
  // Create a new fake Cat at runtime.
  var cat = new FakeCat();

  // Try making a Cat sound...
  print(cat.meow('foo')); // Prints 'FakeMeowfoo'
  print(cat.hiss('foo')); // Throws
}

WARNING: Fake uses noSuchMethod, which is a form of runtime reflection, and causes sub-standard code to be generated. As such, Fake should strictly not be used in any production code, especially if used within the context of Dart for Web (dart2js, DDC) and Dart for Mobile (Flutter).

Constructors

Fake()

Properties

hashCode → int
The hash code for this object.
read-only, inherited
runtimeType → Type
A representation of the runtime type of the object.
read-only, inherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
toString() → String
Returns a string representation of this object.
inherited

Operators

operator ==(dynamic other) → bool
The equality operator.
inherited