2015-05-18 11 views
6

Benhttp isteği için birim testi nasıl yazılır?

myService.getItem('/api/toy/' + scope.id).success(
    function(toy) { 
     $scope.toy = toys.details; 
    } 
); 

MyService benim test denetleyicisi olarak benim durumumda bir birim testi

angular.module('toyApp').service('myService', ['$http', 
    function($http) { 
     var service = {}; 
     return { 
      getItem: function(url) { 
       return $http.get(url); 
      }, 
     }; 
    } 
]); 

Testi dosyası

bir hizmeti yapmaya çalışıyorum.

describe('toy ctrl', function() { 
    var $httpBackend, ctrl, myService; 

    beforeEach(module('toyApp')); 

    beforeEach(inject(function (_$controller_, _$httpBackend_, _$rootScope_, __myService_) { 
     scope = _$rootScope_.$new(); 
     $httpBackend = _$httpBackend_; 
     myService = _myService_; 

     ctrl = _$controller_('toyCtrl', { 
      $scope: scope 
     });  

    })); 

    describe('call my service', function() { 
     it('should make request when app loads', function() { 
      $httpBackend.expectGET('/api/toy/123').respond({id:123, detail:456 }); 
      myService.getItem('/api/toy/123').then(function(toy){ 
       expect(scope.toy.detail).toBe(456); 
      }) 
      $httpBackend.flush(); 
    }) 
}) 

ben $httpBackend.flush() alırsan ben hata gitmiş,

Error: Unexpected request: GET /api/toy/123 
No more request expected 

edilir alıyorum ama

function(toy) { 
     $scope.toy = toys.details; 
    } 

bölümünü karşılamayacaktır. Fonksiyon çağrısını kapatmak ve bunu nasıl yapacağımı bilmiyorum. Bana herkes yardım edebilir mi? Çok teşekkürler

+0

misiniz ünite test kontrolörü veya servisi (uygulama yükleri açık olduğunda talepte bulunmalıdır) ke kontrolörü)? – PSL

+0

benim denetleyicisi – FlyingCat

+1

Tamam içeride hizmetini test ediyorum. Bu durumda servisinizi atabilmeniz ve kontrol cihazına enjekte edebilmeniz yeterlidir. Ve kapsam değerlerine karşı test değeri. Temelde ne ben söylemeye çalışıyorum sadece kontrolör mantığı 'myService.getItem ('/ API/oyuncak/123') test etmek zorunda olduğunda birim test denetleyicisi olan' aslında alakasız. – PSL

cevap

3

Denetleyiciyi test eden "birim" iseniz göründüğünüzden, denetleyici mantığını sınamanız gerektiğinden, resimdeki resmi getirmeniz gerekmez. Sahte bir hizmet oluşturabilir ve testinizdeki denetleyiciyi oluştururken enjekte edebilirsiniz.

Örnek:

var mockItem = {details:{//somestuff}, id:'1'};// set up a mock object to test against later 
//.... 
beforeEach(inject(function (_$controller_, _$httpBackend_, _$rootScope_, _$q_) { 
    scope = _$rootScope_.$new(); 
    $httpBackend = _$httpBackend_; 

    //Set up mock 
    myService = jasmine.CreateSpyObj('myService', ['getItem']); 
    myService.getItem.and.returnValue($q.when(mockItem)); 

    ctrl = _$controller_('toyCtrl', { 
     $scope: scope, 
     myService: myService //<-- Pass it here 
    });  

})); 


    //.....Assuming you are making the call when controller is instantiated 
    it('should make request when app loads', function() { 
    expect(myService.getItem).toHaveBeenCalled(); 
    //You could also check as below 
    //expect(myService.getItem).toHaveBeenCalledWith(expectedidpassedin); 
    scope.$digest(); //Resolve $q promise callback 
    expect($scope.toy).toEqual(mockItem .details); 
    }); 

Özellikle Yapabileceğin tek başına hizmetini test ünitesi ise: Kumandanızda olarak

it('should make request when app loads', function() { 
     var resp; 
     $httpBackend.expectGET('/api/toy/123').respond({id:123, detail:456}); 
     myService.getItem('/api/toy/123').then(function(response){ 
      resp = response.data; 
     }); 
     $httpBackend.flush(); 
     expect(resp.detail).toEqual(456); 
}); 

yerine zincirleme success kullanımı then

myService.getItem('/api/toy/' + scope.id).then(
    function(response) { 
     $scope.toy = response.toys.details; 
    }); 
+0

sayesinde hatasını alıyorum. +1 – FlyingCat

+0

@FlyingCat Bu bir sorun olmamalı. o zaman sözün bir parçasıdır. başarı, sadece vaat üzerinde sadece bir sarıcı olan http vaadinin bir parçasıdır. O zaman her yerde mevcut olmalı. Ayrıca testinizde $ q.when (data) 'yaptığınızdan emin olun. Ayrıca, '' '' 'response.data' kullandığınızda 'başarı' geri çağrısında bir ilk argüman aldığınıza dair yanıt olduğunu unutmayın. – PSL