Javascript Angular2,测试和解析数据:如何测试ngOnINit?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42656045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:19:52  来源:igfitidea点击:

Angular2, testing and resolved data: how to test ngOnINit?

javascriptangularjasminekarma-runnerresolver

提问by Jerome P Mrozak

I'm working through the Angular2 testing guideand wish to write a test for the ngOnInit() function. The one from the Routing part of the programming guide has this format:

我正在阅读Angular2 测试指南,并希望为 ngOnInit() 函数编写一个测试。编程指南中路由部分的格式如下:

let org: Org = null;

ngOnInit(): void {
  let that = this;

  this.route.data
    .subscribe((data: { org: Org }) => {
      that.org = data.org;
    });
}

This is fulfilled through a resolver, like:

这是通过解析器实现的,例如:

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<Org> {
  let id = this.authService.user.orgId;

  return this.orgService
    .getOrg(id)
    .map(
      (org: Org) : Org => {

        if(org) {
          return org;
        } else { 
          // If  the Org isn't available then the edit page isn't appropriate.
          this.router.navigate(['/provider/home']);
          return null;
        }
     })
    .first();
}

The code works OK, but I'm not sure how to write a test for ngOnInit. The examples available to me assume that an embedded OrgService can be replaced by a MockOrgService. However, all I have is a resolver.

代码工作正常,但我不确定如何为 ngOnInit 编写测试。我可以使用的示例假设嵌入式 OrgService 可以被 MockOrgService 替换。但是,我只有一个解析器。

I'll eventually figure out how to test the resolver on its own. Are there any useful tests I can do with a resolver-based ngOnInit?

我最终会弄清楚如何自己测试解析器。我可以使用基于解析器的 ngOnInit 进行任何有用的测试吗?

Thanks,

谢谢,

Jerome.

杰罗姆。

回答by Paul Samsotha

What is the behavior or the ngOnInitmethod? All it does is assign the value of the orgwhen the route data is resolved. So that's all you really need to test.

行为或ngOnInit方法是什么?它所做的只是在org解析路由数据时分配 的值。所以这就是你真正需要测试的。

let routeStub;

beforeEach(() => {
  routeStub = {
    data: null
  }

  TestBed.configureTestingModule({
    providers: [
      { provide: ActivatedRoute, useValue: routeStub }  
    ]
  })
})

it('should assign org when route is resolved', async(() => {
  let org = new Org()
  routeStub.data = Observable.of(org)

  fixture.detectChanges();
  fixture.whenStable().then(() => {
    expect(component.org).toEqual(org)
  })
}))

回答by Ian Yoder

I was trying to test ngOnInit() for a component, and unfortunately the accepted answer didn't work for me. However, this did:

我试图为一个组件测试 ngOnInit(),不幸的是,接受的答案对我不起作用。然而,这确实:

describe('your test', () => {
  beforeEach(async() => {
    // set up your component as necessary

    component.ngOnInit();

    await fixture.whenStable();
  });

  it('should verify your test', () => {
    // run your expectation
  });
});