首页 > 解决方案 > 我的 testing.spec.ts 不起作用。错误:无法从伪造的异步测试中生成 XHR。请求网址:http://xxxxxx/v1/products

问题描述

我尝试了这个测试来测试我的服务:

显示此错误:

错误:无法从伪造的异步测试中生成 XHR。请求网址: http://xxxxxx/v1/products

测试文件

it('should return reasonable json ssss', inject([ProductService, MockBackend], fakeAsync((service: ProductService, mockBackend) => {

    const mockResponse = {
        data: [
            { id: 0, details: 'All cats are lions' },
            { id: 1, details: 'Video 1' },
            { id: 2, details: 'Video 2' },
            { id: 3, details: 'Video 3' },
        ]
    };

    mockBackend.connections.subscribe(connection => {
        connection.mockRespond(new Response(
            new ResponseOptions({
                body: [
                    { id: 0, details: 'All cats are lions' },
                    { id: 1, details: 'Video 1' },
                    { id: 2, details: 'Video 2' },
                    { id: 3, details: 'Video 3' },
                ]
            })));
    });

    service.productsgetall().subscribe((facts) => {
        console.log(facts)
        expect(facts.length).toBe(4);
    });

    tick();
})));

- 我的服务.ts

public productsgetall(): Observable<Products[]> {
                ...
 return this.http.get(Api.getUrl(Api.URLS.productsgetall), {
      headers: headers
    }).map((response: Response) => {
        let res = response.json();
        if (res.StatusCode === 1) {
          this.auth.logout();
        } else {
          return res.StatusDescription.map(aa => {
            return new Products(aa);
          });
        }
  });
}

你能问我,我的代码有什么问题,如何编写好的测试?

我的编辑代码:

TypeError:done.fail 不是函数

 it('should return reasonable json ssss', (done) => {
        inject([ProductService, MockBackend], async((service: ProductService, mockBackend: MockBackend) => {
                const mockResponse = {
                data: [
                    { id: 0, details: 'All cats are lions' },
                    { id: 1, details: 'Video 1' },
                    { id: 2, details: 'Video 2' },
                    { id: 3, details: 'Video 3' },
                ]
            };
                mockBackend.connections.subscribe(connection => {
                connection.mockRespond(new Response(
                    new ResponseOptions({
                        body: [
                    { id: 0, details: 'All cats are lions' },
                    { id: 1, details: 'Video 1' },
                    { id: 2, details: 'Video 2' },
                    { id: 3, details: 'Video 3' },
                        ]
                    })));
            });

            service.productsgetall().subscribe(facts=> {
                console.log(facts);
                console.log(facts[0]);
                expect(facts[0].details).toEqual('ffff');
                done();
            });
        }))();
    });

图片

标签: angularunit-testingtestingmocking

解决方案


错误清楚地表明您不能使用fakeAsync.

使用async代替fakeAsync

it('.....', inject([ProductService, MockBackend], async((service: ProductService, mockBackend) => {

    .................

    service.productsgetall().subscribe((facts) => {
        console.log(facts)
        expect(facts.length).toBe(4);
    });

    tick();      // tick might not work with async
})));

推荐阅读