MockService<Value> class

A test-only ServiceBase that returns a predetermined Result.

Use MockService.success or MockService.failure to stub out a service dependency without invoking the real implementation. Inject the mock wherever the real service would go by typing dependencies as ServiceBase<Value>:

class OrderOrchestrator {
  const OrderOrchestrator({required this.userService});

  final ServiceBase<User> userService; // injectable — real or mock

  Result<Order> run() =>
      userService
          .call()
          .andThen((user) => OrderCreateService(user: user).call());
}

// In tests:
final orchestrator = OrderOrchestrator(
  userService: MockService.success('userCreated', alice),
);

All three constructors are available:

// From a ready-made Result — useful when reusing a result across assertions
MockService<User>(Success(['ok', 'cached'], alice))

// Shorthand for a successful result
MockService<User>.success('userCreated', alice)
MockService<User>.success(['ok', 'cached'], alice)

// Shorthand for a failed result
MockService<User>.failure('unauthorized')
MockService<User>.failure('validationFailed', errors)
MockService<User>.failure(['unprocessableContent', 'clientError'], response)

See also ServiceBase for the real service contract.

Inheritance

Constructors

MockService(Result<Value> _result)
Creates a mock that always returns the given Result on every call.
MockService.failure(Object? outcomes, [Object? context])
Creates a mock that fails with outcomes and optional context.
MockService.success(Object? outcomes, Value value)
Creates a mock that succeeds with outcomes and value.

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

call() Result<Value>
Invokes run, allowing the service to be called like a function.
inherited
check<CheckedValue>(Object? outcomes, CheckedValue data, bool condition()) Result<CheckedValue>
Validates a condition inline, carrying data on both paths.
inherited
failure(Object? outcomes, [Object? context]) Failure<Value>
Signals that the service could not complete its work.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
run() Result<Value>
Implements the business logic as a pipeline of steps.
override
success(Object? outcomes, Value value) Success<Value>
Signals that this step of the service completed successfully.
inherited
toString() String
A string representation of this object.
inherited
tryRun(Object? outcomes, Value operation(), {Object? onException(Object exception, StackTrace stack)?}) Result<Value>
Runs operation and wraps any thrown exception as a Failure.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited