when top-level property

When<T> Function<T>(T x()) get when

Create a stub method response.

Call a method on a mock object within the call to when, and call a canned response method on the result. For example:

when(() => cat.eatFood("fish")).thenReturn(true);

Mocktail will store the fake call to cat.eatFood, and pair the exact arguments given with the response. When cat.eatFood is called outside a when or verify context (a call "for real"), Mocktail will respond with the stored canned response, if it can match the mock method parameters.

The response generators include thenReturn, thenAnswer, and thenThrow.

See the README for more information.

Implementation

When<T> Function<T>(T Function() x) get when {
  if (_whenCall != null) {
    throw StateError('Cannot call `when` within a stub response');
  }
  _whenInProgress = true;
  return <T>(T Function() fn) {
    try {
      fn();
    } catch (e) {
      if (e is! TypeError) rethrow;
    }
    _whenInProgress = false;
    return When<T>();
  };
}