javascript - 在 Karma 测试中,ReferenceError : describe is not defined

标签 javascript angularjs karma-jasmine

我正要在我的项目中进行单元测试。 我写的只是一个简单的测试代码。 但是,出现了奇怪的消息: ReferenceError:描述未定义。

我怎样才能克服这个问题?

这是我的代码:

'use strict';

(function() {
    //Cal test Controller Spec
    describe('Cal test Controller Tests',function(){
        //Initialize global variables
        var CalTestController,
            scope;

        //Then we can start by loading the main application module
        beforeEach(module(ApplicationConfiguration.applicationModuleName));

        //The injector ignores leading and trailing underscores here(i.e._$httpBackend_).
        //This allows us to inject a service but then attach to it to a variable.
        //with the same name as the service.
        beforeEach(inject(function($controller,$rootScope){
            //Set a new global scope
            scope=$rootScope.$new();

            //Initialize the Cal test controller.
            CalTestController = $controller('CalController',{
                $scope:scope
            });
        }));
        var a;
        it('contains spec with an expectation',inject(function(){
            a = true;
            expect(a).toBe(true);
        }));
    });
    describe("The 'toBe' matcher compares with===",function(){
        it("and has a positive case",function(){
            expect(true).toBe(true);
        });
        it("and can have a negative case",function(){
            expect(false).not.toBe(true);
        });
    });
}());

最佳答案

您需要先加载 jasmine 引用。

我已经为您添加了一个小示例配置。

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', 'requirejs'],

    // list of files / patterns to load in the browser
    files: [
        {pattern: 'src/test/js/extlib/jquery.191.js', included: true},
        **LOAD MORE**
        {pattern: 'src/main/js/src/**/*.js', included: false},
        {pattern: 'src/test/js/spec/src/**/*.spec.js', included: false}
    ],



    // 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: ['Firefox', 'Chrome', 'IE'],

    // If browser does not capture in given timeout [ms], kill it
    captureTimeout: 60000,

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

关于javascript - 在 Karma 测试中,ReferenceError : describe is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28718584/

相关文章:

java - 带有 restwebservice 的 Spring MVC 和 JPA

javascript - 当我更改选择选项时,ng-model 没有更新

css - 带有angularjs的动态样式表

javascript - 使用 karma 运行单元测试时出错

karma-jasmine - Karma + Ionic 4-无值访问器,用于带有名称的表单控制

javascript - 在 jsx 表达式中使用扩展运算符

javascript - Google+ 分享 onendinteraction 回调函数不会触发

javascript - 将谷歌翻译添加到网站

javascript - (HTML/CSS) 以有效的方式定位文字/图片,以便考虑窗口收缩

angular - 如何对创建导入类对象的 Angular 方法进行单元测试