【问题标题】:Karma Jasmine: Executed 0 of 0 ErrorKarma Jasmine:执行 0 of 0 错误
【发布时间】:2015-07-29 18:02:35
【问题描述】:

我正在使用 Karma 和 Jasmine 进行 Angular 测试。在完成karma init 并为家庭控制器编写第一个测试后,我不断收到Executed 0 of 0 ERROR。似乎没有在文件中找到它。

module.exports = function(config) {
    config.set({

    basePath: '',

    frameworks: ['jasmine'],
    files: [
        'public/assets/libs/angular/angular.min.js',
        'bower_components/angular-mocks/angular-mocks.js',
        'public/app/app.module.js',
        'public/app/app.config.js',
        'public/app/**/*.js',
        'test/unit/**/*.spec.js'
    ],

    exclude: [
    ],

    preprocessors: {
    },

    reporters: ['progress'],

    port: 3030,

    colors: true,

    logLevel: config.LOG_INFO,

    autoWatch: true,

    browsers: ['Chrome'],

    singleRun: false

    }); //config.set
} //module.export

测试(固定)

describe('HomeController', function() {

//Inject 'app' module into this spec.
beforeEach(module('app'));

//Inject instance of Scope.
var homeController;

beforeEach(inject(function($rootScope, $controller) {
    //create a new scope
    scope = $rootScope.$new();

    //create new controller, using the newly created scope
    HomeController = $controller('HomeController', {
        $scope: scope
    });

}));

//Set expectations
it('the message should say Hello World', function(HomeController) {
    expect(scope.message).toBe('Hello World');
});


});

还有 HomeController:

(function() {
'use strict';

angular
    .module('app')
    .controller('HomeController', HomeController);

HomeController.$inject = ['$scope', '$log'];

function HomeController($scope, $log) {
    /*jshint validthis: true*/
    var vm = this;

    vm.message = 'Hello World';

} //HomeController()

})(); //Controller

感谢您的帮助。

【问题讨论】:

    标签: angularjs testing jasmine karma-runner


    【解决方案1】:

    我想通了。测试 it('...') 位于 beforeEach() 块内,因此有 0 个测试。它现在位于它自己的单独块中,如下所示。

    【讨论】:

      【解决方案2】:

      致所有遇到“Executed 0 of 0 ERROR”问题的人:

      以下是如何更好地调试此错误,因为命令行中的日志消息可能会被截断或不够有用:

      在 karma 的浏览器实例(例如 Chrome)中,在以下函数中的 karma.js 中设置断点: this.error = function (msg, url, line) { 由于msg的字符串表示会被传输到命令行日志中的作用不大,这里分析一下。

      【讨论】:

        【解决方案3】:

        在测试 typescript 时遇到同样的问题 - 我在 chrome 控制台中发现了问题:

        拒绝从“xxx”执行脚本,因为它的 MIME 类型(“video/mp2t”)不可执行。

        修复结果是添加

        mime: {
          'text/x-typescript': ['ts', 'tsx']
        }
        

        进入 karma.conf.js。

        【讨论】:

        • 你值得喝啤酒!
        猜你喜欢
        • 2019-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-21
        相关资源
        最近更新 更多