茉莉花匹配器函数未在 angularjs/karma 单元测试中加载

jasmine matcher functions not loading in angularjs/karma unit tests

我正在为 angularJs 应用程序编写单元测试,我正在使用 karma 测试 运行ner 和 jasmine 框架。

我正在测试一个应该从 firebase 中提取对象的函数,我已经安装了带有 karma-jasmine-matchers 插件的 jasmine-expect

我的函数如下:

describe('Controller: QuizCtrl', function () {

  // load the controller's module
  beforeEach(module('geafApp'));

  var QuizCtrl,
    scope;

  // Initialize the controller and a mock scope
  beforeEach(inject(function ($controller, $rootScope) {
    scope = $rootScope.$new();
    QuizCtrl = $controller('QuizCtrl', {
      $scope: scope
    });
  }));

  it('should contain answers object from firebase', function () {
    expect(scope.questions).toBeNonEmptyObject();
  });
  });

然后我运行:

grunt karma

到 运行 测试,但失败并出现以下错误:

    TypeError: 'undefined' is not a function (evaluating 'expect(scope.questions).toBeNonEmptyObject()')

我的package.json如下:

{
  "name": "geaf",
  "version": "0.0.1",
  "description": "German Embassy Quiz",
  "dependencies": {},
  "repository": {},
  "devDependencies": {
    "bower": "^1.3.1",
    "chalk": "^0.4.0",
    "grunt": "^0.4.5",
    "grunt-autoprefixer": "^2.0.0",
    "grunt-concurrent": "^1.0.0",
    "grunt-contrib-clean": "^0.6.0",
    "grunt-contrib-compass": "^1.0.0",
    "grunt-contrib-concat": "^0.5.0",
    "grunt-contrib-connect": "^0.9.0",
    "grunt-contrib-copy": "^0.7.0",
    "grunt-contrib-cssmin": "^0.12.0",
    "grunt-contrib-htmlmin": "^0.4.0",
    "grunt-contrib-imagemin": "^0.9.2",
    "grunt-contrib-jshint": "^0.11.0",
    "grunt-contrib-uglify": "^0.7.0",
    "grunt-contrib-watch": "^0.6.1",
    "grunt-filerev": "^2.1.2",
    "grunt-google-cdn": "^0.4.3",
    "grunt-karma": "^0.12.0",
    "grunt-newer": "^1.1.0",
    "grunt-ng-annotate": "^0.9.2",
    "grunt-svgmin": "^2.0.0",
    "grunt-usemin": "^3.0.0",
    "grunt-wiredep": "^2.0.0",
    "gulp": "^3.9.0",
    "gulp-bower": "0.0.10",
    "gulp-sass": "^2.0.4",
    "http-server": "^0.6.1",
    "jasmine-core": "^2.3.4",
    "jasmine-expect": "^1.22.3",
    "jshint-stylish": "^1.0.0",
    "karma": "^0.13.3",
    "karma-chrome-launcher": "^0.2.0",
    "karma-firefox-launcher": "^0.1.6",
    "karma-jasmine": "^0.3.6",
    "karma-jasmine-matchers": "^2.0.0-beta1",
    "karma-junit-reporter": "^0.3.3",
    "karma-ng-html2js-preprocessor": "^0.1.2",
    "karma-ng-scenario": "^0.1.0",
    "karma-phantomjs-launcher": "^0.2.0",
    "load-grunt-tasks": "^3.1.0",
    "protractor": "~0.20.1",
    "shelljs": "^0.2.6",
    "time-grunt": "^1.0.0"
  },
  "scripts": {
    "postinstall": "bower install",
    "prestart": "npm install",
    "start": "http-server -a localhost -p 8000",
    "pretest": "npm install",
    "test": "karma start karma.conf.js",
    "test-single-run": "karma start karma.conf.js  --single-run",
    "preupdate-webdriver": "npm install",
    "update-webdriver": "webdriver-manager update",
    "preprotractor": "npm run update-webdriver",
    "protractor": "protractor e2e-tests/protractor-conf.js",
    "update-index-async": "node -e \"require('shelljs/global'); sed('-i', /\/\/@@NG_LOADER_START@@[\s\S]*\/\/@@NG_LOADER_END@@/, '//@@NG_LOADER_START@@\n' + cat('app/bower_components/angular-loader/angular-loader.min.js') + '\n//@@NG_LOADER_END@@', 'app/index-async.html');\""
  },
  "engines": {
    "node": ">=0.10.0"
  }
}

我的karma.conf.js加载插件如下:

插件:[ 'karma-chrome-launcher', 'karma-firefox-launcher', 'karma-jasmine', 'karma-phantomjs-launcher', 'karma-junit-reporter', 'karma-jasmine-matchers' ],

我 运行 npm installbower install 所以功能应该正确加载但似乎没有。

非常感谢任何帮助,因为这是我第一次为它们编写 angular 应用程序和单元测试。

您还必须将 jasmine-matchers 作为框架添加到 karma.conf.js:

module.exports = function(config) {
    // ...
    frameworks: [
        'jasmine',
        'jasmine-matchers' // here
    ],

    plugins: [
        'karma-chrome-launcher',
        'karma-firefox-launcher',
        'karma-jasmine',
        'karma-phantomjs-launcher',
        'karma-junit-reporter',
        'karma-jasmine-matchers'
    ]
    // ....
};

Here 是对 jasmine-matchers.

的官方 Karma 配置示例的引用

另外,如果你没有任何插件,那么指定一个框架就足够了。