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("...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.