當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript testing.fakeAsync函數代碼示例

本文整理匯總了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: 'pepe@gmail.com'
      }]);
      
      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: pepe@gmail.com');
        });
    }))
  );
  
  it('should call the spy method', 
    inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
    
      let mockUserService = new MockUserService();
      mockUserService.setResponse([{
        name: 'John',
        username: 'thejohn',
        email: 'pepe@gmail.com'
      }]);
      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: 'barretollano@gmail.com'
       };
       
       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;未經允許,請勿轉載。