Mock class

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

A mocked class implements all fields and methods with a default implementation that does not throw a NoSuchMethodError, and may be further customized at runtime to define how it may behave using when.

Example use:

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

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

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

  // When 'getSound' is called, return 'Woof'
  when(cat.getSound(any)).thenReturn('Woof');

  // Try making a Cat sound...
  print(cat.getSound('foo')); // Prints 'Woof'
}

A class which extends Mock should not have any directly implemented overridden fields or methods. These fields would not be usable as a Mock with verify or when. To implement a subset of an interface manually use Fake instead.

WARNING: Mock uses noSuchMethod, which is a form of runtime reflection, and causes sub-standard code to be generated. As such, Mock 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

Mock()

Properties

hashCode int
The hash code for this object. [...]
read-only, override
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. [...]
@visibleForTesting, override
toString() String
Returns a string representation of this object.
override

Operators

operator ==(dynamic other) bool
The equality operator. [...]
override