• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript testing.fakeAsync函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了TypeScript中angular2/testing.fakeAsync函数的典型用法代码示例。如果您正苦于以下问题:TypeScript fakeAsync函数的具体用法?TypeScript fakeAsync怎么用?TypeScript fakeAsync使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了fakeAsync函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的TypeScript代码示例。

示例1: describe

  describe('Search Service', () => {
    beforeEachProviders(() => {
      return [BaseRequestOptions, MockBackend, SearchService,
        provide(Http, {
          useFactory: (backend:ConnectionBackend, defaultOptions:BaseRequestOptions) => {
            return new Http(backend, defaultOptions);
          }, deps: [MockBackend, BaseRequestOptions]
        }),
      ];
    });

    it('should retrieve all search results',
      inject([SearchService, MockBackend], fakeAsync((searchService:SearchService, mockBackend:MockBackend) => {
        var res:Response;
        mockBackend.connections.subscribe(c => {
          expect(c.request.url).toBe('shared/data/people.json');
          let response = new ResponseOptions({body: '[{"name": "John Elway"}, {"name": "Gary Kubiak"}]'});
          c.mockRespond(new Response(response));
        });
        searchService.getAll().subscribe((response) => {
          res = response;
        });
        tick();
        expect(res[0].name).toBe('John Elway');
      }))
    );

    it('should filter by search term',
      inject([SearchService, MockBackend], fakeAsync((searchService:SearchService, mockBackend:MockBackend) => {
        var res:Response;
        mockBackend.connections.subscribe(c => {
          expect(c.request.url).toBe('shared/data/people.json');
          let response = new ResponseOptions({body: '[{"name": "John Elway"}, {"name": "Gary Kubiak"}]'});
          c.mockRespond(new Response(response));
        });
        searchService.search('john').subscribe((response) => {
          res = response;
        });
        tick();
        expect(res[0].name).toBe('John Elway');
      }))
    );

    it('should fetch by id',
      inject([SearchService, MockBackend], fakeAsync((searchService:SearchService, mockBackend:MockBackend) => {
        var res:Response;
        mockBackend.connections.subscribe(c => {
          expect(c.request.url).toBe('shared/data/people.json');
          let response = new ResponseOptions({body: '[{"id": 1, "name": "John Elway"}, {"id": 2, "name": "Gary Kubiak"}]'});
          c.mockRespond(new Response(response));
        });
        searchService.search('2').subscribe((response) => {
          res = response;
        });
        tick();
        expect(res[0].name).toBe('Gary Kubiak');
      }))
    );
  });
开发者ID:craigraphics,项目名称:angular2-tutorial,代码行数:59,代码来源:search.service.spec.ts


示例2: describe

describe('When starting the user app component', () => {
  
  it('should show 2 users', 
    inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
    
      let mockUserService = new MockUserService();
      mockUserService.setResponse([{
        name: 'John',
        username: 'thejohn',
        email: '[email protected]'
      }]);
      
      tcb
        .overrideDirective(UserListComponent, UserListItemComponent, MockUserListItemComponent)
        .overrideProviders(UserListComponent, [mockUserService.getProvider()])
        .createAsync(UserListComponent)
        .then((fixture: ComponentFixture) => {
          fixture.detectChanges();
          tick();
          let compiled = fixture.debugElement.nativeElement;
          expect(compiled.querySelector('user-list-item span.email')).toHaveText('Email: [email protected]');
        });
    }))
  );
  
  it('should call the spy method', 
    inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
    
      let mockUserService = new MockUserService();
      mockUserService.setResponse([{
        name: 'John',
        username: 'thejohn',
        email: '[email protected]'
      }]);
      spyOn(mockUserService, 'getAllUsers');
      
      tcb
        .overrideDirective(UserListComponent, UserListItemComponent, MockUserListItemComponent)
        .overrideProviders(UserListComponent, [mockUserService.getProvider()])
        .createAsync(UserListComponent)
        .then((fixture: ComponentFixture) => {
          fixture.detectChanges();
          tick();
          expect(mockUserService.getAllUsers).toHaveBeenCalled();
        });
    }))
  );
  
});
开发者ID:Cayan,项目名称:angular2-unit-test-app,代码行数:49,代码来源:user-list.component.spec.ts


示例3: describe

describe("Test Event Service", () => {
    //inject all third Party Mock dependencies and configure providers
    beforeEachProviders(() => {
        return [
            BaseRequestOptions,
            MockBackend,
            EventsService,
            provide(Http, {
                useFactory: (backend:ConnectionBackend, defaultOptions:BaseRequestOptions) => {
                    return new Http(backend, defaultOptions);
                }, deps: [MockBackend, BaseRequestOptions]
            }),
        ]
    });

    it('retrieves using the event ID',
        inject([EventsService, MockBackend], fakeAsync((eventsService, mockBackend) => {
                var res;
                mockBackend.connections.subscribe(c => {
                    expect(c.request.url).toBe('https://api.spotify.com/v1/tracks/TRACK_ID');
                    let response = new ResponseOptions({body: '{"id": 1, "name": "Trip to Paris"}'});
                    c.mockRespond(new Response(response));
                });
                eventsService.get(1).subscribe((_res) => {
                    res = _res;
                });
                tick();
                expect(res.name).toBe('felipe');
            }
        ))
    );
});
开发者ID:jusefb,项目名称:pay_your_firends,代码行数:32,代码来源:eventService.spec.ts


