在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML5  網絡安全/ 利用 jasmine + karma編寫angularjs的controller

利用 jasmine + karma編寫angularjs的controller單元測試用例 一直報錯

我利用 jasmine + karma編寫angularjs的controller單元測試用例,一直報錯:Error: [ng:areq] Argument 'roleCtrl' is not a function, got undefined,
大伙幫忙看看啥問題?

源碼如下:

(1):roleCtrl

import ngApp from '../components/app';

export default ngApp.controller('roleCtrl', ['$scope', function($scope) {

    $scope.columns = [
        { "title": "角色", "attribute": "roleName", "type": "data" },
        { "title": "權限說明", "attribute": "description", "type": "data" }
    ];

    $scope.url = "/api/role/search/find";
    $scope.catalog = "role";
    $scope.param = { "systemCode": localStorage.syscode, page: 0, size: 10, isDelete: false };
}]);

(2):spec.js

'use strict';
angular.module('ngApp', ['ui.router'])
    .value('rolefactory', 'rolefactory')
    .value('dialogService', 'dialogService');

describe("ngApp Unit Test", function() {
    beforeEach(angular.mock.module("ngApp"));
    var scope, ctrl;
    beforeEach(inject(function($controller, $rootScope) {
        //模擬生成scope, $rootScope是angular中的頂級scope,angular中每個controller中的     
        //scope都是rootScope new出來的
        scope = $rootScope.$new();
        //模擬生成controller 并把先前生成的scope傳入以方便測試
        ctrl = $controller('roleCtrl', { $scope: scope });
    }));
    // controller test
    describe('role controller test', function() {
        it('Add Cat Controller test', function() {
            expect(50).toBe(50);
        })
    })

});

(3):karma配置

// Karma configuration
// Generated on Tue Mar 21 2017 12:39:40 GMT+0800 (CST)

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: [
            'node_modules/angular/angular.js',
            'node_modules/angular-ui-router/release/angular-ui-router.js',
            'node_modules/angular-mocks/angular-mocks.js',
            // 'app/components/*.js',
            // 'app/controller/*.js',
            // 'app/components/**/*.js',
            'app/controller/rolectrl.js',
            'unit/controller/*.js'
        ],

        // list of files to exclude
        exclude: ['karma.conf.js'],


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


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


        // 設置輸出測試內容文件的信息
        // junitReporter: {
        //     outputFile: 'test_out/unit.xml',
        //     suite: 'unit'
        // },


        // 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,

        // plugins: [
        //     'karma-chrome-launcher',
        //     'karma-firefox-launcher',
        //     'karma-jasmine',
        //     'karma-junit-reporter'
        // ],
        // start these browsers
        // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
        browsers: ['Chrome'],



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

        // Concurrency level
        // how many browser should be started simultaneous
        concurrency: Infinity,
        //webpack: require('./webpack.config')(true)
        webpack: {
            module: {
                loaders: [{
                        test: /\.js$/,
                        loader: 'babel',
                        exclude: /node_modules/,
                        query: {
                            presets: ['es2015']
                        }
                    }]
                    //plugins: ['node_modules/angular/angular.js', 'node_modules/angular-ui-router/release/angular-ui-router.js', 'node_modules/angular-mocks/angular-mocks.js']
            }
        }
    })
}

錯誤截圖:

clipboard.png

回答
編輯回答
賤人曾

沒人遇到這種問題么?

2017年3月31日 04:09
編輯回答
懶豬

謝謝邀請,大概看了看,可能是spec.js里面沒有roleCtrl 聲明的代碼吧,你加上試試?

這方面經驗較少,我表示我一直沒有使用過ng中的$mock服務寫一些單元測試,不是不想寫,是真沒有時間寫。

2017年7月21日 23:11