2016-11-15 66 views
0

我嘲笑了一個httpBackend來測試下面的代碼。

self.Api.post('/endpoint/action/', actionData) 
    .then(function(resp){ 
     result = _.get(resp, 'data.MessageList'); 
     if(resp.status = 200 && result) { 
     for (var i = 0; i < result.length; i++) { 
      addResultMessage(result[i]); 
     } 
     } 
    }); 

我嘲笑我的服務執行以下操作:

beforeEach(function(){ 
     angular.mock.module(function(ApiProvider) { 
     ApiProvider.setUrl('http://localhost:10010'); 
     }); 
    }); 

    beforeEach(inject(function($rootScope, $controller, _$interval_, $q, $injector, $httpBackend){ 
    $rootScope = $injector.get('$rootScope'); 
    scope = $rootScope; 
    httpBackend = $httpBackend; 

    createController = function() { 
     return $controller('MyCtrl',{ 
     $scope: scope, 
     $uibModalInstance: modalInstance, 
     $interval: _$interval_, 
     }); 
    } 
    })); 

內的這

 httpBackend 
     .when('POST', 'http://localhost:10010/endpoint/action/') 
      .respond(200, mockedResponse); 

當我做POST到終點,我沒有得到任何迴應。我沒有得到任何錯誤只是我沒有得到任何RESP回調

回答

0

你應該調用flush()測試後,像這樣:

it('should read expired login data', inject(function(authSrvc) { 
    $httpBackend.expectGET('data/auth.json').respond(200, mockedResponse); 

    authSrvc.checkStatus().then(function(data){ 
     expect(authSrvc.isValidSession()).toEqual(false); 
    }); 

    $httpBackend.flush(); 
    })); 
+0

不,它不工作在年底加齊平。 – acostela