Package Exports
- mach.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (mach.js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
mach.js
Simple mocking framework for JavaScript inspired by CppUMock and designed for readability
Mocking a Function
var mach = require('mach');
var f = mach.mockFunction();
f.shouldBeCalled().when(function() {
f();
});Naming a Mocked Function
var mach = require('mach');
// Mocked function is now given as 'f' in error messages
var f = mach.mockFunction('f');Mocking an Object
var mach = require('mach');
var someObject = {
foo: function() {},
bar: function() {}
};
mockedObject = mach.mockObject(someObject);
mockedObject.foo.shouldBeCalled().when(function() {
mockedObject.foo();
});Naming a Mocked Object
var mach = require('mach');
var someObject = {
foo: function() {},
bar: function() {}
};
// Mocked fields are now given as 'someObject.foo' and 'someObject.bar' in error messages
mockedObject = mach.mockObject(someObject, 'someObject');Required Arguments
var mach = require('mach');
var f = mach.mockFunction();
f.shouldBeCalledWith(1, 2).when(function() {
f(1, 2);
});Ignored Arguments
var mach = require('mach');
var f = mach.mockFunction();
f.shouldBeCalledWithAnyArguments().when(function() {
f(1, 2);
});Ignoring Other calls
var mach = require('mach');
var f = mach.mockFunction();
f.shouldBeCalled().andOtherCallsShouldBeIgnored().when(function() {
f();
f(1);
});Return Values
var mach = require('mach');
var f = mach.mockFunction();
f.shouldBeCalled().andWillReturn(4).when(function() {
expect(f()).toBe(4);
});Thrown Values
var mach = require('mach');
var f = mach.mockFunction();
f.shouldBeCalled().andWillThrow(Error('error').when(function() {
try {
f();
}
catch(e) {
console.log(e.message);
}
});Multiple Expectations
var mach = require('mach');
var f1 = mach.mockFunction();
var f2 = mach.mockFunction();
f1.shouldBeCalled()
.andAlso(f2.shouldBeCalled())
.when(function() {
f1();
f2();
});Optional Expectations
var mach = require('mach');
var f = mach.mockFunction();
f.mayBeCalled().when(function() {});Optional Ordering
var mach = require('mach');
var f = mach.mockFunction();
// Use andThen or then when order is important
f.shouldBeCalledWith(1)
.andThen(f.shouldBeCalledWith(2))
.when(function() {
f(2); // Error, out of order call
f(1);
});
f.shouldBeCalledWith(1)
.then(f.shouldBeCalledWith(2))
.when(function() {
f(2); // Error, out of order call
f(1);
});
// Use andAlso or and when order is unimportant
f.shouldBeCalledWith(1)
.andAlso(f.shouldBeCalledWith(2))
.when(function() {
f(2); // No error, order is not fixed when 'andAlso' is used
f(1);
});
f.shouldBeCalledWith(1)
.and(f.shouldBeCalledWith(2))
.when(function() {
f(2); // No error, order is not fixed when 'and' is used
f(1);
});Mixed Ordering
var mach = require('mach');
var f = mach.mockFunction();
f.shouldBeCalledWith(1)
.and(f.shouldBeCalledWith(2))
.then(f.shouldBeCalledWith(3))
.and(f.shouldBeCalledWith(4))
.when(function() {
f(2);
f(1);
f(4);
f(3);
});Value Equality
var mach = require('mach');
var f = mach.mockFunction();
f.shouldBeCalledWith(mach.same([1, 2, 3]))
.when(function() {
f([1, 2, 3]);
});Ignore Mocked Calls
var mach = require('mach');
var f = mach.mockFunction();
mach.ignoreMockedCallsWhen(function() {
f();
});Flexible Syntax
var mach = require('mach');
var f1 = mach.mockFunction();
var f2 = mach.mockFunction();
function somethingShouldHappen() {
return f1.shouldBeCalled();
}
function anotherThingShouldHappen() {
return f2.shouldBeCalledWith(1, 2, 3);
}
function theCodeUnderTestRuns() {
f1();
f2(1, 2, 3);
}
// Actual test.
somethingShouldHappen()
.and(anotherThingShouldHappen())
.when(theCodeUnderTestRuns);Handy Error messages
var mach = require('mach');
var f1 = mach.mockFunction('f1');
var f2 = mach.mockFunction('f2');
var f2 = mach.mockFunction('f3');
f1.shouldBeCalledWith(1)
.and(f2.shouldBeCalledWith(2))
.when(function() {
f1(1);
f3(3);
});Unexpected function call f(3)
Completed calls:
f1(1)
Incomplete calls:
f2(2)