我有一个工作角度应用程序,我需要编写单元测试(我通常知道它是另一种方式)。
每当我运行我的jasmine任务时,我都会收到以下错误:
ReferenceError: Can't find variable: ApplicationService
我们只想在phantomJS中运行测试而没有业力这是可能的,如果是这样,你能看看我的代码并告诉我发生了什么事。
我在我的gruntfile中添加了一个jasmine任务,如下所示:
jasmine:{
src: ['src/main/js/**/*.js'],
options:{
specs: 'src/test/js/**/*.js',
vendor: [ 'src/test/lib/**/*.js', 'src/main/lib/js/*.js']
}
},
我正在尝试测试的服务位于文件'src / main / js / services / ApplicationService.js'中,如下所示:
(function(){
'use strict';
var services = angular.module('portal.services');
services.factory('ApplicationService', ['ApplicationData', 'localStorageService' ,'UserService', function(ApplicationData, localStorageService, UserService){
return new ApplicationService(ApplicationData, localStorageService, UserService);
}]);
function ApplicationService(ApplicationData, localStorageService, UserService){
this.applicationData = ApplicationData;
this.localStorageService = localStorageService;
this.userService = UserService;
}
ApplicationService.prototype.getApplications = function(entity){
var locale = this.userService.getUserinfoLocale();
var applications = this.localStorageService.get(Constants.key_applications+locale+'_'+entity);
if(applications !== null && applications !== undefined){
return JSON.parse(applications);
} else {
return this.applicationData.getApplications().query({locale: locale, entity: entity}, $.proxy(function(data){
this.save(Constants.key_applications+locale+'_'+entity, JSON.stringify(data));
}, this));
}
};
}());
我的测试文件位于'src / test / js / services / ApplicationServiceTest.js'中,如下所示:
(function () {
'use strict';
describe('ApplicationService.js unit test suite', function () {
var applicationData, localStorageService, userService = null;
var applicationService = new ApplicationService(applicationData, localStorageService, userService);
beforeEach(function () {
applicationData = {
getApplications:function () {
return {application1:'yess', application2:'okay'};
}
};
localStorageService = {
get:function (key) {
if (key === Constants.key_applications + 'nl_ESS')
return JSON.stringify({application1:'name1'});
else if (key === Constants.key_applications + 'nl_SVF')
return JSON.stringify({application1:'name2'});
else if (key === Constants.key_applications + 'nl_MED')
return JSON.stringify({application1:'name3'});
},
add:function (key, value) {
}
};
userService = {
getUserinfoLocale:function () {
return 'nl';
}
};
});
it('ApplicationService.getApplications should delegate to ApplicationData.getApplications', function () {
var applicationService =
spyOn(localStorageService, get(Constants.key_applications + 'nl_ESS')).andReturn(null);
spyOn(applicationData, 'getApplications');
expect(applicationService.getApplications()).toBe({application1:'name1', application2:'name2'});
expect(applicationData.getApplications).toHaveBeenCalled();
});
it('ApplicationService.getApplications should use the localsStorageService cache', function () {
});
});
}());
答案 0 :(得分:2)