繁体   English   中英

即使成功,如何避免“错误:规范方法没有期望”?

[英]How to avoid “ERROR: Spec method has no expectations” even when success?

我有 Angular HTTP 服务,可以进行一些 HTTP 调用。 这是我的服务示例:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class APIService {
    constructor(private readonly http: HttpClient) {}

    public async getUsers<T>(): Promise<T[]> {
        return await this.http.get<T[]>('/api/users').toPromise();
    }
}

这是我的APIServicespec.ts文件:

import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { APIService } from './api.service';

describe('API Service', () => {
    let service: APIService;
    let controller: HttpTestingController;

    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [HttpClientTestingModule],
            providers: [APIService]
        });

        service = TetsBed.get(APIService);
        controller = TestBed.get(HttpTestingController);
    });

    it('should create the API service', () => {
        expect(service).toBeTruthy();
    });

    it('should get users', () => {
        const expected = [{id: 1, email: 'email1@domain.tld'},
                          {id: 2, email: 'email2@domain.tld'}];

        service.getUsers<MyUserClass>().then((res) => {
            expect(res).toEqual(expected);
            controller.verify();
        });

        controller.expectOne('/api/users').flush(expected);
    });

});

一切似乎都有效,因为当我运行ng test时,我有这样的消息:

ERROR: 'Spec 'API Service should get users' has no expectations.'
HeadlessChrome 77.0.3865 (Windows 10.0.0): Executed 13 of 15 SUCCESS (0 secs / 0.256 secs)
HeadlessChrome 77.0.3865 (Windows 10.0.0): Executed 15 of 15 SUCCESS (0.42 secs / 0.28 secs)
TOTAL: 15 SUCCESS
TOTAL: 15 SUCCESS
TOTAL: 15 SUCCESS

当我在下一个代码下更改某些内容时,出现错误。

.then((res) => {
    // expect(res).toEqual(expected);
    expect(res).toBeNull();
    controller.verify();
});

所以上面的代码很好并且可以工作。

然后,为什么我有一条错误消息ERROR: 'Spec 'API Service should get users' 没有期望。 ? 有任何想法吗?

我想,async/await 可能会导致这个问题,因为我实际上并没有等待它,但是,它执行时没有错误,当我输入错误的数据时,它就会失败。

我还尝试将所有内容包装到async / fakeAsync方法中 - 不走运。

好吧,实际上(done)解决了我的问题;

import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
import { APIService } from './api.service';

describe('API Service', () => {
    let service: APIService;
    let controller: HttpTestingController;

    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [HttpClientTestingModule],
            providers: [APIService]
        });

        service = TetsBed.get(APIService);
        controller = TestBed.get(HttpTestingController);
    });

    it('should create the API service', () => {
        expect(service).toBeTruthy();
    });

    it('should get users', (done) => {
        const expected = [{id: 1, email: 'email1@domain.tld'},
                          {id: 2, email: 'email2@domain.tld'}];

        service.getUsers<MyUserClass>().then((res) => {
            expect(res).toEqual(expected);
            controller.verify();
            done();
        });

        controller.expectOne('/api/users').flush(expected);
    });

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM