mime/mime.js:109
Mime Class
Mime constructor. Takes an optional argument that implements a deepEqual comparison to be able to determine whether the arguments passed into the function call are equal. By default it uses the Node.js assert.deepEqual. You will want to override this if you have objects that do not serialize naturally
Constructor
mime/mime.js:109
Mime
( [f]
)
Parameters:
-
[f]
<Function>a deepEqual function that can be used to compare whether two object structures are equal
mime/mime.js:167
__logCall
( name, [restOfArguments]
) /* private method */
Log a call to a function
Parameters:
-
name
<String>- the name of the function
-
[restOfArguments]
<Array>- the argument array
mime/mime.js:148
__undefinedMethod__
(
) /* private method */
Internal method that captures calls to undefined functions into the log structure. It returns self so that calls to it can be chained and it can be used for mocking chained objects
mime/mime.js:462
_createClass
( constructor
)
Given a constructor function as an argument, create a new Class that inherits from the Mime class but whose instances' cal logs are shared with this instance's call log. This is useful when mocking a global or require class so that access can be gained to what happens to the instances of that class. The call log will also log the creation of instances under the symbol 'Class', so that the arguments to those calls can also be captured
Parameters:
-
constructor
<Function>- the constructor function
- the inherited and bound constructor
Example
var assert = require('assert'),
Cat, kitty, mongoose;
require('node-mimejs');
mongoose = new Mime();
// When mongoose.model is called, it returns a new class
mongoose._spy('model', function(name, defn) {
return this._createClass(function() {
});
});
Cat = mongoose.model('Cat', { name: String });
// Calling the class's constructor, will register a call against the "Class"
// "method" in the mongoose Mime instance
kitty = new Cat({ name: 'Zildjian' });
// Calling the child instance's functions will also register calls against
// the mongoose Mime instance
kitty.save(function (err) {
if (err) {
console.log('meow');
}
});
// test that with these asserts
describe('mongooseExample.js', function () {
it('Should have created the model', function () {
assert.ok(mongoose._wasCalledWithArguments('model', 'Cat', { name : String }),
'Should have called model');
});
it('Should have called the "Cat" constructor', function () {
assert.equal(mongoose._getCallArguments('Class', 0)[0].name, 'Zildjian',
'Should have created a Zildjian cat');
});
it('Should have called the "save" method', function () {
assert.equal(typeof mongoose._getCallArguments('save', 0)[0], 'function',
'Should have called with a callback');
});
});
mime/mime.js:304
_getAllCallArguments
( name
)
Given a function name return the array of all the calls to that function.
Parameters:
-
name
<String>- the name of the function
Example
var mime = new Mime();
mime.newFunction();
mime.newFunction(1, 2, 3);
assert.deepEqual(mime._getAllCallArguments('newFunction'), [[], [1, 2, 3]],
'Should return array of argument arrays');
mime/mime.js:270
_getCallArguments
( name, [index]
)
Given a function name and an index, returns the arguments of the call at that index. It returns undefined if there was no call at that index. It returns an empty array if there was a call with no arguments at that index. If no index is supplied, the first call (0-index) will be assumed
Parameters:
-
name
<String>- the name of the function
-
[index]
<Integer>- which call's arguments to return
Example
// The mime object was turned into a class factory using _createClass
it('Should have called the child class\'s constructor', function () {
assert.equal(mime._getCallArguments('Class', 0)[0].name, 'Zildjian',
'Should have created a Zildjian cat');
});
mime/mime.js:361
_mockModule
( name, methods
)
Given a module name and a set of methods, create an exports structure for a module with those methods and bind it to the this object. Put that structure into the global mocked modules registry
Parameters:
-
name
<String>- the name of the module as it will be required
-
methods
<ArrayString | Object>- if a String, the name of the method to export and bind, if an Object, its attributes' name will be exported, bound to the Mime instance and the function will also be called when the method is called
Example
// In your test file, you use Mime.getMockedModuleMime to obtain a Mime object for the http module
// This is required so that Node.js's module caching will not get in the way of multiple
// modules requiring the same dependency
httpMime = Mime.getMockedModuleMime('http');
// Then tell the mime instance which symbols to export
httpMime._mockModule('http', ['setHeader', 'write', 'end']);
// Then require the module with the dependency
dependency = require('../src/dependency.js');
// Now run the tests
...
mime/mime.js:549
_sandboxRequire
( path, require, [globals]
)
Given a test target path, require that module in a sandbox
Parameters:
-
path
<String>The name of the test target whose dependencies are to be intercepted
-
require
<Function>The test's require function (needed so that the test target can be resolved in the context of the test)
-
[globals]
<Object>optional argument of globals to expose to the sandboxed module
The exports of the test target
Example
mime = new Mime()
mime._mockModule('something', ['callSomeFunction']);
exports = mime._sandboxRequire('../../testdata/dep4', require);
...
run the tests
...
mime._unmockModule('something');
mime/mime.js:193
_spy
( name, [callback]
)
Create a function with a specific name that is bound to the Mime and return this function. This function can then be passed into a test target and will act as an argument Mime. You can test the arguments passed to these calls in the same way as calls to a mocked object function
Parameters:
-
name
<String>- the name you will use to test the arguments passed into the function when it was called
-
[callback]
<Function>- optional parameter with a function to call when this method is called
Example
// This will add a "model" function to the mime instance that mimics
// Mongoose's model function
mime._spy('model', function(name, schema) {
return this._createClass(function() {
// constructor function logic
...
});
});
mime/mime.js:421
_unmockModule
( name
)
Unregister the mocked exports for a module
Parameters:
-
name
<String>- the name of the module to unmock
mime/mime.js:228
_wasCalledWithArguments
( name, [arguments]
)
Returns true if the function with the name passed in as the first argument was called with the arguments passed in as the rest of the arguments. Returns false under all other conditions
Parameters:
-
name
<String>- the name of the function to test
-
[arguments]
<Anything>- the other arguments to test
Example
// The mime instance here was supplied as a mock for a mongoose module
it('Should have created the model', function () {
assert.ok(mime._wasCalledWithArguments('model', 'Cat', { name : String }),
'Should have called model');
});
mime/mime.js:128
require
( name, require
)
Given a module name and the default require function, Mime.require will return a mocked module for the name if one was registered with _mockModule or it will defer the call to the require function passed in
Parameters:
-
name
<String>- the module name
-
require
<Function>- the default require function
- of exported module's symbols
Example
// In your module, use Mime.require for any dependency
http = Mime.require('http', require);