首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Range6-生命周期钩子中的承诺使其他测试失败。

Range6-生命周期钩子中的承诺使其他测试失败。
EN

Stack Overflow用户
提问于 2018-09-24 08:39:03
回答 1查看 500关注 0票数 1

我的角度测试有问题。我不知道这是个错误,还是我做错了什么。

我有两个部件。一个使用ngOnInit中的承诺,另一个则完全是空的。

使用允诺的组件如下所示:

代码语言:javascript
复制
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客户端。

我的另一个组件看起来如下(只是空的):

代码语言:javascript
复制
import { Component } from '@angular/core';

@Component({
  selector: 'app-test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.css']
})
export class TestComponent {

}

在创建新组件时,两个组件的测试规范只需遵循标准规范即可,因此,对于使用承诺的组件,如下所示:

代码语言:javascript
复制
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的测试:

代码语言:javascript
复制
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:,我正在运行业力和茉莉花的这些版本:

  • “茉莉花核”:"~2.99.1",
  • “茉莉花-规格记者”:"~4.2.1",
  • “业力”:"~1.7.1“
  • “业力-铬发射器”:"~2.2.0",
  • “业力-报道-伊斯坦布尔-记者”:"~2.0.0",
  • “业力-茉莉花”:"~1.1.1",
  • “业力-茉莉花-html-记者”:"^0.2.2",
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-09-24 10:39:30

奇怪的是,有两个beforeEach (还有一个异步),我不知道茉莉花如何调用相同的(在Par等位基因中)?它们的顺序被定义了吗?顺便说一句,您错过了异步函数中的await关键字,因此您没有正确地返回承诺,这将导致的奇怪行为用于测试:)

beforeEach应该更像:

代码语言:javascript
复制
beforeEach(() => {
    return TestBed.configureTestingModule({
      declarations: [
        TestComponent
      ]
    }).compileComponents().then( () => {
      fixture = TestBed.createComponent(TestComponent);
      component = fixture.componentInstance;
      fixture.detectChanges();
    });
  }));
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52475475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档