2015-02-24 85 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