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

TypeScript testing.beforeEachProviders函数代码示例

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

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



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

示例1: describe

describe('Home', () => {
  // provide our implementations or mocks to the dependency injector
  beforeEachProviders(() => [
    BaseRequestOptions,
    MockBackend,
    provide(Http, {
      useFactory: function(backend, defaultOptions) {
        return new Http(backend, defaultOptions);
      },
      deps: [MockBackend, BaseRequestOptions]
    }),

    AppState,
    Title,
    Home
  ]);

  it('should have default data', inject([ Home ], (home) => {
    expect(home.localState).toEqual({ value: '' });
  }));

  it('should have a title', inject([ Home ], (home) => {
    expect(!!home.title).toEqual(true);
  }));

  it('should log ngOnInit', inject([ Home ], (home) => {
    spyOn(console, 'log');
    expect(console.log).not.toHaveBeenCalled();

    home.ngOnInit();
    expect(console.log).toHaveBeenCalled();
  }));

});
开发者ID:jneveux,项目名称:calculator,代码行数:34,代码来源:home.spec.ts


示例2: describe

describe('Dashboard Component', () => {
  setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS, TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
  let tcb: TestComponentBuilder;

  beforeEachProviders(() => [
    TestComponentBuilder,
    HTTP_PROVIDERS,
    ROUTER_FAKE_PROVIDERS,
    provide(Location, {useClass: SpyLocation}),
    provide(DirectiveResolver, {useClass: MockDirectiveResolver}),
    provide(ViewResolver, {useClass: MockViewResolver}),
    HeroService,
    AppComponent,
  ]);

  beforeEach(inject([TestComponentBuilder], (tcb_) => {
    tcb = tcb_;
  }));

  it('should render the title', (done) => {
    return tcb.createAsync(DashboardComponent)
      .then(fixture => {
        let component = fixture.componentInstance;
        component.title = 'Top_heroes';
        fixture.detectChanges();
        let element = fixture.nativeElement;
        expect(element.querySelector('h3').innerText).toContain('Top_heroes');
        done();
      });
  });
});
开发者ID:mahpah,项目名称:ng2-pack,代码行数:31,代码来源:dashboard.component.spec.ts


示例3: describe

describe('Login Component', () => {
  setBaseTestProviders(TEST_BROWSER_DYNAMIC_PLATFORM_PROVIDERS, TEST_BROWSER_DYNAMIC_APPLICATION_PROVIDERS);
  let tcb: TestComponentBuilder;

  beforeEachProviders(() => [
    TestComponentBuilder,
    provide(DirectiveResolver, {useClass: MockDirectiveResolver}),
    provide(ViewResolver, {useClass: MockViewResolver}),
    LoginComponent,
  ]);

  beforeEach(inject([TestComponentBuilder], (tcb_) => {
    tcb = tcb_;
  }));

  it('should render form', (done) => {
    tcb.createAsync(LoginComponent)
      .then((fixture) => {
        let element = fixture.nativeElement;
        fixture.detectChanges();
        let form = element.querySelector('form');
        expect(form).toBeTruthy();
        expect(form.querySelector('input[name="username"]')).toBeTruthy();
        expect(form.querySelector('input[name="password"]')).toBeTruthy();
        done();
      });
  });
});
开发者ID:mahpah,项目名称:ng2-pack,代码行数:28,代码来源:login.component.spec.ts


示例4: describe

describe('API Service', () => {

  beforeEachProviders(() => [API]);


  it('should ...', inject([API], (service: API) => {

  }));

});
开发者ID:manekinekko,项目名称:gif-finder,代码行数:10,代码来源:api.spec.ts


示例5: describe

  describe('OrderService', () => {

    beforeEachProviders(() => [{ provide: OrderService, useFactory: () => new OrderService(false) },
      DateFormatter,
      DatePipe
    ]);

    it('should fetch our data', inject([OrderService], (orderService: OrderService) => {
      expect(orderService.getData().length).toBe(2);
    }));

    it('should fetch our data', inject([DateFormatter], (dateFormatService: DateFormatter) => {
      expect(dateFormatService.x).toBe(3);
    }));
  });
开发者ID:AnimalStyle55,项目名称:Angular2,代码行数:15,代码来源:test2.ts


示例6: describe

describe('About', () => {
  // provide our implementations or mocks to the dependency injector
  beforeEachProviders(() => [
    About
  ]);

  it('should log ngOnInit', inject([ About ], (about) => {
    spyOn(console, 'log');
    expect(console.log).not.toHaveBeenCalled();

    about.ngOnInit();
    expect(console.log).toHaveBeenCalled();
  }));

});
开发者ID:karnex47,项目名称:iaa-new,代码行数:15,代码来源:about.spec.ts


