首页 文章

当Fuse Angular APP注入模块时,业力茉莉花测试问题

提问于
浏览
1

基于AngularJS Gulp项目,在我的Fuse App(withinpixels [dot] com / themes / fuse)中设置业力茉莉花测试,并使用下一个配置文件:

karma.conf.js:

// Karma configuration
// Generated on Tue Oct 25 2016 11:50:38 GMT+0200 (CEST)

module.exports = function (config) {
    config.set({

        // base path that will be used to resolve all patterns (eg. files, exclude)
        basePath : '',

        // frameworks to use
        // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
        frameworks : ['jasmine'],

        // list of files / patterns to load in the browser
        files : [
            'bower_components/angular/angular.js',
            'bower_components/angular-mocks/angular-mocks.js',
            'src/app/**/*.json',
            'src/app/**/*.js',
            'test/*.js'
        ],

        // list of files to exclude
        exclude : [
        ],

        // preprocess matching files before serving them to the browser
        // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
        preprocessors : {
            'src/app/**/*.json' : ['json_fixtures'],
            'src/app/**/*.js' : ['coverage']
        },

        coverageReporter : {
            type : 'text-summary',
            dir : 'coverage/'
        },

        // test results reporter to use
        // possible values: 'dots', 'progress'
        // available reporters: https://npmjs.org/browse/keyword/karma-reporter
        reporters : ['progress', 'coverage'],

        // web server port
        port : 9876,

        // enable / disable colors in the output (reporters and logs)
        colors : true,

        // level of logging
        // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
        logLevel : config.LOG_INFO,

        // enable / disable watching file and executing tests whenever any file changes
        autoWatch : true,

        // start these browsers
        // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
        browsers : ['PhantomJS'], //['Chrome'],


        // Continuous Integration mode
        // if true, Karma captures browsers, runs the tests and exits
        singleRun : true,

        plugins: [
            'karma-jasmine',
            'karma-coverage',
            'karma-phantomjs-launcher',
            'karma-json-fixtures-preprocessor'
        ],


        // Concurrency level
        // how many browser should be started simultaneous
        concurrency : Infinity
    });
};

test/test.js:

describe("Index/Module", function () {
    var rootScope, scope, ctrl;

    beforeEach(angular.mock.module('fuse'));

    beforeEach(inject(function($rootScope, $controller) {
        rootScope = $rootScope;
        scope = rootScope.$new();

        ctrl = $controller("IndexController", {
            $scope: scope
        });
    }));

    it("exists", function() {
        expect(ctrl).not.toBeUndefined();
    });
});

当我执行测试时,我获取下一条错误消息:

[PhantomJS 2.1.1(Mac OS X 0.0.0)]错误:错误:[$ injector:nomod]模块'app.core'不可用!您要么错误拼写了模块名称,要么忘记加载它 . 如果注册模块,请确保将依赖项指定为第二个参数 . http://errors.angularjs.org/1.5.7/$injector/nomod?p0=app.core位于http:// localhost:9876 / base / bower_components / angular / angular.js?628a6cd3a27859d17a9a4ee87e62c9b6996ba4cb:2077发生未捕获的错误 . 可以在“输出”窗口中验证完整输出 .

任何的想法?

1 回答

  • 0

    我遇到了同样的问题 . 我通过将 app.core 添加到我的控制器依赖项中来解决它 .

    它是必需的,因为我的模块使用 ui-router 这是一个熔丝核心依赖 .

    此外,您可能需要在beforeAll钩子中声明控制器的模块: angular.moodule('my.module')

    我的控制器:

    (function ()
     {
     'use strict';
    
     angular
         .module('app.notifs', ['app.core'])
         .config(config);
    
     /** @ngInject */
     function config($stateProvider, msNavigationServiceProvider)
     {
         // State
         $stateProvider.state('app.notifs', {
             url      : '/notifications',
             views    : {
                 'content@app': {
                     templateUrl: 'app/main/apps/notifs/notifs.html',
                     controller : 'NotifsCtrl as vm'
                 }
             }
         });
    
         // Navigation
         msNavigationServiceProvider.saveItem('apps.notifs', {
             title : 'Notifications',
             icon  : 'icon-alert-box',
             state : 'app.notifs',
             weight: 2
         });
    
     }
    
     })();
    

    我的测试:

    describe('Controller: Notifs', function () {
    var $scope, $rootScope, $controller;
    
    it('Expect True to be True', function () {
        expect(true).toBe(true);
    });
    
    
    beforeEach(function () {
        module('app.notifs');
    });
    
    
    beforeEach(inject(function (_$controller_, _$rootScope_) {
        $rootScope  = _$rootScope_;
        $controller = _$controller_;
    }));
    
    it('can be instantiated', function() {
        $scope = $rootScope.$new();
        var ctrl =  $controller('NotifsCtrl', { $scope: $scope });
        expect(ctrl).not.toBe(null);
    
    });
    
    
    it('must have boolean set to false', function() {
        $scope = $rootScope.$new();
        var ctrl =  $controller('NotifsCtrl', { $scope: $scope });
        expect(ctrl.officialPriceMissing).toBe(false);
    });
    });
    

    希望这有帮助 .

相关问题