Options
All
  • Public
  • Public/Protected
  • All
Menu

Interface SinonSpy<TArgs, TReturnValue>

Type parameters

  • TArgs: readonly any[] = any[]

  • TReturnValue = any

Hierarchy

Callable

  • SinonSpy(...args: TArgs): TReturnValue
  • Parameters

    • Rest ...args: TArgs

    Returns TReturnValue

Index

Properties

args

args: TArgs[]

Array of arguments received, spy.args[0] is an array of arguments received in the first call.

callCount

callCount: number

The number of recorded calls.

called

called: boolean

true if the spy was called at least once

calledOnce

calledOnce: boolean

true if spy was called exactly once

calledThrice

calledThrice: boolean

true if the spy was called exactly thrice

calledTwice

calledTwice: boolean

true if the spy was called exactly twice

exceptions

exceptions: any[]

Array of exception objects thrown, spy.exceptions[0] is the exception thrown by the first call. If the call did not throw an error, the value at the call’s location in .exceptions will be undefined.

firstCall

firstCall: SinonSpyCall<TArgs, TReturnValue>

The first call

lastCall

lastCall: SinonSpyCall<TArgs, TReturnValue>

The last call

notCalled

notCalled: boolean

true if the spy was not called

returnValues

returnValues: TReturnValue[]

Array of return values, spy.returnValues[0] is the return value of the first call. If the call did not explicitly return a value, the value at the call’s location in .returnValues will be undefined.

secondCall

secondCall: SinonSpyCall<TArgs, TReturnValue>

The second call

thirdCall

thirdCall: SinonSpyCall<TArgs, TReturnValue>

The third call

thisValues

thisValues: any[]

Array of this objects, spy.thisValues[0] is the this object for the first call.

Methods

alwaysCalledOn

  • alwaysCalledOn(obj: any): boolean
  • Returns true if the spy was always called with @param obj as this.

    Parameters

    • obj: any

    Returns boolean

alwaysCalledWith

  • Returns true if spy was always called with the provided arguments (and possibly others).

    Parameters

    Returns boolean

alwaysCalledWithExactly

  • Returns true if spy was always called with the exact provided arguments.

    Parameters

    Returns boolean

alwaysCalledWithMatch

  • alwaysCalledWithMatch(...args: TArgs): boolean
  • Returns true if spy was always called with matching arguments (and possibly others). This behaves the same as spy.alwaysCalledWith(sinon.match(arg1), sinon.match(arg2), ...).

    Parameters

    • Rest ...args: TArgs

    Returns boolean

alwaysReturned

  • alwaysReturned(obj: any): boolean
  • Returns true if spy always returned the provided value.

    Parameters

    • obj: any

    Returns boolean

alwaysThrew

  • alwaysThrew(): boolean
  • alwaysThrew(type: string): boolean
  • alwaysThrew(obj: any): boolean
  • Returns true if spy always threw an exception.

    Returns boolean

  • Returns true if spy always threw an exception of the provided type.

    Parameters

    • type: string

    Returns boolean

  • Returns true if spy always threw the provided exception object.

    Parameters

    • obj: any

    Returns boolean

callArg

  • callArg(pos: number): unknown[]
  • Like yield, but with an explicit argument number specifying which callback to call. Useful if a function is called with more than one callback, and simply calling the first callback is not desired.

    Parameters

    • pos: number

    Returns unknown[]

callArgOn

  • callArgOn(pos: number, obj: any, ...args: any[]): unknown[]
  • Parameters

    • pos: number
    • obj: any
    • Rest ...args: any[]

    Returns unknown[]

callArgOnWith

  • callArgOnWith(pos: number, obj: any, ...args: any[]): unknown[]
  • Parameters

    • pos: number
    • obj: any
    • Rest ...args: any[]

    Returns unknown[]

callArgWith

  • callArgWith(pos: number, ...args: any[]): unknown[]
  • Like callArg, but with arguments.

    Parameters

    • pos: number
    • Rest ...args: any[]

    Returns unknown[]

calledAfter

  • calledAfter(anotherSpy: SinonSpy<any, any>): boolean
  • Returns true if the spy was called after @param anotherSpy

    Parameters

    Returns boolean

calledBefore

  • calledBefore(anotherSpy: SinonSpy<any, any>): boolean
  • Returns true if the spy was called before @param anotherSpy

    Parameters

    Returns boolean

calledImmediatelyAfter

  • calledImmediatelyAfter(anotherSpy: SinonSpy<any, any>): boolean
  • Returns true if spy was called after @param anotherSpy, and no spy calls occurred between @param anotherSpy and spy.

    Parameters

    Returns boolean

calledImmediatelyBefore

  • calledImmediatelyBefore(anotherSpy: SinonSpy<any, any>): boolean
  • Returns true if spy was called before @param anotherSpy, and no spy calls occurred between spy and @param anotherSpy.

    Parameters

    Returns boolean

calledOn

  • calledOn(obj: any): boolean
  • Returns true if the spy was called at least once with @param obj as this. calledOn also accepts a matcher spyCall.calledOn(sinon.match(fn)) (see matchers).

    Parameters

    • obj: any

    Returns boolean

calledOnceWith

  • Returns true if spy was called at exactly once with the provided arguments.

    Parameters

    Returns boolean

calledOnceWithExactly

  • Parameters

    Returns boolean