示例7: describe

describe('Router', () => {
  let location: Location;
  let router: Router;

  beforeEachProviders(() => [
    RouteRegistry,
    provide(Location, {useClass: SpyLocation}),
    provide(Router, {useClass: RootRouter}),
    provide(ROUTER_PRIMARY_COMPONENT, {useValue: MyApp})
  ]);

  beforeEach(inject([Router, Location], (_router, _location) => {
    location = _location;
    router = _router;
  }));

  it('should be able to navigate to Home', done => {
    router.navigate(['/Home']).then(() => {
      expect(location.path()).toBe('');
      done();
    }).catch(e => done.fail(e));
  });

  it('should be able to navigate to About by route name', done => {
    router.navigate(['/About']).then(() => {
      expect(location.path()).toBe('/about');
      done();
    }).catch(e => done.fail(e));
  });

  it('should be able to navigate to Weather by URL', done => {
    router.navigateByUrl('/about').then(() => {
      expect(location.path()).toBe('/about');
      done();
    }).catch(e => done.fail(e));
  });
});
开发者ID:Singulus2,项目名称:medi,代码行数:37,代码来源:app.spec.ts


示例8: 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:GeertJohan,项目名称:angular.io,代码行数:91,代码来源:http-hero.service.spec.ts


示例9: describe

  describe('using TCB', () => {
    let comp: DashboardComponent;
    let mockHeroService: MockHeroService;

    beforeEachProviders(() => {
      mockHeroService = new MockHeroService();
      return [
        provide(Router,      {useClass: MockRouter}),
        provide(MockRouter,  {useExisting: Router}),
        provide(HeroService, {useValue: mockHeroService})
      ];
    });

    it('can instantiate it',
      async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
      tcb.createAsync(DashboardComponent);
    })));

    it('should NOT have heroes before OnInit',
      async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
      tcb.createAsync(DashboardComponent).then(fixture => {
        comp = fixture.debugElement.componentInstance;

        expect(comp.heroes.length).toEqual(0,
          'should not have heroes before OnInit');
      });
    })));

    it('should NOT have heroes immediately after OnInit',
      async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
      tcb.createAsync(DashboardComponent).then(fixture => {
        comp = fixture.debugElement.componentInstance;
        fixture.detectChanges(); // runs initial lifecycle hooks

        expect(comp.heroes.length).toEqual(0,
          'should not have heroes until service promise resolves');
      });
    })));

    it('should HAVE heroes after HeroService gets them',
      async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {

      tcb.createAsync(DashboardComponent).then(fixture => {
        comp = fixture.debugElement.componentInstance;
        fixture.detectChanges();           // runs ngOnInit -> getHeroes

        mockHeroService.lastPromise // the one from getHeroes
          .then(() => {
            expect(comp.heroes.length).toBeGreaterThan(0,
              'should have heroes after service promise resolves');
          });

      });
    })));

    it('should DISPLAY heroes after HeroService gets them',
      async(inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {

      tcb.createAsync(DashboardComponent).then(fixture => {
        comp = fixture.debugElement.componentInstance;
        fixture.detectChanges();           // runs ngOnInit -> getHeroes

        mockHeroService.lastPromise // the one from getHeroes
          .then(() => {

            // Find and examine the displayed heroes
            fixture.detectChanges();      // update bindings
            let heroNames = fixture.debugElement.queryAll(By.css('h4'));

            expect(heroNames.length).toEqual(4, 'should display 4 heroes');

            // the 4th displayed hero should be the 5th mock hero
            expect(heroNames[3].nativeElement)
              .toHaveText(mockHeroService.mockHeroes[4].name);
          });

      });
    })));

    it('should tell ROUTER to navigate by hero id',
      async(inject([TestComponentBuilder, Router],
      (tcb: TestComponentBuilder, router: MockRouter) => {

      let spy = spyOn(router, 'navigate').and.callThrough();

      tcb.createAsync(DashboardComponent).then(fixture => {
        let hero: Hero = {id: 42, name: 'Abbracadabra' };
        comp = fixture.debugElement.componentInstance;
        comp.gotoDetail(hero);

        let linkParams = spy.calls.mostRecent().args[0];
        expect(linkParams[0]).toEqual('HeroDetail', 'should nav to "HeroDetail"');
        expect(linkParams[1].id).toEqual(hero.id, 'should nav to fake hero\'s id');

      });
    })));
  });
开发者ID:GeertJohan,项目名称:angular.io,代码行数:97,代码来源:dashboard.component.spec.ts



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

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

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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