【发布时间】: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? -
谢谢这就是我所缺少的,将此添加到答案中以便我可以接受:)
标签: angularjs gruntjs jasmine bower karma-jasmine