我的testing.spec.ts不起作用。错误:无法在假异步测试中制作XHR。请求网址:http:// xxxxxx / v1 / products

时间:2018-05-30 13:29:07

标签: angular unit-testing testing mocking

我尝试过此测试来测试我的服务:

显示此错误:

  

错误:无法在假异步测试中制作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();
})));

-     我的service.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();
            });
        }))();
    });

image

1 个答案:

答案 0 :(得分:0)

错误清楚地表明您无法使用fakeAsync生成XHR请求。

使用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
})));