2016-05-31 5 views
0

JavaScriptテストフレームワークのバージョン2 Jasmineは残念ながらいくつかの大きな変更を導入しました。ここで概説されるように、これらの変化の一つは、カスタムのmatcherの処理方法である。カスタムマッチャをJasmine 1からJasmine 2に移行するにはどうすればよいですか?

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

addMatchers機能、それがグローバルジャスミンの対象になりました(この)仕様ではなくなりました。

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

マッチャが少し異なって設定されました。ファクトリは、jasmines 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

新しいジャスミンバージョンへの移行を容易にするために、次の特別な移行オブジェクトが役立ちます。

このオブジェクトにマッチャーを追加する代わりに、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、および冗談と互換性のあるマッチャを書き込むことができます。

関連する問題