MyJasmineMatcher = function () {};
MyJasmineMatcher.prototype.getArrayFromString = function (arrString) {
var arrayResult = arrString.split(',');
/* use this if expecting int content in array
for (a in arrayResult ) {
arrayResult[a] = parseInt(arrayResult[a]);
}
*/
return arrayResult;
};
//from http://joelhooks.com/blog/2012/11/17/using-custom-jasmine-matchers-to-make-unit-tests-more-readable/
describe("Write a function that returns an array and then use a custom Jasmine Matcher that checks if the array has the same content ignoring the order of the content: ", function () {
var myJasmineMatcher;
beforeEach(function () {
myJasmineMatcher = new MyJasmineMatcher();
//inArray(array, el) and isEqArrays(arr1, arr2) from http://stackoverflow.com/questions/3243275/javascript-arrays-checking-two-arrays-of-objects-for-same-contents-ignoring-o
function inArray(array, el) {
for (var i = array.length; i--;) {
if (array[i] === el) return true;
}
return false;
}
function isEqArrays(arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
}
for (var i = arr1.length; i--;) {
if (!inArray(arr2, arr1[i])) {
return false;
}
}
return true;
}
//here is the custom matcher
this.addMatchers({
arrayToContainContentOf: function (expectedArray) {
return isEqArrays(this.actual, expectedArray);
}
});
});
it(" myJasmineMatcher.getArrayFromString('1,2,3') which produces ['1','2','3'] matches the content of ['2','3','1']", function () {
var result = myJasmineMatcher.getArrayFromString("1,2,3");
//using the custom matcher here
expect(result).arrayToContainContentOf(['2', '3', '1']);
});
it(" myJasmineMatcher.getArrayFromString('1,2,3') which produces ['1','2','3'] matches the content of ['3','1','2']", function () {
var result = myJasmineMatcher.getArrayFromString("1,2,3");
//using the custom matcher here
expect(result).arrayToContainContentOf(['3', '1', '2']);
});
});
// load jasmine htmlReporter
(function () {
var env = jasmine.getEnv();
env.addReporter(new jasmine.HtmlReporter());
env.execute();
}());