简体   繁体   中英

Unit testing Javascript - Jasmine test fails although code is working as expected

I'm trying to get started with unit testing with javascript. I'm using jasmine framework 2.5.2 and Netbeans 8.2 as my IDE.

This is the simple code i want to test:

var ListHandler = {

    "reverseList": function (inputList) {
        var list = inputList,
            reversedList = [],
            length = list.length;

        for (var i = 0; i < length; i++) {
            reversedList.push(list[length - i - 1]);
        }

        return reversedList;
    },

    "sumUp": function (inputList) {
        var count = 0;
        for (var i = 0; i < inputList.length; i++) {
            count += inputList[i];
        }

        return count;
    },

    "concatenate": function (inputList1, inputList2) {
        var result = inputList1.concat(inputList2);
        return result;
    }
}

This is my test:

describe("List Handler", function(){ 

   it("should return reversed List",function(){ 
      expect(ListHandler.reverseList([1,2,3])).toEqual([3,2,1]); 
   }); 

   it("should return sum of list values",function(){ 
      expect(ListHandler.sumUp([1,2,3])).toEqual(6); 
   }); 

   it("should return concatenated List", function(){
       expect(ListHandler.concatenate([1,2],["a","b"]).toEqual([1,2,"a","b"]));
   });
});

So when i run the test, the first two methods pass, but i get an error for the last method "concenate":

TypeError: ListHandler.concatenate(...).toEqual is not a function

However, when i just execute it like alert(ListHandler.concatenate([1,2,5], ["a", "b", "c"])); it works fine. Can someone explain to me why jasmine complains?

You have a misplaced closing paranthesis.

Try;

  expect(ListHandler.concatenate([1,2],["a","b"])).toEqual([1,2,"a","b"]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM