在 Angular 2 中编写最基本的单元测试?
Writing the most basic Unit test in Angular 2?
问题: 一旦我将 Angular 2 导入文件 none 我的测试就会执行。
问题:如何设置我的 karma 配置以支持 angular 两个,以便我的测试正确通过?
或
问题:如何使用用 es6 编写的 angular2 设置任何测试框架?
Git Repo (make sure you're on branch angular-2
业力:
// Karma configuration
// Generated on Mon Jun 01 2015 14:16:41 GMT-0700 (PDT)
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jspm', 'jasmine'],
// list of files / patterns to load in the browser
jspm: {
loadFiles: [
'client/app/**/*.js'
]
},
// list of files to exclude
plugins:[
'karma-jasmine',
'karma-coverage',
'karma-jspm',
'karma-chrome-launcher'
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true
});
};
JS:
"use strict";
import {ComponentAnnotation as Component, ViewAnnotation as View} from 'angular2/angular2';
import List from './list/list';
//@Component({selector: 'my-app'})
//@View({template: `<h1>{{title}}</h1>`})
class Todo{
constructor(){
this.title = 'Gym';
this.list = [new List()];
}
setTitle(newTitle){
this.title = newTitle;
}
addListItem(){
this.list.push(new List());
}
removeListItem(){
this.list.pop();
}
}
export default Todo;
Todo.spec.js:
import Todo from './todo';
describe('Todo list:', function(){
var todo;
beforeEach(function(){
todo = new Todo();
});
it('expect Todo to be present', function(){
expect(todo).not.toBe(null);
});
it('expect Todo constructor to accept a title', function(){
expect(todo.title).toEqual('Gym');
});
it('expect Todo List property to be Present', function(){
expect(todo.list).not.toBe(null);
})
it('expect Todo List property to accept a title:empty', function(){
expect(todo.list[0].title).toEqual('empty');
});
it('expect Todo Title property to accept a title change', function(){
todo.setTitle('Work');
expect(todo.title).toEqual('Work');
});
it('expect Todo List property to have an add function', function(){
todo.addListItem();
expect(todo.list.length).toEqual(2);
});
it('expect Todo to have a remove function', function(){
todo.removeListItem();
expect(todo.list.length).toEqual(0);
})
});
预期错误:
$ karma start
INFO [karma]: Karma v0.12.36 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
WARN [web-server]: 404: /favicon.ico
INFO [Chrome 43.0.2357 (Mac OS X 10.10.3)]: Connected on socket 31YT5XsHM29BDG8sYXSq with id 13157663
Chrome 43.0.2357 (Mac OS X 10.10.3): Executed 0 of 0 ERROR (0.002 secs / 0 secs)
如果我删除 angular 导入 Todo.js
$ karma start
INFO [karma]: Karma v0.12.36 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
WARN [web-server]: 404: /favicon.ico
INFO [Chrome 43.0.2357 (Mac OS X 10.10.3)]: Connected on socket 7QKCB-7aTRwNsOGfYjmG with id 71239348
Chrome 43.0.2357 (Mac OS X 10.10.3): Executed 7 of 7 SUCCESS (0.008 secs / 0.005 secs)
来自 GITTER 的更新:
@matthewharwood try loading the bundled ng2 file into your jspm Loadfiles section. with the karma-jspm plugin you can specify custom paths, so you can override the angular/angular path to point to that single bundled file. it made our tests run a lot easier. also I had to include the karma babel preprocessor and run my code thru that.
不幸的是我无法让加载文件正常工作 :c
打开调试页面后,您可以看到 angular 告诉您它缺少 reflect-metadata
包。所以我在 app/todo/todo.js
中添加了一个手动导入语句,解决了这个问题:
import Reflect from 'reflect-metadata'
import { ComponentAnnotation as Component, ViewAnnotation as View } from 'angular2/angular2';
import List from './list/list';
我现在的输出是:
node_modules/.bin/karma start --single-run
INFO [karma]: Karma v0.12.36 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
WARN [web-server]: 404: /favicon.ico
INFO [Chrome 43.0.2357 (Mac OS X 10.10.3)]: Connected on socket cGnqva8p5bf-j7L2EVzI with id 94803307
Chrome 43.0.2357 (Mac OS X 10.10.3): Executed 7 of 7 SUCCESS (0.005 secs / 0.004 secs)
问题: 一旦我将 Angular 2 导入文件 none 我的测试就会执行。
问题:如何设置我的 karma 配置以支持 angular 两个,以便我的测试正确通过?
或 问题:如何使用用 es6 编写的 angular2 设置任何测试框架?
Git Repo (make sure you're on branch angular-2
业力:
// Karma configuration
// Generated on Mon Jun 01 2015 14:16:41 GMT-0700 (PDT)
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jspm', 'jasmine'],
// list of files / patterns to load in the browser
jspm: {
loadFiles: [
'client/app/**/*.js'
]
},
// list of files to exclude
plugins:[
'karma-jasmine',
'karma-coverage',
'karma-jspm',
'karma-chrome-launcher'
],
// list of files to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// web server port
port: 9876,
// enable / disable colors in the output (reporters and logs)
colors: true,
// level of logging
// possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
logLevel: config.LOG_INFO,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true
});
};
JS:
"use strict";
import {ComponentAnnotation as Component, ViewAnnotation as View} from 'angular2/angular2';
import List from './list/list';
//@Component({selector: 'my-app'})
//@View({template: `<h1>{{title}}</h1>`})
class Todo{
constructor(){
this.title = 'Gym';
this.list = [new List()];
}
setTitle(newTitle){
this.title = newTitle;
}
addListItem(){
this.list.push(new List());
}
removeListItem(){
this.list.pop();
}
}
export default Todo;
Todo.spec.js:
import Todo from './todo';
describe('Todo list:', function(){
var todo;
beforeEach(function(){
todo = new Todo();
});
it('expect Todo to be present', function(){
expect(todo).not.toBe(null);
});
it('expect Todo constructor to accept a title', function(){
expect(todo.title).toEqual('Gym');
});
it('expect Todo List property to be Present', function(){
expect(todo.list).not.toBe(null);
})
it('expect Todo List property to accept a title:empty', function(){
expect(todo.list[0].title).toEqual('empty');
});
it('expect Todo Title property to accept a title change', function(){
todo.setTitle('Work');
expect(todo.title).toEqual('Work');
});
it('expect Todo List property to have an add function', function(){
todo.addListItem();
expect(todo.list.length).toEqual(2);
});
it('expect Todo to have a remove function', function(){
todo.removeListItem();
expect(todo.list.length).toEqual(0);
})
});
预期错误:
$ karma start
INFO [karma]: Karma v0.12.36 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
WARN [web-server]: 404: /favicon.ico
INFO [Chrome 43.0.2357 (Mac OS X 10.10.3)]: Connected on socket 31YT5XsHM29BDG8sYXSq with id 13157663
Chrome 43.0.2357 (Mac OS X 10.10.3): Executed 0 of 0 ERROR (0.002 secs / 0 secs)
如果我删除 angular 导入 Todo.js
$ karma start
INFO [karma]: Karma v0.12.36 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
WARN [web-server]: 404: /favicon.ico
INFO [Chrome 43.0.2357 (Mac OS X 10.10.3)]: Connected on socket 7QKCB-7aTRwNsOGfYjmG with id 71239348
Chrome 43.0.2357 (Mac OS X 10.10.3): Executed 7 of 7 SUCCESS (0.008 secs / 0.005 secs)
来自 GITTER 的更新:
@matthewharwood try loading the bundled ng2 file into your jspm Loadfiles section. with the karma-jspm plugin you can specify custom paths, so you can override the angular/angular path to point to that single bundled file. it made our tests run a lot easier. also I had to include the karma babel preprocessor and run my code thru that.
不幸的是我无法让加载文件正常工作 :c
打开调试页面后,您可以看到 angular 告诉您它缺少 reflect-metadata
包。所以我在 app/todo/todo.js
中添加了一个手动导入语句,解决了这个问题:
import Reflect from 'reflect-metadata'
import { ComponentAnnotation as Component, ViewAnnotation as View } from 'angular2/angular2';
import List from './list/list';
我现在的输出是:
node_modules/.bin/karma start --single-run
INFO [karma]: Karma v0.12.36 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
WARN [web-server]: 404: /favicon.ico
INFO [Chrome 43.0.2357 (Mac OS X 10.10.3)]: Connected on socket cGnqva8p5bf-j7L2EVzI with id 94803307
Chrome 43.0.2357 (Mac OS X 10.10.3): Executed 7 of 7 SUCCESS (0.005 secs / 0.004 secs)