Skip to content

Instantly share code, notes, and snippets.

@TravisMullen
Last active September 17, 2016 16:42
Show Gist options
  • Save TravisMullen/a765a07242b68897a832e852dabc1442 to your computer and use it in GitHub Desktop.
Save TravisMullen/a765a07242b68897a832e852dabc1442 to your computer and use it in GitHub Desktop.
hasRequiredAttribute asmine Matchers
beforeEach(function() {
jasmine.addMatchers({
//
// actual: data object to check for property
//
// expected: property or property chain as string
//
hasRequiredAttribute: function() {
return {
compare: function(actual, expected) {
var result = {},
key, // current target key/scope
keys, // all
scope = actual, // scope cause we gonna dig deep
// results dont exist until we find them!
value = undefined;
if (expected.indexOf('.') < 0) {
// if not nested property do shallow test
//
value = actual[expected];
} else {
keys = expected.split('.');
for (var i = 0; i < keys.length; i++) {
key = keys[i];
if (typeof(scope[key]) !== 'undefined') {
scope = scope[key];
if (i === keys.length - 1) {
// since this is last check, lets return the value
value = scope;
}
}
}
}
// if (typeof(value) === 'object') {
// // result.message = expected + ' is an object. Please defined a spefic property.';
// }
result.pass = (typeof(value) !== 'undefined');
result.message = expected + ' is not a defined attribute ( value: ' + value + ' )!';
return result;
//The compare function must return a result object with:
// 1. A 'pass' property that is a boolean result of the matcher
// 2. A 'message' property that is a string to show details when test get failed.
}
};
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment