【发布时间】:2016-08-07 03:20:56
【问题描述】:
我有一个带有简单组件的 Angular2 应用程序
@Component({
selector: 'Techs',
template: '',
providers: [HTTP_PROVIDERS]
})
export class Techs {
public techs: Tech[];
constructor(http: Http) {
http
.get('src/app/techs/techs.json')
.map(response => response.json())
.subscribe(result => this.techs = result);
}
}
我的测试执行了两次:
Chrome 49.0.2623 (Mac OS X 10.11.4):执行 0 of 6 SUCCESS (0 secs / 0 secs)
LOG: '测试结束'
Chrome 49.0.2623 (Mac OS X 10.11.4):执行 6 次成功 6 次(0.182 秒 / 0.153 秒)
但是,如果我删除 Http 调用,测试只会执行一次
Chrome 49.0.2623 (Mac OS X 10.11.4):执行 6 次成功 6 次(0.182 秒 / 0.153 秒)
这是我的测试
describe('techs component', () => {
it('should render 3 elements <tech>', injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
return tcb
.createAsync(Techs)
.then(fixture => {
fixture.componentInstance.techs = [{}, {}, {}];
fixture.detectChanges();
const techs = fixture.nativeElement;
expect(techs.querySelectorAll('tech').length).toBe(3);
});
}));
});
【问题讨论】:
标签: javascript unit-testing typescript angular