UNPKG

@incdevco/framework

Version:
109 lines (60 loc) 1.9 kB
var Call = require("../call"); var Expectation = require("../expectation"); function Mocked(obj) { "use strict"; this.calls = {}; this.expectations = {}; this.notExpected = {}; this.obj = obj; this.original = {}; } Mocked.prototype.done = function () { "use strict"; var self = this; Object.keys(this.expectations).forEach(function (key) { self.expectations[key].forEach(function (expectation) { if (!expectation.met()) { throw new Error("expectation for '" + key + "' not met."); } }); }); }; Mocked.prototype.doNotExpect = function (name) { var expectation = new Expectation(name), self = this; this.notExpected[name] = this.expectations[name] || []; this.notExpected[name].push(expectation); this.original[name] = this.obj[name]; this.obj[name] = function () { var call; self.calls[name] = self.calls[name] || []; call = new Call(name, self.notExpected[name][self.calls[name].length]); self.calls[name].push(call); return call.execute(arguments); }; return expectation; }; Mocked.prototype.expect = function (name) { "use strict"; var expectation = new Expectation(name), self = this; this.expectations[name] = this.expectations[name] || []; this.expectations[name].push(expectation); this.original[name] = this.obj[name]; this.obj[name] = function () { var call; self.calls[name] = self.calls[name] || []; call = new Call(name, self.expectations[name][self.calls[name].length]); self.calls[name].push(call); return call.execute(arguments); }; return expectation; }; Mocked.prototype.restore = function () { 'use strict'; var self = this; Object.keys(this.original).forEach(function (key) { self.obj[key] = self.original[key]; }); }; module.exports = Mocked;