在Karma Testing中,没有定义ReferenceError:describe

时间:2015-02-25 12:04:01

标签: 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);
        });
    });
}());

1 个答案:

答案 0 :(得分:5)

您需要先加载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
});
 };