2016-08-02 2 views
0

테스트에 Jasmine을 사용하고 테스트 러너로 Karma를 사용하고 있습니다.Jasmine Karma Array.find는 함수가 아닙니다.

TypeError: myns.dataList.find is not a function

이이 기능을 이해할 수없는 제안 :

나는 오류와 함께 실패 Array.prototype.find()를 사용하여 간단한 찾기 방법이있다. 또한 동일한 파일에 메서드에 대한 polyfill이 있고 노드에 an es6 shim도 설치되어 있습니다.

describe('the description', function() { 
    it('should work', function() { 

     jasmine.getJSONFixtures().fixturesPath = 'base/path/to/json/'; 
     myns.dataList = loadJSONFixtures('dataList.json'); 

     console.log(myns.dataList); /* all ok here, json is loaded */ 

     var theName = myns.dataList.find(function(entry) { 
      return entry.name === selfName; 
     }); 

     expect(2 + 2).not.toEqual(4); /* doesn't get here because of TypeError */ 
    }); 
}); 

참고 : 크롬 51.0.2704을

이 내가 노력하고있어되어 사용

이는 재스민 브라우저에서 직접 실행하면 정상적으로 작동

답변

1

월 늦게 회신해도되지만,이 문제를 해결하기 위해 할 수있는 일입니다. 테스트 코드가 fin 메서드를 호출하기 전에 다음 코드를 호출하십시오.

if (typeof Array.prototype.find !== 'function') { 
     Array.prototype.find = function(iterator) { 
      var list = Object(this); 
      var length = list.length >>> 0; 
      var thisArg = arguments[1]; 
      var value; 

      for (var i = 0; i < length; i++) { 
       value = list[i]; 
       if (iterator.call(thisArg, value, i, list)) { 
        return value; 
       } 
      } 
      return undefined;  
     }; 
    } 

이렇게하면 배열 정의에 사용할 수있는 메소드가 없는지 확인하여 추가합니다.

원래는 여기에 게시 :

describe('the description', function() { 
    it('should work', function() { 

     jasmine.getJSONFixtures().fixturesPath = 'base/path/to/json/'; 
     myns.dataList = loadJSONFixtures('dataList.json'); 

     console.log(myns.dataList); /* all ok here, json is loaded */ 
     if (typeof Array.prototype.find !== 'function') { 
      Array.prototype.find = function(iterator) { 
       var list = Object(this); 
       var length = list.length >>> 0; 
       var thisArg = arguments[1]; 
       var value; 

       for (var i = 0; i < length; i++) { 
        value = list[i]; 
        if (iterator.call(thisArg, value, i, list)) { 
         return value; 
        } 
       } 
       return undefined;  
      }; 
     } 

     var theName = myns.dataList.find(function(entry) { 
      return entry.name === selfName; 
     }); 

     expect(2 + 2).not.toEqual(4); /* doesn't get here because of TypeError */ 
    }); 
}); 
: http://sehajpal.com/2016/10/missing-function-definitions-in-jasmine-karma-testing-not-a-function/

그래서, 약, 코드가 (당신의 필요에 따라 리팩터링)과 같이 표시됩니다

관련 문제