我尝试根据FountainJS种子(带有bower,js es5,sass和hello world应用程序的基本角度1.6应用程序)自定义角度项目。
问题是当我运行gulp test
时测试失败:
PhantomJS 2.1.1 (Windows 7 0.0.0) hello component should render hello world FAILED
forEach@bower_components/angular/angular.js:402:24
loadModules@bower_components/angular/angular.js:4880:12
createInjector@bower_components/angular/angular.js:4802:30
WorkFn@bower_components/angular-mocks/angular-mocks.js:3161:60
loaded@http://localhost:9876/context.js:151:17
bower_components/angular/angular.js:4921:53
PhantomJS 2.1.1 (Windows 7 0.0.0): Executed 1 of 1 (1 FAILED) ERROR (0.004 secs / 0.011 secs)
15 03 2017 14:44:20.072:DEBUG [karma]: Run complete, exiting.
15 03 2017 14:44:20.073:DEBUG [launcher]: Disconnecting all browsers
15 03 2017 14:44:20.082:DEBUG [launcher]: Process PhantomJS exited with code 0
15 03 2017 14:44:20.082:DEBUG [temp-dir]: Cleaning temp dir C:\Users\marmin-t\AppData\Local\Temp\karma-62828717
15 03 2017 14:44:20.089:DEBUG [launcher]: Finished all browsers
[14:44:20] 'karma:single-run' errored after 4.04 s
[14:44:20] Error: Failed 1 tests.
at C:\xxx\angular1-front-seed\gulp_tasks\karma.js:15:22
at removeAllListeners (C:\xxx\angular1-front-seed\node_modules\karma\lib\server.js:380:7)
at Server.<anonymous> (C:\xxx\angular1-front-seed\node_modules\karma\lib\server.js:391:9)
at Server.g (events.js:260:16)
at emitNone (events.js:72:20)
at Server.emit (events.js:166:7)
at emitCloseNT (net.js:1537:8)
at nextTickCallbackWith1Arg (node.js:431:9)
at process._tickDomainCallback (node.js:394:17)
[14:44:20] 'test' errored after 4.46 s
我使用自定义引导程序进行异步配置加载,就像我在index.html
中看到的那样:
<script>
// inspired from https://blog.mariusschulz.com/2014/10/22/asynchronously-bootstrapping-angularjs-applications-with-server-side-data
// Async load config/config.json
(function() {
var app = angular.module('app', ['ui.router']);
fetchData().then(bootstrapApplication);
function fetchData() {
var initInjector = angular.injector(["ng"]);
var $http = initInjector.get("$http");
var $log = initInjector.get("$log");
return $http.get("/config/config.json").then(function(response) {
app.constant("config", response.data);
}, function(errorResponse) {
$log.error('an error occurred while loading configuration', error);
});
}
function bootstrapApplication() {
angular.element(document).ready(function() {
angular.bootstrap(document, ["app"]);
document.body.className = 'ng-app';
});
}
}());
</script>
我有自己生成器生成的默认hello组件:
hello.js
:
angular
.module('app')
.component('app', {
templateUrl: 'app/hello.html',
controller: function(config) {
this.hello = 'Hello World!';
this.config = config;
}
});
hello.html
<h1>{{ $ctrl.hello }}</h1>
<h2>Config:</h2>
<pre>{{ $ctrl.config | json }}</pre>
和hello.spec.js
describe('hello component', function() {
beforeEach(module('app', function($provide) {
$provide.factory('app', function() {
return {
templateUrl: 'app/hello.html'
};
});
}));
it('should render hello world', angular.mock.inject(function($rootScope, $compile) {
var element = $compile('<app>Loading...</app>')($rootScope);
$rootScope.$digest();
var h1 = element.find('h1');
expect(h1.html()).toEqual('Hello World!');
}));
});
我启用了karma的调试日志级别,这是在测试启动时加载的文件:
15 03 2017 14:44:16.614:DEBUG [web-server]: Instantiating middleware
15 03 2017 14:44:16.691:WARN [watcher]: Pattern "C:/xxx/angular1-front-seed/src/assets/**/*" does not match any file.
15 03 2017 14:44:16.715:DEBUG [preprocessor.html2js]: Processing "C:/xxx/angular1-front-seed/src/app/hello.html".
15 03 2017 14:44:16.715:DEBUG [preprocessor.html2js]: Processing "C:/xxx/angular1-front-seed/src/index.html".
15 03 2017 14:44:16.739:DEBUG [karma-angular-filesort]: Sorted files:
C:/xxx/angular1-front-seed/node_modules/jasmine-core/lib/jasmine-core/jasmine.js
C:/xxx/angular1-front-seed/node_modules/karma-jasmine/lib/boot.js
C:/xxx/angular1-front-seed/node_modules/karma-jasmine/lib/adapter.js
C:/xxx/github/angular1-front-seed/node_modules/karma-phantomjs-shim/shim.js
C:/xxx/github/angular1-front-seed/bower_components/angular/angular.js
C:/xxx/angular1-front-seed/bower_components/angular-ui-router/release/angular-ui-router.js
C:/xxx/angular1-front-seed/bower_components/angular-mocks/angular-mocks.js
C:/xxx/angular1-front-seed/src/app/hello.html.js
C:/xxx/angular1-front-seed/src/index.html.js
C:/xxx/angular1-front-seed/.tmp/routes.js
C:/xxx/angular1-front-seed/.tmp/app/hello.js
C:/xxx/angular1-front-seed/.tmp/app/hello.spec.js
缺少什么?任何想法?
THX!