【问题标题】:running jasmine angular tests without karma在没有业力的情况下运行茉莉花角度测试
【发布时间】:2013-12-18 12:33:48
【问题描述】:

我有一个工作的 Angular 应用程序,我需要为其编写单元测试(我知道通常情况正好相反)。

每当我运行我的 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 () {

    });

});
}());

【问题讨论】:

    标签: javascript unit-testing angularjs jasmine


    【解决方案1】:

    我知道这是一个非常老的问题,但以防万一它仍然有效或其他人有同样的问题:您不会将 ApplicationService.js 中的任何 ApplicationService 暴露给外界,因为您放置了一个 (function () {...})() 围绕它,因此无法在测试中实例化它。

    如果你想以 angular-mocks 的方式进行测试,你应该使用 angular-mocks,尤其是它提供的 module() 和 inject() 函数。这将允许您将服务注入到您的测试中。

    如果您想以“POJO”方式对此进行测试,您应该以某种方式公开 ApplicationService。删除函数包装器将是最简单的方法,但这会破坏您所做的仅将其作为服务公开的良好隔离。因此,我的建议是使用 angular-mocks。

    【讨论】:

      猜你喜欢
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 2020-03-27
      • 1970-01-01
      • 2015-07-22
      • 2014-12-12
      • 2015-10-23
      • 1970-01-01
      相关资源
      最近更新 更多