0
我正在爲使用Karma和Jasmine在Angular 1和JSPM中編寫的應用程序編寫單元測試。使用Karma/Jasmine/JSPM單元測試角度服務
這裏是我的噶配置(JSPM部分):
jspm: {
meta: {
'jspm_packages/github/angular/angular.js': {
format: 'global',
exports: 'angular'
},
'jspm_packages/github/angular/angular-mocks.js': {
format: 'global',
deps: 'angular'
}
},
loadFiles: [
'test/**/*.js'
],
serveFiles: [
'app/**/*.js'
]
},
首先,我想測試自定義的角度服務:
// imports
class Game {
constructor($rootScope, $http, $timeout, $translate, $location) {
// ...
this.$rootScope.game = this;
}
}
這裏是我的測試文件:
import { module, inject } from 'angular-mocks';
describe('this is a test', function() {
beforeEach(module('module-name'));
var game;
beforeEach(inject(function(_game_) {
game = new _game_;
}));
it('should be defined', function() {
expect(game).toBeDefined();
});
});
主要問題是我一直有這個錯誤。來自angular-mocks的inject
函數似乎不起作用,因爲我沒有定義我的變量game
,但它應該是。
PhantomJS 2.1.1 (Windows 8 0.0.0) should be defined FAILED
forEach
loadModules
createInjector
workFn
{path}/node_modules/karma-jspm/src/adapter.js:61:24
[email protected]:/{path}/jspm_packages/system-polyfills.src.js:1188:34
[email protected]:/{path}/jspm_packages/system-polyfills.src.js:1147:18
[email protected]:/{path}/jspm_packages/system-polyfills.src.js:935:20
[email protected]:/{path}/jspm_packages/system-polyfills.src.js:826:17
[email protected]:/{path}/jspm_packages/system-polyfills.src.js:102:22
[email protected]:/{path}/jspm_packages/system-polyfills.src.js:67:15
Expected undefined to be defined.
D:/{path}/node_modules/karma-jspm/src/adapter.js:61:24
[email protected]:/{path}/jspm_packages/system-polyfills.src.js:1188:34
[email protected]:/{path}/jspm_packages/system-polyfills.src.js:1147:18
[email protected]:/{path}/jspm_packages/system-polyfills.src.js:935:20
[email protected]:/{path}/jspm_packages/system-polyfills.src.js:826:17
[email protected]:/{path}/jspm_packages/system-polyfills.src.js:102:22
[email protected]:/{path}/jspm_packages/system-polyfills.src.js:67:15
所以,我不明白爲什麼它不起作用。任何線索?
這個任何解決方案?我也面臨同樣的問題 –