2016-04-11 12 views
0

Karma, açısal ve mocha kullanıyorum.Bir hizmetin bağımlılığını açısal birim testimde nasıl alayım?

MyServices modülü Enjekte etmek istiyorum. Aynı addThing yöntem ayrıca $localStorage sağlayıcı çağırır ve ben $localStorage.setItem yöntem örtük İşte benim kod ben MyServices.addThing(data)

çağırdığınızda anılmış test etmek istiyorum: Ben çalıştırdığınızda

describe('MyServices', function() { 
    var $q; 
    var $rootScope; 
    var $scope; 
    var MyServices; 
    var $timeout; 
    var $httpBackend; 

    // inject modules 
    beforeEach(function(){ 
    module('my.stuff'); 

    var _LocalStorage_ = { 
     setItem: function(){ 
     return 'foo'; 
     } 
    }; 

    // mock the dependencies of the module we are testing 
    module(function ($provide) { 
     $provide.value('$localStorage', _LocalStorage_); //here is where the error occurs 
    }); 
    }); 

    beforeEach(inject(function (_$q_, _$rootScope_, _$timeout_, _MyServices_, _$httpBackend_) { 
    $q = _$q_; 
    $rootScope = _$rootScope_; 
    $timeout = _$timeout_; 
    MyServices = _MyServices_; 
    $httpBackend = _$httpBackend_; 
    $scope = $rootScope.$new(); 
    })); 

    it.only('should call a method and access localStorage', function(){ 
    var spyAdd = sinon.spy(MyServices, 'addThing'); 
    var spySetItem = sinon.spy($localStorage, 'setItem'); 

    MyServices.addThing({ name: 'Thing' }); 

    expect(spyAdd.callCount).to.equal(1); 
    expect(spySetItem.callCount).to.equal(1); 
    }) 
}); 

bir hata $localStorage olsun tanımlanmamış.

cevap

0

Bu sahte $ localStorage kaydınız yok, sadece orijinal olanı kullanın ve setItem yönteminde casus oluşturun.

nedenle bu

var _LocalStorage_ = { 
    setItem: function(){ 
    return 'foo'; 
    } 
}; 
module(function ($provide) { 
    $provide.value('$localStorage', _LocalStorage_); //here is where the error occurs 
}); 

kaldırmak ama zaten yönteme casus yarattık beri burada

var $localStorage; 
beforeEach(inject(function (_$q_, _$rootScope_, _$timeout_, _MyServices_, _$httpBackend_, _$localStorage_) { 
    $q = _$q_; 
    $rootScope = _$rootScope_; 
    $timeout = _$timeout_; 
    MyServices = _MyServices_; 
    $httpBackend = _$httpBackend_; 
    $scope = $rootScope.$new(); 
    $localStorage = _$localStorage_; //here 
})); 

enjekte, böylece hiçbir şey testinde değiştirilmesi gerekir.

+0

Okuduğum tüm örnekler bunu böyle yapmak için söylüyor. – chovy

+0

sizin için çalışıyor mu? ya da bu şekilde sevmiyor musun? – sdfacre

+0

, enjeksiyon için mevcut değildir, çünkü MyServices enjeksiyonundan farklı bir ad alanındadır. İsim alanını yüklediysem 100 şey gibi alay etmeliyim. Bu yüzden sadece varlığını $ offer.value ('$ localStorage', myMock) ' – chovy

0

BeforeEach'inizin dışında LocalStorage bildirmeye çalışın. Btw, buna ihtiyacınız yok, sadece LocalStorage diyebilirsiniz.

Ayrıca, bağımlılıklarla uğraşırken veya alay ederken bir grup faydalı yönteme sahip olduğundan, SinonJS dosyasını indirmenizi öneririz.

İlgili konular