簡體   English   中英

在沒有業力的情況下進行茉莉​​角測試

[英]running jasmine angular tests without karma

我有一個工作角度應用程序,我需要編寫單元測試(我通常知道它是另一種方式)。

每當我運行我的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 () {

    });

});
}());

我知道這是一個非常古老的問題,但萬一它仍然有效或其他人有同樣的問題:你沒有將ApplicationService.js中的任何ApplicationService暴露給外界,因為你放了一個(function(){ ...})()圍繞它,所以沒有辦法在你的測試中實例化它。

如果你想以agular的方式測試它,你應該使用angular-mocks,特別是它提供的module()inject()函數。 這將允許您將服務注入到測試中。

如果要以“POJO”方式測試,則應以某種方式公開ApplicationService。 刪除函數包裝器將是最簡單的方法,但這會破壞您僅將其作為服務公開的良好隔離。 因此我的建議是使用角度模擬。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM