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


TypeScript testing.withProviders函數代碼示例

本文整理匯總了TypeScript中@angular/core/testing.withProviders函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript withProviders函數的具體用法?TypeScript withProviders怎麽用?TypeScript withProviders使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了withProviders函數的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。

示例1: describe

describe('Http-HeroService (mockBackend)', () => {

  beforeEachProviders(() => [
    HTTP_PROVIDERS,
    { provide: XHRBackend, useClass: MockBackend }
  ]);

  it('can instantiate service when inject service',
    withProviders(() => [HeroService])
      .inject([HeroService], (service: HeroService) => {
        expect(service instanceof HeroService).toBe(true);
  }));


  it('can instantiate service with "new"', inject([Http], (http: Http) => {
    expect(http).not.toBeNull('http should be provided');
    let service = new HeroService(http);
    expect(service instanceof HeroService).toBe(true, 'new service should be ok');
  }));


  it('can provide the mockBackend as XHRBackend',
    inject([XHRBackend], (backend: MockBackend) => {
      expect(backend).not.toBeNull('backend should be provided');
  }));

  describe('when getHeroes', () => {
      let backend: MockBackend;
      let service: HeroService;
      let fakeHeroes: HeroData[];
      let response: Response;


      beforeEach(inject([Http, XHRBackend], (http: Http, be: MockBackend) => {
        backend = be;
        service = new HeroService(http);
        fakeHeroes = makeHeroData();
        let options = new ResponseOptions({status: 200, body: {data: fakeHeroes}});
        response = new Response(options);
      }));

      it('should have expected fake heroes (then)', async(inject([], () => {
        backend.connections.subscribe((c: MockConnection) => c.mockRespond(response));

        service.getHeroes().toPromise()
        // .then(() => Promise.reject('deliberate'))
          .then(heroes => {
            expect(heroes.length).toEqual(fakeHeroes.length,
              'should have expected no. of heroes');
          });
      })));

      it('should have expected fake heroes (Observable.do)', async(inject([], () => {
        backend.connections.subscribe((c: MockConnection) => c.mockRespond(response));

        service.getHeroes()
          .do(heroes => {
            expect(heroes.length).toEqual(fakeHeroes.length,
              'should have expected no. of heroes');
          })
          .toPromise();
      })));


      it('should be OK returning no heroes', async(inject([], () => {
        let resp = new Response(new ResponseOptions({status: 200, body: {data: []}}));
        backend.connections.subscribe((c: MockConnection) => c.mockRespond(resp));

        service.getHeroes()
          .do(heroes => {
            expect(heroes.length).toEqual(0, 'should have no heroes');
          })
          .toPromise();
      })));

      it('should treat 404 as an Observable error', async(inject([], () => {
        let resp = new Response(new ResponseOptions({status: 404}));
        backend.connections.subscribe((c: MockConnection) => c.mockRespond(resp));

        service.getHeroes()
          .do(heroes => {
            fail('should not respond with heroes');
          })
          .catch(err => {
            expect(err).toMatch(/Bad response status/, 'should catch bad response status code');
            return Observable.of(null); // failure is the expected test result
          })
          .toPromise();
      })));
  });
});
開發者ID:AndresRicardoTorres,項目名稱:angular.io,代碼行數:91,代碼來源:http-hero.service.spec.ts

示例2: describe

describe('HeroService', () => {

    beforeEach(() => {
        addProviders([
            HTTP_PROVIDERS,
            HeroService,
            { provide: XHRBackend, useClass: MockBackend }
        ]);
    });

    it('can instantiate service when inject service',
        withProviders(() => [HeroService])
            .inject([HeroService], (service: HeroService) => {
                expect(service instanceof HeroService).toBe(true);
            }));

    it('can instantiate service with "new"', inject([Http], (http: Http) => {
        expect(http).not.toBeNull('http should be provided');
        let service = new HeroService(http);
        expect(service instanceof HeroService).toBe(true, 'new service should be ok');
    }));

    it('can provide the mockBackend as XHRBackend',
        inject([XHRBackend], (backend: MockBackend) => {
            expect(backend).not.toBeNull('backend should be provided');
        }));

    describe('getHeroes', () => {
        let backend: MockBackend;
        let service: HeroService;
        let fakeHeroes: HeroData[];
        let response: Response;

        beforeEach(inject([Http, XHRBackend], (http: Http, be: MockBackend) => {
            backend = be;
            service = new HeroService(http);
            fakeHeroes = makeHeroData();
            let options = new ResponseOptions({ status: 200, body: fakeHeroes });
            response = new Response(options);
        }));

        it('should have expected fake heroes', async(inject([], () => {
            backend.connections.subscribe((c: MockConnection) => c.mockRespond(response));

            service.getHeroes()
                // .then(() => Promise.reject('deliberate'))
                .then(heroes => {
                    expect(heroes.length).toEqual(fakeHeroes.length,
                        'should have expected no. of heroes');
                });
        })));

        it('should be OK returning no heroes', async(inject([], () => {
            let resp = new Response(new ResponseOptions({ status: 200, body: [] }));
            backend.connections.subscribe((c: MockConnection) => c.mockRespond(resp));

            service.getHeroes()
                .then(heroes => {
                    expect(heroes.length).toEqual(0, 'should have no heroes');
                });
        })));
    });

    describe('save', () => {
        it('should call PUT when hero has id', async(inject([HeroService, XHRBackend], (heroService: HeroService, mockBackend: MockBackend) => {
            var hero = new Hero();
            hero.name = 'George';
            hero.id = 5;
            var calledRequestMethod: RequestMethod;
            mockBackend.connections.subscribe((c: MockConnection) => {
                calledRequestMethod = c.request.method;
                let options = new ResponseOptions({ status: 200, body: hero });
                var response = new Response(options);
                c.mockRespond(response);
            });
            heroService.save(hero)
                .then(() => {
                    expect(calledRequestMethod).toBe(RequestMethod.Put);
                });
        })));

        it('should call POST when hero has no id', async(inject([HeroService, XHRBackend], (heroService: HeroService, mockBackend: MockBackend) => {
            var hero = new Hero();
            hero.name = 'George';
            var calledRequestMethod: RequestMethod;
            mockBackend.connections.subscribe((c: MockConnection) => {
                calledRequestMethod = c.request.method;
                hero.id = 5;
                let options = new ResponseOptions({ status: 200, body: hero });
                var response = new Response(options);
                c.mockRespond(response);
            });
            heroService.save(hero)
                .then(() => {
                    expect(calledRequestMethod).toBe(RequestMethod.Post);
                });
        })));
    });
});
開發者ID:mmanela,項目名稱:NetCoreHeroes,代碼行數:99,代碼來源:hero.service.spec.ts


注:本文中的@angular/core/testing.withProviders函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。