Xerox
Xerox is a unit testing mock framework.
Usage
Xerox is best used with a dependency manager like proxyquire. To use, simply include in your test file and create a mocked class.
// mymock.jsvar Xerox = ; var mock = 'MyMock'; moduleexports = { return mock; } proxy: ;
Then use proxyquire to inject MyMock
as a dependency in your test file.
// mytest.jsvar proxyquire = ;var mymock = ; var mymodule = ;...
Then use mymock.mock
function to mock a specific function exported by the mocked module.
... { ...}var myfunc = mymock; myfunc;mymodule;
Where the module under test may look like the following:
// mymodule.jsvar Adder = ; var adder = ; moduleexports = { var value = adder; console; };
Documentation
Xerox
Class that manages a mock document.
Xerox
Xerox Document
constructor.
Parameters:
name
<String
>: Name of the class to mock.
Xerox#copy(test, method)
Creates new template to mock on the provided Document
with a given name.
Parameters:
test
<Function
,null
>: callback function to test values.expected
: expected valueactual
: actual valuemsg
: optional message to provide
method
<String
>: method name to be added to the prototype.- If method is
'@construct'
then the constructor for this class is mocked.
- If method is
Returns:
- <
Template
>: Template to set mock parameters.
Template
Instances of Template are returned by Xerox#copy
.
Template#expects([arg1, [arg2... [argN]]])
Sets the arguments the next invocation of the method will expect.
Parameters:
arg
: Value to expect in next invocation. Will be used withtest
.
Returns:
- <
Template
>: Template it was invoked on to allow chaining.
Template#throws(err)
Sets err that the next invocation will throw after parameters have been verified.
Parameters:
err
: The error to be thrown.
Returns:
- <
Template
>: Template it was invoked on to allow chaining.
Template#sets(property, value)
Sets a property with the given name and value to the object this object was invoked on.
Parameters:
property
: The property name to be set.value
: The value for the property to be set.
Returns:
- <
Template
>: Template it was invoked on to allow chaining.
Template#yields(value)
Sets value that will be returned from next invocation.
Parameters:
value
: The value to be returned.
Returns:
- <
Template
>: Template it was invoked on to allow chaining.
Template#callback(err, value)
Sets callback parameters that will be called from next invocation. If the last parameter of the mocked function is a function then it is used as a callback with the given parameters.
Parameters:
err
: The error parameter to be used with the next callback.value
: The value to be used with the callback.
Returns:
- <
Template
>: Template it was invoked on to allow chaining.
Template#calls(method)
Adds a function to be called to a queue. Each method will be called when the mocked function is invoked.
Parameters:
method
<Function
>: The function to be called on invocation.
Returns:
- <
Template
>: Template it was invoked on to allow chaining.
Template#then()
Returns a new instance of the invocation to test. This instance will be used on the invocation after the previous instance.
Returns:
- <
Template
>: A new template to modify. Allows chaining.
License
MIT