我正在为angularJs app编写单元测试,我使用karma测试运行器和jasmine框架。
我正在测试一个应该从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();
});
});
然后我跑了:
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按如下方式加载插件:
插件:[ '因缘 - 铬 - 发射&#39 ;, '果报火狐-发射&#39 ;, '果报茉莉&#39 ;, '果报phantomjs-发射&#39 ;, '卡玛的JUnit记者&#39 ;, '果报茉莉匹配器' ],
我已同时运行了npm install
和bower install
,因此这些功能应该正确加载,但似乎没有。
我非常感谢任何帮助,因为这是我第一次为他们编写角度应用和单元测试。
答案 0 :(得分:3)
您还必须将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配置示例的引用。
另外,如果你没有任何插件,那么仅仅指定一个框架就足够了。