【问题标题】:Karma not finding my jasmine test, TypeErrorKarma 没有找到我的 jasmine 测试,TypeError
【发布时间】:2015-07-11 17:02:43
【问题描述】:

当尝试运行我的 jasmine 测试时,该测试正在使用 test-runner karma 测试我的角度客户指令“book directive”,我收到以下输出错误:(我从每个脚本文件中收到各种 TypeErrors,甚至没有找到我的测试更不用说显示失败的测试))

业力输出

------ Discover test started ------
Error: TypeError: undefined is not an object (evaluating 'angular.module')
in file:///c:/users/ray/documents/visual%20studio%202013/projects/requirejsdemo/requirejsdemo/scripts/angular-route.js (line 24)
While Running:c:\users\ray\documents\visual studio 2013\projects\requirejsdemo\requirejsdemo\scripts\angular-route.js

Error: TypeError: undefined is not an object (evaluating 'angular.$$minErr')
in file:///c:/users/ray/documents/visual%20studio%202013/projects/requirejsdemo/requirejsdemo/scripts/angular-sanitize.js (line 8)
While Running:c:\users\ray\documents\visual studio 2013\projects\requirejsdemo\requirejsdemo\scripts\angular-sanitize.js

Error: ReferenceError: Can't find variable: define
at global code in file:///c:/users/ray/documents/visual%20studio%202013/projects/requirejsdemo/requirejsdemo/tests/newfeaturetests/directivetest.js (line 3)
While Running:c:\users\ray\documents\visual studio 2013\projects\requirejsdemo\requirejsdemo\tests\newfeaturetests\directivetest.js

Error: TypeError: Attempted to assign to readonly property.
in file:///c:/users/ray/documents/visual%20studio%202013/projects/requirejsdemo/requirejsdemo/scripts/angular-mocks.js (line 17)
While Running:c:\users\ray\documents\visual studio 2013\projects\requirejsdemo\requirejsdemo\scripts\angular-mocks.js

[Karma] [Warning] [Discover] Could not get settings for c:\users\ray\documents\visual studio 2013\projects\requirejsdemo\requirejsdemo\scripts\angular-mocks.js
[Karma] [Warning] [Discover] Could not get settings for c:\users\ray\documents\visual studio 2013\projects\requirejsdemo\requirejsdemo\scripts\angular-route.js
[Karma] [Warning] [Discover] Could not get settings for c:\users\ray\documents\visual studio 2013\projects\requirejsdemo\requirejsdemo\scripts\angular-sanitize.js
[Karma] [Warning] [Discover] Could not get settings for c:\users\ray\documents\visual studio 2013\projects\requirejsdemo\requirejsdemo\scripts\angular-scenario.js
[Karma] [Warning] [Discover] Could not get settings for c:\users\ray\documents\visual studio 2013\projects\requirejsdemo\requirejsdemo\scripts\angular.js
[Karma] [Warning] [Discover] Could not get settings for c:\users\ray\documents\visual studio 2013\projects\requirejsdemo\requirejsdemo\scripts\jasmine.js
[Karma] [Warning] [Discover] Could not get settings for c:\users\ray\documents\visual studio 2013\projects\requirejsdemo\requirejsdemo\tests\newfeaturetests\directivetest.js
========== Discover test finished: 0 found (0:00:17.4009953) ==========

karma.conf 文件

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: [
        "Scripts/angular.js",
           { pattern: "Scripts/require.js", included: false },
              { pattern: "Scripts/jasmine.js", included: false },
                 { pattern: "Scripts/angularAMD.js", included: false },
                    { pattern: "Scripts/jquery-1.8.2.js", included: false },
        { pattern: "Js/**/*.js", included: false },
        { pattern: "tests/NewFeatureTests/*.js", included: false },
        { pattern: "Js/NewFeature/*.html", include: false },

        "tests/test-main.js"
    ],


    // list of files to exclude
    exclude: [
        "Js/main.js"
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
        "Js/**.*.html": ["ng-html2js"]
    },


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

    plugins: [
        'karma-ng-html2js-preprocessor'
    ],

    // 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
})
}

茉莉花测试

'use strict'

define(['angular', 'jquery',], function (angular) {

    describe('bookDirective', function () {
      var el;

        // Load the myApp module, which contains the directive
        beforeEach(module('testModule'));
        beforeEach(module('Js/NewFeature/Book.html'));

        // Store references to $rootScope and $compile
        // so they are available to all tests in this describe block
        beforeEach(inject(function ($compile, $rootScope) {
            // The injector unwraps the underscores (_) from around the parameter names when matching
            scope = _$rootScope_;

            scope.color = "red";
            scope.title = "ghost";
            //create and compile directive
            el = angular.element("<book title='Ghost' color='red'></book>");

            // compile the dom node and apply the scope, this will return a getter function
            $compile(el)(scope);

            scope.$digest(); // update the html
        }));

        it('background colour of book should be red', function () {
            // check if background color is red
            expect(el.css('background-color').toEqual('red'));
        });

    });

});

如何让我的测试运行?

【问题讨论】:

    标签: javascript angularjs jasmine karma-runner


    【解决方案1】:

    您的基本路径是 basePath: '../',。所以,文件数组应该是

    files: [
            "../scripts/angular.js",
           "../scripts/angular-route.js",
           "../scripts/angular-mocks.js",
           "../scripts/angular-sanitize.js",
           "../scripts/angular-scenario.js",
           "../scripts/jasmine.js",
           "../tests/newfeaturetests/directivetest.js",
           "../tests/test-main.js"
        ]
    

    请检查基本路径。

    还请检查您的 index.html 文件中包含的所有脚本 JS 并将其列在 files Array 中。否则,如果其中一个脚本将导致错误对其他的依赖。

    也在你的茉莉花测试中beforeEach(module('Js/NewFeature/Book.html'));

    这是错误的。这不是使用 Angular 应用程序注册的模块名称。请检查它。删除它。

    也请参考directive testing了解更多。

    【讨论】:

      猜你喜欢
      • 2019-01-18
      • 1970-01-01
      • 2015-12-07
      • 1970-01-01
      • 2015-06-29
      • 2017-02-26
      • 2018-01-31
      • 2014-05-04
      • 2017-05-27
      相关资源
      最近更新 更多