测试服务。 TypeError:done.fail不是函数

时间:2018-05-31 08:28:11

标签: angular unit-testing testing jasmine mocking

我已经尝试过这个测试,用于测试我的服务:

它显示了这个错误:

TypeError: done.fail is not a function

测试文件

 it('should return reasonable json ssss', 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)
            expect(facts[0].details).toEqual('ffff');
        });
    })));

-     我的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: [
                { alarmnumber: 0, alarmdesc: 'All cats are lions' },
                { alarmnumber: 1, alarmdesc: 'Video 1' },
                { alarmnumber: 2, alarmdesc: 'Video 2' },
                { alarmnumber: 3, alarmdesc: 'Video 3' },
            ]
        };

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

        service.productsgetall().subscribe(facts=> {
            console.log(facts);
            console.log(facts[0]);
           // expect(facts.length).toEqual(300);
            expect(facts[0].alarmdesc).toEqual('ffff');
            done();
        },
        (error) => done.fail());
    }))();
});

image error

2 个答案:

答案 0 :(得分:1)

我自己在尝试测试需要http的简单Angular 6服务时遇到了类似情况。不确定潜在的问题是什么,但我怀疑这与将异步与注入结合使用有关。当我将测试重构为不再使用注入时,该错误消失了。

请注意,在最新版本的Angular中,如果您使用的是新的HttpClient,则上面使用的'MockBackend'已由HttpTestingController替换,请在medium.com {{ 3}}。

鉴于上面的代码,重构看起来类似于以下内容。显然,我没有要测试的原始服务详细信息。 :)

import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
describe('ProductService', () => {
    let httpMock: HttpTestingController;
    let service: ProductService;  
    beforeEach(() => {
        TestBed.configureTestingModule({
            imports: [HttpClientTestingModule],
            providers: [ProductService]
        });
        httpMock = TestBed.get(HttpTestingController);
        service = TestBed.get(ProductService);
    });
    it('should return reasonable json ssss', async(() => {
        const mockResponse = {
            data: [
                { alarmnumber: 0, alarmdesc: 'All cats are lions' },
                { alarmnumber: 1, alarmdesc: 'Video 1' },
                { alarmnumber: 2, alarmdesc: 'Video 2' },
                { alarmnumber: 3, alarmdesc: 'Video 3' },
            ]
        };
        service.productsgetall().subscribe(facts=> {
            console.log(facts);
            console.log(facts[0]);
            // expect(facts.length).toEqual(300);
            expect(facts[0].alarmdesc).toEqual('ffff');
        });
        let req = httpMock.expectOne('/your/test/url'); // <-- details not provided in your question
        expect(req.request.responseType).toEqual('json');
        req.flush(mockResponse); // send mockResponse back into the subscribe above
    }));
});

答案 1 :(得分:0)

你有一个异步测试用例,你必须使用这样的done参数:

it('should return reasonable json ssss', (done) => {

  inject([ProductService, MockBackend], async((service: ProductService, mockBackend: MockBackend) => {
    // your implementation

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

      done(); // <-- this will finish the test
    },
    (error) => done.fail());
  }))(); // <-- important, inject itself returns a function which needs to be called

});