Jasmine 角度测试异步承诺

问题描述 投票:0回答:1

我需要在 Jasmin 中测试异步承诺,但我不知道该怎么做,因为 Angular 每次都会返回错误。

myService.ts

async getNewContext(): Promise<IContext> {
    const test1 = await this.service1.function();
    const test2 = await this.service2.function();
    const test3 = await this.service3.function();
    const test4 = await this.service4.function();

    const cc = {
      .
      .
      .
    };

    this.setContext = cc;

    return cc;
  }

组件.ts

setFeatures() {
    this.myService.getNewContext().then((response) => {});
}

组件.spect.ts

.
.
.
beforeEach(waitForAsync(() => {
    TestBed.configureTestingModule({
      declarations: [...],
      imports: [...],
      providers: [
        {
          provide: myService,
          useValue: {
            select: () => [],
            getNewContext: () => [],
          },
        }
      ],
    }).compileComponents();
  }));

我收到一条错误消息“getNewContext 不是函数”。请问有什么想法吗?

angular promise jasmine angular-test
1个回答
0
投票

使函数返回一个promise,

then
仅在返回promise时才存在。

beforeEach(waitForAsync(() => {
    TestBed.configureTestingModule({
      declarations: [...],
      imports: [...],
      providers: [
        {
          provide: myService,
          useValue: {
            select: () => [],
            getNewContext: () => Promise.resolve(1),
          },
        }
      ],
    }).compileComponents();
  }));
© www.soinside.com 2019 - 2024. All rights reserved.