Improve errors with the domaine and how to use the API

This commit is contained in:
dhoko
2016-01-20 15:49:47 +01:00
parent b6798cdb06
commit 14067d0785
16 changed files with 131 additions and 28 deletions

View File

@@ -1,5 +1,7 @@
getJasmineRequireObj().SpyRegistry = function(j$) {
var getErrorMsg = j$.formatErrorMsg('<spyOn>', 'spyOn(<object>, <methodName>)');
function SpyRegistry(options) {
options = options || {};
var currentSpies = options.currentSpies || function() { return []; };
@@ -9,23 +11,24 @@ getJasmineRequireObj().SpyRegistry = function(j$) {
};
this.spyOn = function(obj, methodName) {
if (j$.util.isUndefined(obj)) {
throw new Error('spyOn could not find an object to spy upon for ' + methodName + '()');
throw new Error(getErrorMsg('could not find an object to spy upon for ' + methodName + '()'));
}
if (j$.util.isUndefined(methodName)) {
throw new Error('No method name supplied');
throw new Error(getErrorMsg('No method name supplied the second parameter is required'));
}
if (j$.util.isUndefined(obj[methodName])) {
throw new Error(methodName + '() method does not exist');
throw new Error(getErrorMsg(methodName + '() method does not exist'));
}
if (obj[methodName] && j$.isSpy(obj[methodName]) ) {
if ( !!this.respy ){
return obj[methodName];
}else {
throw new Error(methodName + ' has already been spied upon');
throw new Error(getErrorMsg(methodName + ' has already been spied upon'));
}
}
@@ -37,7 +40,7 @@ getJasmineRequireObj().SpyRegistry = function(j$) {
}
if (descriptor && !(descriptor.writable || descriptor.set)) {
throw new Error(methodName + ' is not declared writable or has no setter');
throw new Error(getErrorMsg(methodName + ' is not declared writable or has no setter'));
}
var originalMethod = obj[methodName],

View File

@@ -0,0 +1,14 @@
getJasmineRequireObj().formatErrorMsg = function() {
function generateErrorMsg(domain, usage, reco) {
var usageDefinition = usage ? 'Usage: ' + usage + '\n' : '';
var recoDefinition = reco ? 'Tips: ' + reco + '\n' : '';
return function errorMsg(msg) {
return domain + ' : ' + msg + '\n' + usageDefinition + recoDefinition;
};
}
return generateErrorMsg;
};

View File

@@ -1,16 +1,18 @@
getJasmineRequireObj().toHaveBeenCalled = function(j$) {
var getErrorMsg = j$.formatErrorMsg('<toHaveBeenCalled>', 'expect(<spyObj>).toHaveBeenCalled()', 'To test with arguments, you can use <toHaveBeenCalledWith>');
function toHaveBeenCalled() {
return {
compare: function(actual) {
var result = {};
if (!j$.isSpy(actual)) {
throw new Error('Expected a spy, but got ' + j$.pp(actual) + '.');
throw new Error(getErrorMsg('Expected a spy, but got ' + j$.pp(actual) + '.'));
}
if (arguments.length > 1) {
throw new Error('toHaveBeenCalled does not take arguments, use toHaveBeenCalledWith');
throw new Error(getErrorMsg('It does not take arguments, use toHaveBeenCalledWith'));
}
result.pass = actual.calls.any();

View File

@@ -1,17 +1,19 @@
getJasmineRequireObj().toHaveBeenCalledTimes = function(j$) {
var getErrorMsg = j$.formatErrorMsg('<toHaveBeenCalledTimes>', 'expect(<spyObj>).toHaveBeenCalledTimes(<Number>)');
function toHaveBeenCalledTimes() {
return {
compare: function(actual, expected) {
if (!j$.isSpy(actual)) {
throw new Error('Expected a spy, but got ' + j$.pp(actual) + '.');
throw new Error(getErrorMsg('Expected a spy, but got ' + j$.pp(actual) + '.'));
}
var args = Array.prototype.slice.call(arguments, 0),
result = { pass: false };
if (!j$.isNumber_(expected)){
throw new Error('The expected times failed is a required argument and must be a number.');
throw new Error(getErrorMsg('The expected times failed is a required argument and must be a number.'));
}
actual = args[0];
@@ -27,4 +29,4 @@ getJasmineRequireObj().toHaveBeenCalledTimes = function(j$) {
}
return toHaveBeenCalledTimes;
};
};

View File

@@ -1,5 +1,7 @@
getJasmineRequireObj().toHaveBeenCalledWith = function(j$) {
var getErrorMsg = j$.formatErrorMsg('<toHaveBeenCalledWith>', 'expect(<spyObj>).toHaveBeenCalledWith(...arguments)');
function toHaveBeenCalledWith(util, customEqualityTesters) {
return {
compare: function() {
@@ -9,7 +11,7 @@ getJasmineRequireObj().toHaveBeenCalledWith = function(j$) {
result = { pass: false };
if (!j$.isSpy(actual)) {
throw new Error('Expected a spy, but got ' + j$.pp(actual) + '.');
throw new Error(getErrorMsg('Expected a spy, but got ' + j$.pp(actual) + '.'));
}
if (!actual.calls.any()) {

View File

@@ -1,10 +1,12 @@
getJasmineRequireObj().toMatch = function(j$) {
var getErrorMsg = j$.formatErrorMsg('<toMatch>', 'expect(<expectation>).toMatch(<string> || <regexp>)');
function toMatch() {
return {
compare: function(actual, expected) {
if (!j$.isString_(expected) && !j$.isA_('RegExp', expected)) {
throw new Error('Expected is not a String or a RegExp');
throw new Error(getErrorMsg('Expected is not a String or a RegExp'));
}
var regexp = new RegExp(expected);

View File

@@ -1,5 +1,7 @@
getJasmineRequireObj().toThrow = function(j$) {
var getErrorMsg = j$.formatErrorMsg('<toThrow>', 'expect(function() {<expectation>}).toThrow()', 'You can match a specific exception with <toThrowError>');
function toThrow(util) {
return {
compare: function(actual, expected) {
@@ -8,7 +10,7 @@ getJasmineRequireObj().toThrow = function(j$) {
thrown;
if (typeof actual != 'function') {
throw new Error('Actual is not a Function');
throw new Error(getErrorMsg('Actual is not a Function'));
}
try {

View File

@@ -1,4 +1,7 @@
getJasmineRequireObj().toThrowError = function(j$) {
var getErrorMsg = j$.formatErrorMsg('<toThrowError>', 'expect(function() {' + '\n' + '\t' + '<expectation>' + '\n' + '}).toThrowError(<constructorErrror>, <message>)');
function toThrowError () {
return {
compare: function(actual) {
@@ -8,7 +11,7 @@ getJasmineRequireObj().toThrowError = function(j$) {
thrown;
if (typeof actual != 'function') {
throw new Error('Actual is not a Function');
throw new Error(getErrorMsg('Actual is not a Function'));
}
var errorMatcher = getMatcher.apply(null, arguments);
@@ -64,15 +67,15 @@ getJasmineRequireObj().toThrowError = function(j$) {
errorType = arguments[1];
expected = arguments[2];
if (!isAnErrorType(errorType)) {
throw new Error('Expected error type is not an Error.');
throw new Error(getErrorMsg('Expected error type is not an Error.'));
}
}
if (expected && !isStringOrRegExp(expected)) {
if (errorType) {
throw new Error('Expected error message is not a string or RegExp.');
throw new Error(getErrorMsg('Expected error message is not a string or RegExp.'));
} else {
throw new Error('Expected is not an Error, string, or RegExp.');
throw new Error(getErrorMsg('Expected is not an Error, string, or RegExp.'));
}
}

View File

@@ -25,6 +25,7 @@ var getJasmineRequireObj = (function (jasmineGlobal) {
jRequire.base(j$, jasmineGlobal);
j$.util = jRequire.util();
j$.errors = jRequire.errors();
j$.formatErrorMsg = jRequire.formatErrorMsg();
j$.Any = jRequire.Any(j$);
j$.Anything = jRequire.Anything(j$);
j$.CallTracker = jRequire.CallTracker(j$);