@incdevco/framework
Version:
node.js lambda framework
139 lines (79 loc) • 2.8 kB
JavaScript
var Expect = require('chai').expect;
var Mock = require('./index');
describe('mock', function () {
'use strict';
var mock, obj;
beforeEach(function () {
mock = new Mock();
obj = {};
});
it('should call submitted callback with expected arguments', function(done) {
var arg1 = 'arg1',
arg2 = 'arg2',
arg3 = 'arg3',
arg4 = 'arg4',
called = false;
mock.mock(obj).expect('test')
.with(arg1, arg2)
.willExecuteCallback(arg3, arg4);
obj.test(arg1, arg2, function (first, second) {
called = true;
Expect(first).to.equal(arg3, 'callback first');
Expect(second).to.equal(arg4, 'callback second');
});
Expect(called).to.equal(true, 'called');
mock.done(done);
});
it('should be able to reflect a rejected promise', function(done) {
var rejected = 'rejected';
mock.mock(obj).expect('test')
.willReject(rejected);
obj.test().reflect()
.then(function (actual) {
Expect(actual.isRejected()).to.equal(true, 'actual.isRejected');
Expect(actual.reason()).to.equal(rejected, 'actual.reason');
mock.done(done);
})
.catch(done);
});
it('should resolve with the first supplied argument when no argument is supplied to willResolve', function(done) {
var expected = 'expected';
mock.mock(obj).expect('test')
.willResolve();
obj.test(expected)
.then(function (actual) {
Expect(actual).to.equal(expected, 'actual');
mock.done(done);
})
.catch(done);
});
it('should return first supplied argument when no argument is supplied to willReturn', function () {
var expected = 'expected';
mock.mock(obj).expect('test')
.willReturn();
Expect(obj.test(expected)).to.equal(expected, 'actual');
mock.done();
});
it('should restore expected function to object', function () {
var original = {
count: 1,
test: function () {
this.count++;
return this.count;
}
};
mock.mock(original).expect('test')
.willReturn('mocked');
Expect(original.test()).to.equal('mocked', 'mocked result');
mock.done();
Expect(original.test()).to.equal(2, 'original result');
});
it('should throw an exception when an expectation is not setup with a will', function() {
mock.mock(obj).expect('test');
try {
obj.test();
} catch (e) {
Expect(e.message).to.equal('function mocked without will', 'exception.message');
}
});
});