2015-02-24 113 views
0

代碼:這個單元測試失敗的原因是什麼?

$scope.nextStep = function(route) { 
    session.save($scope.sessionViewModel); 

    var input = { 
     emailAddress : session.account.email, 
     caller : 'USERNAME_EXIST' 
    }; 

    webServiceDal.doesWebLoginExist(input).success(function(response) { 
     console.log(response.WebLoginAppResponse.errorFlag); 
     if ((response.WebLoginAppResponse.errorFlag) && ((response.WebLoginAppResponse.returnCode == 1006) || (response.WebLoginAppResponse.returnCode == 'MSG0307'))) { 
     $scope.globalError = $scope.validationViewModel.email.existErrorMessage; 
     } 
     else 
     $location.path(route); 
    }); 
    }; 

測試:

describe('forgotPasswordCtrl', function() { 

    beforeEach(module('forgotPasswordApp')); 


    var scope, controller, q, $location, route, deferred, mockSessionService, validationProviderMock, webServDalMock; 

    beforeEach(function(){ 

     var config = { 
       urlPath : { 
        match : "" 
       } 
     }; 

     validationProviderMock = { 

     }; 

     var response = { 

     }; 

    mockSessionService = { 
     account : { 
      email : "" 
     }, 
     clear : function(){ 
      return true; 
      } 
    }; 

    webServDalMock = { 
     forgotPassword : function(){ 
      deferred = q.defer(); 
      deferred.resolve(response); 
      return deferred.promise; 
     }, 
     doesWebLoginExist : function(){ 
      deferred = q.defer(); 
      deferred.resolve(response); 
      return deferred.promise; 
     } 
    }; 

    spyOn(webServDalMock, 'forgotPassword').and.callThrough(); 
    spyOn(webServDalMock, 'doesWebLoginExist').and.callThrough(); 
    spyOn(mockSessionService, 'clear').and.callThrough(); 
}); 

     beforeEach(inject(function($rootScope, $controller, _$location_, $q){ 
     scope = $rootScope.$new(); 
     $location = _$location_; 
     q = $q; 
     controller = $controller('forgotPasswordCtrl', { $scope: scope, webServiceDal : webServDalMock, session : mockSessionService, validationProvider : validationProviderMock }); 
     scope.$apply(); 
     })); 


     it('should call clear method of session', function(){ 
      scope.cancel(); 
      expect(mockSessionService.clear).toHaveBeenCalled(); 
     }); 

     it('should return the correct url', function(){ 
      scope.cancel(); 
      config.urlPath.match("tfgm_customer"); 
      expect(window.location.assign).toEqual("/web/tfgm_customer"); 
     }); 

     it('asf', function(){ 
      scope.cancel(); 
      config.urlPath.match("tfgm_customerERROR"); 
      expect(window.location.assign).toEqual("/web/tfgm_admin"); 
     }); 

     it('should call webServiceDal', function(input){ 
      scope.finish(); 
      scope.$apply(); 
      expect(webServDalMock.forgotPassword).toHaveBeenCalled(); 
     }); 

     it('should call webServiceDal', function(){ 
      scope.nextStep(route); 
      scope.$apply(); 
      expect(webServDalMock.doesWebLoginExist).toHaveBeenCalled(); 
     }); 
}); 

每個前:

beforeEach(inject(function($rootScope, $controller, _$location_, $q){ 
     scope = $rootScope.$new(); 
     $location = _$location_; 
     q = $q; 
     controller = $controller('forgotPasswordCtrl', { $scope: scope, webServiceDal : webServDalMock, session : mockSessionService, validationProvider : validationProviderMock }); 
     scope.$apply(); 
     })); 

爲我的生活着工作了這是爲什麼不通過?我已經調用了正確的函數,並正確地調用了期望值。我有其他文件,我已經運行了相同的測試,唯一的區別是命名變量等,他們通過。 我錯過了一些簡單的東西嗎?

+2

什麼是測試失敗的原因嗎? – 2015-02-24 10:01:11

+0

請指定失敗測試的原因。 – Wawy 2015-02-24 10:20:11

+0

由於未達到預期,測試是否失敗,或者您是否收到另一條消息?順便說一句,您不需要調用$ apply,因爲您可以預期在摘要運行之前調用模擬。你需要$ apply來驗證成功處理程序被調用。 – thomaux 2015-02-24 10:22:17

回答

1

您的問題是,延期承諾不返回success功能,而是(thencatchfinally$q docs

你將不得不修改您的模擬doesWebLoginExist調用時返回一個成功的功能。

編輯: 喜歡的東西

doesWebLoginExist : function(){ 
     return {success: function(cb) { 
      cb(response); 
     }}; 
    } 
+0

,所以就在它現在說的地方返回deferred.promise你認爲它是返回deferred.success? – RagingBull 2015-02-24 11:11:05

+0

@RagingBull檢查我的編輯。 – Wawy 2015-02-24 14:06:25

+0

你怎麼擺脫延期?爲什麼延期工作在我的所有其他情況下只是不是這個? cb代表什麼?但我會給它一個去看看會發生什麼 – RagingBull 2015-02-25 14:07:45