Dart API ReferenceunittestBehavior

Behavior Class

A Behavior represents how a Mock will respond to one particular type of method call.

Constructors

Code new Behavior(CallMatcher matcher) #

Behavior (this.matcher) {
  actions = new List<Responder>();
}

Methods

Code Behavior alwaysCall(value) #

Creates a repeating proxy call.

Behavior alwaysCall(value) {
  return thenCall(value, 0);
}

Code Behavior alwaysReturn(value) #

Adds a Responder that repeatedly returns a value.

Behavior alwaysReturn(value) {
  return thenReturn(value, 0);
}

Code Behavior alwaysThrow(value) #

Adds a Responder that throws value endlessly.

Behavior alwaysThrow(value) {
  return thenThrow(value, 0);
}

Code bool matches(String method, List args) #

Returns true if a method call matches the Behavior.

bool matches(String method, List args) => matcher.matches(method, args);

Code Behavior thenCall(value, [count = 1]) #

thenCall creates a proxy Responder, that is called count times (1 by default; 0 is used for unlimited calls, and is exposed as alwaysCall). value is the function that will be called with the same arguments that were passed to the mock. Proxies can be used to wrap real objects or to define more complex return/throw behavior. You could even (if you wanted) use proxies to emulate the behavior of thenReturn; e.g.:

m.when(callsTo('foo')).thenReturn(0)

is equivalent to:

m.when(callsTo('foo')).thenCall(() => 0)
Behavior thenCall(value, [count = 1]) {
  actions.add(new Responder(value, count, Action.PROXY));
  return this; // For chaining calls.
}

Code Behavior thenReturn(value, [count = 1]) #

Adds a Responder that returns a value for count calls (1 by default).

Behavior thenReturn(value, [count = 1]) {
  actions.add(new Responder(value, count, Action.RETURN));
  return this; // For chaining calls.
}

Code Behavior thenThrow(value, [count = 1]) #

Adds a Responder that throws [value] [count] times (1 by default).

Behavior thenThrow(value, [count = 1]) {
  actions.add(new Responder(value, count, Action.THROW));
  return this; // For chaining calls.
}

Code String toString() #

Returns the matcher's representation.

String toString() => matcher.toString();

Fields

Code List<Responder> actions #

List<Responder> actions;

Code CallMatcher matcher #

CallMatcher matcher;