利用 jasmine + karma编写angularjs的controller单元测试用例 一直报错

发布于 2022-09-06 04:51:43 字数 4830 浏览 20 评论 0

我利用 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

破晓 2022-09-13 04:51:43

没人遇到这种问题么?

南冥有猫 2022-09-13 04:51:43

谢谢邀请,大概看了看,可能是spec.js里面没有roleCtrl声明的代码吧,你加上试试?

这方面经验较少,我表示我一直没有使用过ng中的$mock服务写一些单元测试,不是不想写,是真没有时间写。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文