簡體   English   中英

使用requirejs,angularamd和karma時,“ undefined”不是對象(評估“ app.register.service”)

[英]'undefined' is not an object (evaluating 'app.register.service') when using requirejs, angularamd and karma

在我們的angularjs項目中,我們使用requirejs + angularAMD來管理依賴項。 該應用程序運行良好,但是使用業力運行茉莉花測試時遇到問題。

這是錯誤:

    TypeError: 'undefined' is not an object (evaluating 'app.register.service')
  at /home/bcatarino/Projects/spr/target-grunt/js/app/model/PressRelease.js:3

似乎我們的應用程序未正確注入我們的測試中。

測試的源代碼:

'use strict';

define(['angularAMD', 'pressRelease'], function(angularAMD, pressRelease) {

    describe('PressReleaseModel test', function () {

        var scope, pressReleaseModel;

        var inject = angularAMD.inject;

        inject(function (pressRelease) {
            pressReleaseModel = pressRelease;
        });


        it('Should add a non-existing recipient', function(pressRelease) {
            expect(pressReleaseModel.get().recipients.length, 0);
        });
    });
});

PressRelease的編碼如下:

define([ 'app', 'angularAMD' ], function(app, angularAMD) {

    app.register.service('pressRelease', function() {

    //Stuff going on here

    } );

});

因此,這是未定義app.register地方,我們無法找出原因。

根據要求,這是karma.conf:

module.exports = function(config) {
    config.set({
        basePath: '../../../',
        files: [
            {pattern: 'js_components/angular/angular.js', watched: false, included: false},
            {pattern: 'js_components/angular-mocks/angular-mocks.js', included: false},
            {pattern: 'js_components/angularAMD/angularAMD.js', included: false},
            {pattern: 'js_components/angular-ui-router/release/angular-ui-router.min.js', included: false},
            {pattern: 'js_components/angular-ui-utils/ui-utils.js', included: false},
            {pattern: 'js/test/test-app.js', watched: true, included: false},
            {pattern: 'js/app/model/PressRelease.js', watched: true, included: false},
            {pattern: 'js/test/model/PressReleaseModelSpec.js', watched: true, included: false},
            'js_components/requirejs/require.js',
            'js/test/test-main.js'
        ],
        exclude: [],
        plugins: [
            'karma-jasmine',
            'karma-phantomjs-launcher',
            'karma-chrome-launcher',
            'karma-requirejs'
        ],
        frameworks: ['jasmine', 'requirejs'],
        junitReporter: {
            outputFile: 'karma-test-results.xml',
            suite: ''
        },
        port: 9876,
        runnerPort: 9100,
        browsers: ['PhantomJS', 'Chrome'],
        autoWatch: true,
        preprocessors: {
            'template/**/*.html': ['html2js']
        },
        ngHtml2JsPreprocessor: {
            // strip this from the file path
            stripPrefix: 'web',
            moduleName: 'templates'
        },
        logLevel : config.LOG_INFO
    })
};

有人有什么想法嗎? 我們以這個為例,但是找不到答案: https : //github.com/marcoslin/angularAMD

謝謝

我從未設法讓ngScenario與angularAMD一起工作。 我不得不將Protractor (用於AngularJS E2E測試的新標准)用於angularAMD。

這是量角器規格文件: https : //github.com/marcoslin/angularAMD/blob/master/test/e2e/www.spec.js

您還可以在以下位置找到有關端到端測試和angularAMD的更多詳細信息:
https://github.com/marcoslin/angularAMD/issues/20

Travis現在報告構建失敗,但是測試在我的本地計算機上運行良好。

我通過從單元測試中刪除AngularAMD來解決它。 我創建了一個橋,該橋將app.register.method重定向到app.method ,其中app是Angular模塊。

define([
    'angularMocks',
    'angular'
], function () {
    'use strict';
    var myapp = angular.module('SPR', []);

    myapp.register = {};
    var enhanceModule = function (method) {
        var register = myapp.register;
        register[method] = angular.bind(myapp, myapp[method]);
    };

    angular.forEach([
        'controller',
        'directive',
        'filter',
        'factory',
        'service',
        'constant',
        'value',
        'animation']
    , enhanceModule);

    console.info('Enhanced angular.module');
    return angular.module('SPR');
});

這是測試:

define(['angularMocks', 'app', 'js/app/model/PressRelease'], function (mocks) {

    describe('Press Release model', function () {

        var service;
        beforeEach(mocks.module('SPR'));
        beforeEach(mocks.inject(function (PressRelease) {
            service = PressRelease;
        }));

        it('should do something', function () {
            var res = service.serviceCall();
            expect(res).toBe(1);
        });

    });
});

暫無
暫無

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

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