示例4: describe

 describe('and when trying to create a new user', () => {
   
   it('should try to make a POST request to the proper URL',
     inject([UserService, MockBackend], fakeAsync((service: UserService, backend: MockBackend) => {
       
       let mockUser: IUser = {
         username: 'barretodavid',
         email: '[email protected]'
       };
       
       backend.connections.subscribe((connection: MockConnection) => {
         
         expect(connection.request.url).toBe('http://jsonplaceholder.typicode.com/users');
         expect(connection.request.method).toBe(RequestMethod.Post);
         expect(connection.request.text()).toBe(JSON.stringify(mockUser));
         
         let mockResponseBody: IUser = { id: 11 };
         let response = new ResponseOptions({body: JSON.stringify(mockResponseBody)});
         connection.mockRespond(new Response(response));
       });
       
       service.createUser(mockUser);
     }))
   );
   
 });  
开发者ID:Cayan,项目名称:angular2-unit-test-app,代码行数:26,代码来源:user.service.spec.ts


示例5: describe

describe('Home', () => {
  it('should change name to "Angular" after 1s', injectAsync([TestComponentBuilder], fakeAsync((tcb) => {
    return tcb.createAsync(Home).then((fixture) => {
      const { componentInstance } = fixture;
      expect(componentInstance.name).toBe('World');
      tick(1000);
      expect(componentInstance.name).toBe('Angular');
    });
  })));

  it('should set message on button click', injectAsync([TestComponentBuilder], (tcb) => {
    return tcb.createAsync(Home).then((fixture) => {
      const { componentInstance, nativeElement } = fixture;

      expect(componentInstance.messagePreboot).toBeFalsy();
      nativeElement.querySelector('#check-preboot').click();
      expect(componentInstance.messagePreboot).toBeTruthy();
    });
  }));

  it('should lazy load service', injectAsync([TestComponentBuilder], (tcb) => {
    return tcb.createAsync(Home).then((fixture) => {
      const { componentInstance, nativeElement } = fixture;

      expect(componentInstance.messageLazyLoading).toBeFalsy();
      nativeElement.querySelector('#check-lazyloading').click();
      expect(componentInstance.messageLazyLoading).toBeFalsy();

      return new Promise(resolve => setTimeout(resolve, 100)).then(() => {
        expect(componentInstance.messageLazyLoading).toBeTruthy();
      });
    });
  }));
});
开发者ID:DavyDuDu,项目名称:angular2-universal-starter,代码行数:34,代码来源:home.spec.ts


示例6: describe

describe('TaLogin Component', () => {
  beforeEachProviders((): any[] => []);

  it('should login', inject([TestComponentBuilder], fakeAsync((tcb:TestComponentBuilder) => {
    let fixture;
    tcb
      // https://github.com/angular/angular/issues/2835
      .overrideTemplate(TaLogin, `
        <form>
          <input type="text" #username>
          <button (click)="loginSubmit(username)">Find user</button>
        </form>
      `)
      .createAsync(TaLogin).then((rootFixture) => {
        fixture = rootFixture;
      });

    tick();

    let componentInstance = fixture.componentInstance;
    let element = fixture.nativeElement;
    let username;

    componentInstance.login.subscribe((value) => {
      username = value;
    });

    element.querySelector('input').value = 'cironunes';
    element.querySelector('button').click();
    tick();
    expect(username).toBe('cironunes');
  })));
});
开发者ID:cironunes,项目名称:ng2-tests,代码行数:33,代码来源:ta-login.spec.ts


示例7: describe

 describe('searchTrack', () => {
   it('searches type and term',
     inject([SpotifyService, MockBackend], fakeAsync((svc, backend) => {
       var res;
       expectURL(backend, 'https://api.spotify.com/v1/search?q=TERM&type=track');
       svc.searchTrack("TERM").subscribe((_res) => {
         res = _res;
       });
       tick();
       expect(res.name).toBe('felipe');
     }))
   );
 });
开发者ID:Gitjerryzhong,项目名称:angular2,代码行数:13,代码来源:SpotifyService.spec.ts


示例8: describe

describe('with fake async', () => {
  beforeEachProviders(() => [LoginService, UserService]);

  it('should greet (with fakeAsync)', inject([UserService], fakeAsync((service) => {
    var greeting;
    service.getGreeting().then((value) => {
      greeting = value;
    });

    tick(2000);
    expect(greeting).toEqual('Login failure!');
  })));
});
开发者ID:SBero,项目名称:ng2-test-seed,代码行数:13,代码来源:user-service_test.ts



注:本文中的angular2/testing.fakeAsync函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
TypeScript testing.flushMicrotasks函数代码示例发布时间:2022-05-25
下一篇:
TypeScript testing.expect函数代码示例发布时间:2022-05-25
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap