小编Joh*_*ppe的帖子

Angular:使用MockBackend测试HTTP,是否真的需要async()?

我正在MockBackend测试依赖的代码@angular/http.
Web上的所有示例都使用异步测试设置,如下所示:
thoughtram:在Angular中使用Http测试服务

describe('getVideos()', () => {

  it('should return an Observable<Array<Video>>',
      async(inject([VideoService, MockBackend], (videoService, mockBackend) => {

      videoService.getVideos().subscribe((videos) => {
        expect(videos.length).toBe(4);
        expect(videos[0].name).toEqual('Video 0');
        expect(videos[1].name).toEqual('Video 1');
        expect(videos[2].name).toEqual('Video 2');
        expect(videos[3].name).toEqual('Video 3');

        expect("THIS TEST IS FALSE POSITIVE").toEqual(false); 
      });

      const mockResponse = {
        data: [
          { id: 0, name: 'Video 0' },
          { id: 1, name: 'Video 1' },
          { id: 2, name: 'Video 2' },
          { id: 3, name: 'Video 3' }
        ]
      };

      mockBackend.connections.subscribe((connection) => {
        connection.mockRespond(new Response(new …
Run Code Online (Sandbox Code Playgroud)

javascript typescript angular2-testing angular

5
推荐指数
1
解决办法
741
查看次数