我的角度测试有问题。我不知道这是个错误,还是我做错了什么。
我有两个部件。一个使用ngOnInit中的承诺,另一个则完全是空的。
使用允诺的组件如下所示:
import {Component, OnInit} from '@angular/core';
import {User, UserManager} from 'oidc-client';
@Component({
selector: 'app-test',
templateUrl: './test-promise.component.html',
styleUrls: ['./test-promise.component.css']
})
export class TestPromiseComponent implements OnInit {
private readonly userManager: UserManager;
public async ngOnInit(): Promise<any> {
return this.getDataStore('', '', '');
}
public getDataStore(url: string, key: string, keyType: string): Promise<any> {
return this.getToken();
}
public getToken(): Promise<string> {
return this.getUser().then(user => {
return user.access_token;
});
}
public getUser(): Promise<User> {
return this.userManager.getUser();
}
}在import语句中,您可以看到我有哪些依赖项,并且我使用了Oidc客户端。
我的另一个组件看起来如下(只是空的):
import { Component } from '@angular/core';
@Component({
selector: 'app-test',
templateUrl: './test.component.html',
styleUrls: ['./test.component.css']
})
export class TestComponent {
}在创建新组件时,两个组件的测试规范只需遵循标准规范即可,因此,对于使用承诺的组件,如下所示:
describe('TestPromiseComponent', () => {
let component: TestPromiseComponent;
let fixture: ComponentFixture<TestPromiseComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
TestPromiseComponent
]
}).compileComponents().then();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TestPromiseComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
fit('should create', () => {
expect(component).toBeTruthy();
});
});这是我的安排。现在非常奇怪的是,当我运行测试时,TestComponent就会失败--尽管实际上失败的是TestPromiseComponent。见这里的结果:

,所以现在要问一个大问题:为什么TestComponent会失败,即使实际上是TestPromiseComponent失败了呢?
有人能向我解释一下吗?)
编辑1:
下面是对TestComponent的测试:
describe('TestComponent', () => {
let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
TestComponent
]
}).compileComponents().then();
}));
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
fit('should create', () => {
expect(component).toBeTruthy();
});
});编辑2:,我正在运行业力和茉莉花的这些版本:
发布于 2018-09-24 10:39:30
奇怪的是,有两个beforeEach (还有一个异步),我不知道茉莉花如何调用相同的(在Par等位基因中)?它们的顺序被定义了吗?顺便说一句,您错过了异步函数中的await关键字,因此您没有正确地返回承诺,这将导致的奇怪行为用于测试:)
beforeEach应该更像:
beforeEach(() => {
return TestBed.configureTestingModule({
declarations: [
TestComponent
]
}).compileComponents().then( () => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
}));https://stackoverflow.com/questions/52475475
复制相似问题