Is there any QUnit statement / function that checks if an element is in the array or not

I have an array of expected output in my Qunit function. Now I want to check if my result of the function is in this array or not.

var a =new array('abc','cde','efg','mgh'); 

Now my question is: is there any QUnit condition / function that can do this for me?

I know that with some JS encoding I am creating a method to test this, but I want to be just OUnit !!!!

+4
source share
2 answers

If you have JavaScript 1.6, you can use Array.indexOf

 test("myFunction with expected value", function() { var expectedValues = ['abc','cde','efg','mgh']; ok(expectedValues.indexOf(myFunction()) !== -1, 'myFunction() should return an expected value'); }); 

If you want, you can extend QUnit to support such statements:

 QUnit.extend(QUnit, { inArray: function (actual, expectedValues, message) { ok(expectedValues.indexOf(actual) !== -1, message); } }); 

Then you can use this custom inArray() method in your tests:

 test("myFunction with expected value", function() { var expectedValues = ['abc','cde','efg','mgh']; QUnit.inArray(myFunction(), expectedValues, 'myFunction() should return an expected value'); }); 

I created jsFiddle to show both options .

+5
source

QUnit offers the deepEqual function for this. You can compare the array using it:

 var resultArray = myFunction(); deepEqual(["Expected", "array"], resultArray, "myFunction returned wrong Array"); 
0
source

All Articles