【问题标题】:Angular testing Jasmine failsAngular 测试 Jasmine 失败
【发布时间】:2016-08-01 14:41:21
【问题描述】:

我是测试新手,有 Angular 经验。我开发了一个 Angular 应用程序,想向它添加测试以掌握 Jasmine 和 Karma。所以我设置了 Karma,在我的 Angular 应用程序中的一项服务中添加了一个简单的“getGreeting”功能,并添加了一个带有 Jasmine 测试的测试文件 (/test/UtilsService.spec.js)。它失败是因为服务未定义(添加了 angular-mocks.js)。这是我的代码:

karma.conf.js:

// 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/**/*.js',
    'app/**/*.js',
    'test/UtilsService.spec.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: {
},


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


// 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: ['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

/test/UtilsService.spec.js 中的测试文件:

describe('getGreeting',function(){
    var UtilsService;
    beforeEach(angular.mock.module('app.ontdekJouwTalent'));
    beforeEach(inject(
        function(_UtilsService_) {
            UtilsService = _UtilsService_;
        }
    ));
    it('should test a dummy function',
        function(){
            expect(1+1).toEqual(2);
            expect(UtilsService.getGreeting("Marc")).toEqual("Hello Marc");
        }
    )
});

请注意,我注释掉了加载/测试服务,但只加载了应用程序:app.ontdekJouwTalent。 /app/shared/services/UtilsService.js中的UtilsService

angular.module('app.ontdekJouwTalent').
service('UtilsService',['AppConfig',function(AppConfig){
    this.debug = function(data){
        if(AppConfig.APPCONSTANTS_ISLOCAL){
            return data;
        }
    }

    this.getGreeting = function(name){
        return "Hello " + name;
    }
}])

Angular 应用程序在其他地方定义 - 在 /app/app.js 中,如下所示:

angular.module('app.ontdekJouwTalent', [
    'angular-storage',
    'ui.bootstrap',
    'ui.router',
    'ui.router.modal',
    'xeditable',
    'angular-confirm',
    'ui.select',
    'ngSanitize',
    'angular-growl',
    'ngAnimate'
])

在带有“业力启动”的 cmd 窗口中从 webroot 目录 (wwwroot) 运行它时,我得到了

I:\www\ontdekJouwTalent\wwwroot>karma start
04 08 2016 19:23:32.633:WARN [karma]: No captured browser, open http://localhost:9876/
04 08 2016 19:23:32.756:INFO [karma]: Karma v1.1.2 server started at http://localhost:9876/
04 08 2016 19:23:32.757:INFO [launcher]: Launching browser Chrome with unlimited concurrency
04 08 2016 19:23:32.769:INFO [launcher]: Starting browser Chrome
04 08 2016 19:23:38.634:INFO [Chrome 51.0.2704 (Windows 10 0.0.0)]: Connected on socket /#CXn5vEn8tQBLJ23oAAAA with id 50497607
Chrome 51.0.2704 (Windows 10 0.0.0) ERROR
  Uncaught ReferenceError: module is not defined
  at node_modules/abbrev/abbrev.js:2

所以“模块”是未定义的......我不知道如何解决这个问题。这里有什么问题?

【问题讨论】:

  • 一起调试吧。请把console.log(UtilsService) 放在UtilsService = _UtilsService_; 之后,告诉我们你看到了什么
  • 我已经有一段时间没有进行 Angular 测试了,但您肯定需要从服务中返回一些东西吗?
  • 您添加的内容与您之前编写的内容不同。 inject 是否立即调用函数?不应该这样。
  • 另外,你应该对你之前的问题做点什么,否则没有人愿意帮助你。

标签: angularjs unit-testing jasmine


【解决方案1】:

好的,原来我必须在 karma.conf.js 文件部分添加 很多 个文件! 具体来说:

    // list of files / patterns to load in the browser
files: [
    'node_modules/angular/angular.js',
    'node_modules/angular-ui-bootstrap/dist/ui-bootstrap-tpls.js',
    'node_modules/ui-select/dist/select.js',
    'node_modules/angular-mocks/angular-mocks.js',
    'node_modules/angular-growl-v2/build/angular-growl.min.js',
    'node_modules/angular-ui-router/release/angular-ui-router.min.js',
    'node_modules/angular-storage/dist/angular-storage.min.js',
    'node_modules/angular-animate/angular-animate.min.js',
    'node_modules/angular-sanitize/angular-sanitize.min.js',
    'node_modules/angular-xeditable/dist/js/xeditable.min.js',
    'node_modules/angular-ui-router/release/angular-ui-router.min.js',
    'node_modules/angular-ui-router-uib-modal/angular-ui-router-uib-modal.js',
    'node_modules/angular-confirm/angular-confirm.js',
    'node_modules/angular-aria/angular-aria.min.js',
    'node_modules/angular-messages/angular-messages.min.js',
    'app/app.js',

    'app/shared/services/APIService.js',
    'app/shared/services/SessionService.js',
    'app/login/AuthService.js',
    'app/translation/TranslationService.js',
    'app/shared/services/UtilsService.js',
    'test/UtilsService.spec.js'
],

不太清楚为什么我必须包含这些服务...我的 UtilsService 肯定不需要它们。我的应用包含 7 个服务,而不是 5 个。另外,为什么不需要也包含控制器(应用使用 +9)?

【讨论】:

    猜你喜欢
    • 2017-12-25
    • 2018-09-30
    • 1970-01-01
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    • 2018-04-11
    • 2020-06-10
    • 1970-01-01
    相关资源
    最近更新 更多