SORU
28 HAZİRAN 2013, Cuma


Nasıl AngularJS Test İzole Kapsam Yönerge Birimi

Birim için iyi bir yoldur ne izole AngularJS kapsamı test

JSFiddle showing unit test

Direktif Pasajı

    scope: {name: '=myGreet'},
    link: function (scope, element, attrs) {
        //show the initial state
        greet(element, scope[attrs.myGreet]);

        //listen for changes in the model
        scope.$watch(attrs.myGreet, function (name) {
            greet(element, name);
        });
    }

Ben bu yönerge değişiklikleri dinleme sağlamak için bunu yapardeğilizole kapsamlı bir çalışma:

    it('should watch for changes in the model', function () {
        var elm;
        //arrange
        spyOn(scope, '$watch');
        //act
        elm = compile(validHTML)(scope);
        //assert
        expect(scope.$watch.callCount).toBe(1);
        expect(scope.$watch).toHaveBeenCalledWith('name', jasmine.any(Function));
    });

GÜNCELLEME: Bende o düzeni tarafından kontrol eğer beklenen gözlemcileri eklenmiştir çocuk kapsamı, ama çok kırılgan ve muhtemelen kullanarak erişenleri bir kaçak yolu (nam-ı diğer konu için bir değişiklik olmadan dikkat edin!).

//this is super brittle, is there a better way!?
elm = compile(validHTML)(scope);
expect(elm.scope().$$watchers[0].exp).toBe('name');

GÜNCELLEME 2: Dediğim gibi bu kırılgan! Fikir hala ama erişimci isolateScope() 10 *değişti AngularJS daha yeni sürümlerinde çalışır:

//this is STILL super brittle, is there a better way!?
elm = compile(validHTML)(scope);                       
expect(elm.isolateScope().$$watchers[0].exp).toBe('name');

CEVAP
1 Aralık 2013, Pazar


angular element api docs bkz. Eğer kullanırsanızeleman.() kapsamsenin direktifin kapsamı özelliğinde tanımlanan element kapsamı. Eğer kullanırsanızeleman.() isolateScopetüm izole kapsamında olsun. Eğer Direktifi örneğin, şöyle bir şey :

scope : {
 myScopeThingy : '='
},
controller : function($scope){
 $scope.myIsolatedThingy = 'some value';
}

Sonra eleman arıyor.() kapsam testinizde dönecektir

{ myScopeThingy : 'whatever value this is bound to' }

Ama eğer eleman denirse.() isolateScope alacaksın

{ 
  myScopeThingy : 'whatever value this is bound to', 
  myIsolatedThingy : 'some value'
}

Bu açısal 1.2.2 kadar doğru ya da değil 1.2.3, tam olarak emin olabilirsiniz. Önceki sürümlerde tek elemanı vardı.() kapsam.

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • B4ROK

    B4ROK

    1 EKİM 2008
  • FUzzyBUnnyBOoties

    FUzzyBUnnyBO

    3 EKİM 2007
  • newreleaseblitz

    newreleasebl

    13 Ocak 2010