Skip to content

Instantly share code, notes, and snippets.

@RhysC
Last active August 29, 2015 14:03
Show Gist options
  • Save RhysC/3bff3022cc1c48d6c62b to your computer and use it in GitHub Desktop.
Save RhysC/3bff3022cc1c48d6c62b to your computer and use it in GitHub Desktop.
Expected Object like approach in Jasmine - Tags - JavaScript JS TDD Spec ExpectedObject Matcher addMatchers 2.0
var customMatchers = {
toInclude: function (util, customEqualityTesters) {
return {
compare: function (actual, expected) {
if (expected === undefined) {
expected = '';
}
var result = {
message: '',
pass: true
};
var missingProperties = {};
var notEqualProperties = {};
for (var i in expected) {
if (expected.hasOwnProperty(i) && !actual.hasOwnProperty(i)) {
missingProperties[i] = expected[i];
} else {
if (!util.equals(actual[i], expected[i], customEqualityTesters)) {
notEqualProperties[i] = { expected: expected[i], actual: actual[i] };
}
};
}
if (getObjectPropertyCount(missingProperties) > 0) {
result.message = 'Failed asserting that object had expected properties :\r\n';
for (var missingProperty in missingProperties) {
result.message += "Expected property '" + missingProperty + "'\r\n";
}
result.pass = false;
}
if (getObjectPropertyCount(notEqualProperties) > 0) {
result.message = 'Failed asserting that object had expected values :\r\n';
for (var notEqualProperty in notEqualProperties) {
result.message += "Expected '" + notEqualProperty + "' to be '" + expected[notEqualProperty] + "' but was '" + actual[notEqualProperty] + "'\r\n";
}
result.pass = false;
}
return result;
}
};
}
};
beforeEach(function () {
jasmine.addMatchers(customMatchers);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment