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-onlyoverride
- runtimeType → Type
-
A representation of the runtime type of the object.
read-onlyinherited
Methods
-
noSuchMethod(
Invocation invocation, {Object? returnValue, Object? returnValueForMissingStub = deferToDefaultResponse}) → dynamic -
Handles method stubbing, method call verification, and real method calls.
override
-
toString(
) → String -
A string representation of this object.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
override
Constants
- deferToDefaultResponse → const Object
-
A sentinal value used as the default argument for noSuchMethod's
'returnValueForMissingStub' parameter.
Object()