2013-11-04 19 views
18

Beklenenden daha fazla bir isteğim var Kullanıcının bir düğmeyi tıklattığında hizmetimin durumunu almak için bir işlevim var veya bir olay tetiklendiğinde ve bu işlev otomatik olarak çağrıldığında . Ben de diğer $ httpBackend testleri birim testinde

describe('SendServiceCtrl', function(){ 
    var scope, ctrl, $httpBackend, configuration; 

    beforeEach(function() { 
     module('papi', 'ngUpload'); 
    }); 

    beforeEach(inject(function(_$httpBackend_, $rootScope, $controller, config) { 
     configuration = config; 
     $httpBackend = _$httpBackend_; 
     $httpBackend.expectGET(configuration.entrypoint + configuration.api + "/user/outboundstatus/").respond(200, {"meta":{"apiVersion":"0.1","code":200,"errors":null},"response":{"allowed":false}}); 
     scope = $rootScope.$new(); 
     ctrl = $controller('SendServiceCtrl', {$scope: scope}); 
    })); 

    it('Should get the status', function() { 

    scope.serviceId = '09bb5943fa2881e1'; 
    scope.getStatus(); 
    $httpBackend.whenGET(configuration.entrypoint + configuration.api + '/outbound/service/' + scope.serviceId).respond(200, {"meta":{"apiVersion":"0.1","code":200,"errors":null}}); 

    }); 

}); 

: birim test gibi yapılır

$scope.getStatus = function() { 
    $http({method: 'GET', url: config.entrypoint + config.api + '/outbound/service/' + $scope.serviceId}) 
    .success(function(data) { 
     $scope.errorMessage = ''; 
     $scope.serviceAllGood = data; 
    }) 
    .error(function() { 
     $scope.serviceAllGood = ''; 
     $scope.errorMessage = 'We are experiencing problems retrieving your service status.'; 
    }); 
    } 

:

Bu kullanıyorum denetleyicisi tanımlanan benim işlevi vardır Aynı kontrolör, ama hepsi sadece somurtkan olarak çalışır. Neyi yanlış yapıyorum?

cevap

12

yöntemini çağırmadan önce whenGETürününü sağlamanız gerekir.

it('Should get the status', function() { 
    scope.serviceId = '09bb5943fa2881e1'; 
    $httpBackend.whenGET(configuration.entrypoint + configuration.api + '/outbound/service/' + scope.serviceId).respond(200, {"meta":{"apiVersion":"0.1","code":200,"errors":null}}); 
    scope.getStatus(); 
}); 

İsteğin beklentisini ayarlayın ve ardından isteği tetikleyin.

Bu yardımcı olur umarım.

+0

şablonları bu hataya neden olur. – FlavorScape

+0

'ui.bootstrap' gibi '$ templateCache' kullanarak GET alay ihtiyacı ortadan kaldıracak mıdır? – domokun

+0

@domokun Kullanıldığı yere bağlantı gönderebilir misiniz? –