茉莉花匹配器功能未在 angularjs/karma 单元测试中加载
Posted
技术标签:
【中文标题】茉莉花匹配器功能未在 angularjs/karma 单元测试中加载【英文标题】:jasmine matcher functions not loading in angularjs/karma unit tests 【发布时间】:2015-10-23 00:56:11 【问题描述】:我正在为一个 angularJs 应用程序编写单元测试,我正在使用带有 jasmine 框架的 karma 测试运行器。
我正在测试一个应该从 firebase 中提取对象的函数,我已经安装了 jasmine-expect
和 karma-jasmine-matchers
插件。
我的功能如下:
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();
);
);
然后我运行:
咕噜声
运行测试,但失败并出现以下错误:
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-phantomjs-launcher', '业力-junit-记者', '因果报应者' ],
我已经运行了 npm install
和 bower install
,所以函数应该可以正确加载,但似乎没有。
非常感谢任何帮助,因为这是我第一次为它们编写 Angular 应用程序和单元测试。
【问题讨论】:
您是否将jasmine-matchers
作为框架添加到karma.conf.js,如here?
谢谢这就是我所缺少的,将此添加到答案中以便我可以接受:)
【参考方案1】:
您还必须将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 配置示例的参考。
另外,如果您没有任何插件,那么只需指定一个框架就足够了。
【讨论】:
感谢您的回答,这也是一个适用于任何可能需要它的人的工作示例github.com/JamieMason/Jasmine-Matchers/tree/master/examples/…以上是关于茉莉花匹配器功能未在 angularjs/karma 单元测试中加载的主要内容,如果未能解决你的问题,请参考以下文章