calledWith

  • Returns true if spy was called at least once with the provided arguments. Can be used for partial matching, Sinon only checks the provided arguments against actual arguments, so a call that received the provided arguments (in the same spots) and possibly others as well will return true.

    Parameters

    Returns boolean

calledWithExactly

  • Returns true if spy was called at least once with the provided arguments and no others.

    Parameters

    Returns boolean

calledWithMatch

  • Returns true if spy was called with matching arguments (and possibly others). This behaves the same as spy.calledWith(sinon.match(arg1), sinon.match(arg2), ...).

    Parameters

    Returns boolean

calledWithNew

  • calledWithNew(): boolean
  • Returns true if spy/stub was called the new operator. Beware that this is inferred based on the value of the this object and the spy function’s prototype, so it may give false positives if you actively return the right kind of object.

    Returns boolean

getCall

  • Returns the nth call. Accessing individual calls helps with more detailed behavior verification when the spy is called more than once.

    Parameters

    • n: number

      Zero based index of the spy call.

    Returns SinonSpyCall<TArgs, TReturnValue>

getCalls

  • Returns an Array of all calls recorded by the spy.

    Returns SinonSpyCall<TArgs, TReturnValue>[]

invokeCallback

  • invokeCallback(...args: TArgs): void
  • Invoke callbacks passed to the stub with the given arguments. If the stub was never called with a function argument, yield throws an error. Returns an Array with all callbacks return values in the order they were called, if no error is thrown.

    Parameters

    • Rest ...args: TArgs

    Returns void

named

  • named(name: string): SinonSpy<TArgs, TReturnValue>
  • Set the displayName of the spy or stub.

    Parameters

    • name: string

    Returns SinonSpy<TArgs, TReturnValue>

neverCalledWith

  • Returns true if the spy/stub was never called with the provided arguments.

    Parameters

    Returns boolean

neverCalledWithMatch

  • neverCalledWithMatch(...args: TArgs): boolean
  • Returns true if the spy/stub was never called with matching arguments. This behaves the same as spy.neverCalledWith(sinon.match(arg1), sinon.match(arg2), ...).

    Parameters

    • Rest ...args: TArgs

    Returns boolean

notCalledWith

  • Returns true if call did not receive provided arguments.

    Parameters

    Returns boolean

notCalledWithMatch

  • Returns true if call did not receive matching arguments. This behaves the same as spyCall.notCalledWith(sinon.match(arg1), sinon.match(arg2), ...).

    Parameters

    Returns boolean

printf

  • printf(format: string, ...args: any[]): string
  • Returns the passed format string with the following replacements performed:

    • %n - the name of the spy "spy" by default)
    • %c - the number of times the spy was called, in words ("once", "twice", etc.)
    • %C - a list of string representations of the calls to the spy, with each call prefixed by a newline and four spaces
    • %t - a comma-delimited list of this values the spy was called on
    • %n - the formatted value of the nth argument passed to printf
    • %* - a comma-delimited list of the (non-format string) arguments passed to printf
    • %D - a multi-line list of the arguments received by all calls to the spy

    Parameters

    • format: string
    • Rest ...args: any[]

    Returns string

resetHistory

  • resetHistory(): void
  • Resets the state of a spy.

    Returns void

restore

  • restore(): void
  • Replaces the spy with the original method. Only available if the spy replaced an existing method.

    Returns void

returned

  • Returns true if spy returned the provided value at least once. Uses deep comparison for objects and arrays. Use spy.returned(sinon.match.same(obj)) for strict comparison (see matchers).

    Parameters

    Returns boolean

threw

  • threw(): boolean
  • threw(type: string): boolean
  • threw(obj: any): boolean
  • Returns true if spy threw an exception at least once.

    Returns boolean

  • Returns true if spy threw an exception of the provided type at least once.

    Parameters

    • type: string

    Returns boolean

  • Returns true if spy threw the provided exception object at least once.

    Parameters

    • obj: any

    Returns boolean

withArgs

  • Creates a spy that only records calls when the received arguments match those passed to withArgs. This is useful to be more expressive in your assertions, where you can access the spy with the same call.

    Parameters

    Returns SinonSpy<TArgs, TReturnValue>

wrappedMethod

  • wrappedMethod(...args: TArgs): TReturnValue
  • Holds a reference to the original method/function this stub has wrapped.

    Parameters

    • Rest ...args: TArgs

    Returns TReturnValue

yield

  • yield(...args: any[]): unknown[]
  • Invoke callbacks passed to the stub with the given arguments. If the stub was never called with a function argument, yield throws an error. Returns an Array with all callbacks return values in the order they were called, if no error is thrown. Also aliased as invokeCallback.

    Parameters

    • Rest ...args: any[]

    Returns unknown[]

yieldOn

  • yieldOn(obj: any, ...args: any[]): unknown[]
  • Parameters

    • obj: any
    • Rest ...args: any[]

    Returns unknown[]

yieldTo

  • yieldTo(property: string, ...args: any[]): unknown[]
  • Invokes callbacks passed as a property of an object to the stub. Like yield, yieldTo grabs the first matching argument, finds the callback and calls it with the (optional) arguments.

    Parameters

    • property: string
    • Rest ...args: any[]

    Returns unknown[]

yieldToOn

  • yieldToOn(property: string, obj: any, ...args: any[]): unknown[]
  • Parameters

    • property: string
    • obj: any
    • Rest ...args: any[]

    Returns unknown[]