A stand-in for another object which cannot be used except for specifically overridden methods.
A fake has a default behavior for every field and method of throwing UnimplementedError. Fields and methods that are exercised 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.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
override
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited