Bunoon: JUST | Manual | Mocking

Below is a list of the functions that are available in the mocking library, which can be used within tests, or elsewhere. The mocking library will allow specific functions to be mocked, replacing them with an empty function which will be called in its place. The number of times the function is called, the arguments it called with and where are all tracked.

Creates a mock function that will be called in the place of another (arguments and called triggers are tracked).

The main arguments for this function are as follows:

  • referenceName [String] - The reference name to store this mock function under (used for call checks)..
  • functionReference [Object] - The function reference that is to be replaced with the mock function (defaults to null).

The return value for this function is as follows:

  • [Object] - A mock function object (or the original if already created).

Below is an example of how to use this function:

var test = new $T.Class.New( {

_inherit_: new $J.TestCase(),


test: function () {

var newFunction = $MO.GetMockFunction( "TestFunction" );

newFunction();

}

} );

Clears all the referenced mock functions (all originals ones are reset) and tracked call arguments.

Below is an example of how to use this function:

var test = new $T.Class.New( {

_inherit_: new $J.TestCase(),


test: function () {

$MO.ClearAll();

}

} );

Determines if a mock function has been called (must have a mock object set).

The main arguments for this function are as follows:

  • referenceName [String] - The reference name that the mock function was stored under.

The return value for this function is as follows:

  • [Boolean] - A boolean of true/false to indicate if the mock function has been called.

Below is an example of how to use this function:

var test = new $T.Class.New( {

_inherit_: new $J.TestCase(),


test: function () {

var newFunction = $MO.GetMockFunction( "TestFunction" );

newFunction();

this.Assert.True( $MO.Called( "TestFunction" ) );

}

} );

Determines if a mock function has been called (must have a mock object set) 'x' number of times.

The main arguments for this function are as follows:

  • referenceName [String] - The reference name that the mock function was stored under.
  • calledCount [Number] - The number of times the function should of been called.

The return value for this function is as follows:

  • [Boolean] - A boolean of true/false to indicate if the mock function has been called 'x' number of times.

Below is an example of how to use this function:

var test = new $T.Class.New( {

_inherit_: new $J.TestCase(),


test: function () {

var newFunction = $MO.GetMockFunction( "TestFunction" );

newFunction();

newFunction();

newFunction();

this.Assert.True( $MO.CalledXTimes( "TestFunction", 3 ) );

}

} );

Determines if a mock function has been called (must have a mock object set) with a specific set of arguments.

The main arguments for this function are as follows:

  • referenceName [String] - The reference name that the mock function was stored under.

The return value for this function is as follows:

  • [Boolean] - A boolean of true/false to indicate if the mock function has been called with the arguments.

Below is an example of how to use this function:

var test = new $T.Class.New( {

_inherit_: new $J.TestCase(),


test: function () {

var newFunction = $MO.GetMockFunction( "TestFunction" );

newFunction( 1, 2, 3, 4 );

newFunction( 5, 6, 7, 8 );

newFunction( 9, 10, 11, 12 );

this.Assert.True( $MO.CalledWithArguments( "TestFunction", 1, 2, 3, 4 ) );

this.Assert.True( $MO.CalledWithArguments( "TestFunction", 5, 6, 7, 8 ) );

this.Assert.True( $MO.CalledWithArguments( "TestFunction", 9, 10, 11, 12 ) );

}

} );

Determines if a function (by reference name of function reference) has been mocked.

The main arguments for this function are as follows:

  • reference [Object] - The reference to the function (either string reference name of function reference).

The return value for this function is as follows:

  • [Boolean] - A boolean of true/false to indicate if the function has been mocked.

Below is an example of how to use this function:

var test = new $T.Class.New( {

_inherit_: new $J.TestCase(),


test: function () {

var newFunction = $MO.GetMockFunction( "TestFunction" );

newFunction();

this.Assert.True( $MO.IsMocked( TestFunction ) );

}

} );

Determines if a function matches an existing mocked function (by reference name).

The main arguments for this function are as follows:

  • referenceName [String] - The reference name that the mock function was stored under.
  • functionReference [Object] - The function reference that is to be checked against the mock function.

The return value for this function is as follows:

  • [Boolean] - A boolean of true/false to indicate if the function matches the specified mock function.

Below is an example of how to use this function:

var test = new $T.Class.New( {

_inherit_: new $J.TestCase(),


test: function () {

var newFunction = $MO.GetMockFunction( "TestFunction" );

newFunction();

this.Assert.True( $MO.IsMockMatch( "TestFunction", TestFunction ), );

}

} );