我有一个单元测试,上面的错误一直没有发现,不确定这究竟是什么问题,通过错误的外观,它说我需要声明一个函数。有人可以指出我的代码有什么问题:
describe('Test loginService', function() {
var $location, loginService, $scope, authentication, $rootScope;
beforeEach(function() {
module('app');
inject(function(_$location_, _$rootScope_, _loginService_,
_authentication_) {
$location = _$location_;
authentication = _authentication_;
loginService = _loginService_;
$rootScope = _$rootScope_;
$scope = $rootScope.$new();
});
});
it('Should successfully login with correct credentials', function() {
$scope.username = 'a@a.com';
$scope.password = 'a';
spyOn($location, 'url');
loginService.login($scope);//Complains about this line
expect($location.url).toHaveBeenCalled();
expect($location.url).toHaveBeenCalledWith('/home');
});
app.factory('loginService', function(parserService, $location,
authentication, $rootScope) {
return {
login : function(scope) {
parserService.getData().then(function(data) { //This line
if (scope.username === data.username
&& scope.password === data.password) {
...
$location.url("/home");
} else {
scope.loginError = "Invalid Credentials";
}
});
}
});
app.factory('parserService', function($http, $q) {
return {
getData: function() {
var deferred = $q.defer();
$http.get('res/file.json').success(function(data) {
deferred.resolve(data);
}).error(function(){
deferred.reject();
});
return deferred.promise;
}
}
});
Chrome 36.0.1985 (Windows 7) Test loginService Should successfully login with correct credentials FAILED
TypeError: undefined is not a function
at Object.login (D:../js/services/loginService.js:6:18)
at null.<anonymous> (D:/../test/unit/loginTest.js:92:16)
答案 0 :(得分:0)
因为您无法格式化评论,所以我使用答案......:D
你试过这个吗?var $location, loginService, $scope, authentication, $rootScope, parserService;
beforeEach(function() {
module('app');
inject(function(_$location_,
_$rootScope_,
_parserService_,
_loginService_,
_authentication_) {
$location = _$location_;
authentication = _authentication_;
loginService = _loginService_;
$rootScope = _$rootScope_;
$scope = $rootScope.$new();
parserService = _parserService_;
});
});