gpt4 book ai didi

javascript - 该单元测试失败的原因是什么?

转载 作者:行者123 更新时间:2023-12-03 10:43:00 26 4
gpt4 key购买 nike

代码:

$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();
}));

我一生都无法解决为什么这没有过去?我已经调用了正确的函数并正确地调用了期望。我还有其他文件运行了相同的测试,唯一的区别是变量的命名等,并且它们通过了。我错过了一些简单的事情吗?

最佳答案

您的问题是,延迟 promise 不会返回 success 函数,而是返回 (thencatchfinally), $q docs

您必须修改模拟 doesWebLoginExist 以在调用时返回成功函数。

编辑:类似的东西

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

关于javascript - 该单元测试失败的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28692555/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com