2016-05-31 4 views
0

JavaScript 테스트 프레임 워크 Jasmine 버전 2는 여러 가지 중요한 변경 사항을 도입했습니다. 여기에 설명 된대로 이러한 변화 중 하나는 사용자 정의 매처 (matcher)가 처리하는 방법입니다 :Jasmine 1에서 Jasmine 2로 사용자 지정 matcher를 마이그레이션하는 방법

http://jasmine.github.io/2.0/upgrading.html

addMatchers 기능은 사양 (이)가 글로벌 자스민 객체에 지금에 더 이상이다.

/* was: 
    this.addMatchers({ 
    */ 
    jasmine.addMatchers({ 

이제 matcher가 약간 다르게 설정됩니다. 팩토리는 재스민 equality 함수와 등록 된 customEqualityTester와 같은 것을 포함하는 util 객체를받습니다. 공장 대신 실제 값이 지금 통과 및 메시지 속성을 가진 객체를 반환해야이

/* was: 
     toBeCustom: function(expected) { 
     var passed = this.actual == expected; 
    */ 
    toBeCustom: function(util, customEqualityTesters) { 
     return { 
     compare: function(actual, expected) { 
      var passed = actual == expected 

비교에 존재의 실제 호출 직접 예상됩니다 비교 함수 객체를 반환 할 것으로 예상된다 .

기존 재배자를 쉽게 마이그레이션 할 수있는 방법을 찾고 있습니다. 따라서 새로운 재 스민 버전으로 쉽게 전환 할 수 있습니다.

답변

0

새로운 재 스민 버전으로 쉽게 전환 할 수 있도록 다음 특수 이송 객체가 도움이 될 것입니다.

이 오브젝트에 대해 matcher를 추가하는 대신 jasmineMigrate 오브젝트에 추가하십시오. 그러나 이것은 당신이 정말로 필요로하는 전부입니다. jasmineMigrate 객체가 나머지를 처리합니다.

/* was: 
    this.addMatchers({ 
    */ 
    jasmineMigrate .addMatchers({ 

마이그레이션 오브젝트의 구현 :

var jasmineMigrate = {}; 

jasmineMigrate.addMatchers = function (matchers) { 

    Object.keys(matchers).forEach(function (matcherName) { 
     var matcher = matchers[matcherName], 
      migratedMatcher = {}; 

     migratedMatcher[matcherName] = function (util, customEqualityTesters) { 
      return { 
       compare: function (actual) { 
        var matcherArguments, 
         thisForMigratedMatcher, 
         matcherResult, 
         passed; 

        //In Jasmine 2 the first parameter of the compare function 
        //is the actual value. 
        //Whereas with Jasmine 1 the actual value was a property of the matchers this 
        //Therefore modify the given arguments array and remove actual 
        matcherArguments = [].slice.call(arguments) 
        matcherArguments.splice(0, 1); 

        //Add actual to the this object we'll be passing to the matcher 
        thisForMigratedMatcher = { 
         actual: actual 
        }; 

        //Now call the original matcher aufgerufen, with the modified 
        //arguments and thisForMigratedMatcher which will be applied to 
        //the matcher 
        passed = matcher.apply(thisForMigratedMatcher, matcherArguments); 

        matcherResult = { 
         pass: passed, 
         message: thisForMigratedMatcher.message 
        }; 
        return matcherResult; 
       } 
      } 
     }; 

     jasmine.addMatchers(migratedMatcher); 
    }); 
} 
0

add-matchers 라이브러리는 당신이 재스민 V1, 재스민 V2, 그리고 농담과 호환되는 매처 (matcher)를 작성할 수 있습니다.

관련 문제