首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角6单元测试ngOnInit与setTimeOut不工作

角6单元测试ngOnInit与setTimeOut不工作
EN

Stack Overflow用户
提问于 2019-01-23 22:05:51
回答 1查看 6.3K关注 0票数 4

我在一个setTimeOut函数中有一个带有ngOnInit函数的组件。为了编写单元测试用例,我使用tick和fakeAsync来快速转发setTimeOut。但是,它没有被执行,这反过来又没有调用其他函数closeAlert()。

组件代码:

代码语言:javascript
复制
export class BannerComponent implements OnInit {

  @Input()errorData: any;

  @Input()callback: any;

  @Input()autoHide: boolean;

  constructor() { }

  ngOnInit() {

    if (this.autoHide) {
      setTimeout
        (() => {
          this.closeAlert();
        }, 500);
    }
  }

  closeAlert() {
    this.errorData = null;
    if (this.callback) {
      this.callback();
    }
  };
}

规范文件:

代码语言:javascript
复制
describe('BannerComponent', () => {
  let component: BannerComponent;
  let fixture: ComponentFixture<BannerComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ BannerComponent ]
    })
    .compileComponents();
  }));

  beforeEach(async() => {
    fixture = TestBed.createComponent(BannerComponent);
    component = fixture.componentInstance;
    component.ngOnInit();
    fixture.detectChanges();
  });

  it("banner should hide after 500ms", fakeAsync(() => {
    component.errorData = {
      _statusMessage: "New alert banner",
      _statusCode: '200',
    };
    component.callback = null;;
    component.autoHide = true;

    tick(600);
    fixture.detectChanges()

    fixture.whenStable().then(() => {
      let banner = fixture.debugElement.query(By.css('.success'));
      expect(banner).toBe(null)
    })
  }));

});

Html代码:

代码语言:javascript
复制
<div class="success">
    <p>{{errorData._statusMessage}}</p>
</div>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-01-25 02:09:23

我在代码中看到了几个问题。

  • 在在component.ngOnInit()中设置有效数据之前,您将同时调用component.errorDatafixture.detectChanges() (也将调用ngOnInit)。
  • 我不清楚为什么您期望banner与您所显示的html为空。因此,我将测试更改为查看是否调用了component.closeAlert(),如果component.errorData被重置为null,因为这似乎是您真正想要测试的。为了对此进行测试,我在component.closeAlert()上进行了监视。
  • 我设置了滴答,以显示何时调用component.closeAlert(),测试后的tick(499),然后再多一个滴答.

工作StackBlitz

代码:

代码语言:javascript
复制
beforeEach(async(() => { // slight correction of incorrect async wrapper ...
  fixture = TestBed.createComponent(BannerComponent);
  component = fixture.componentInstance;
  // component.ngOnInit(); // <-- don't call this here, the data isn't set up yet ...
  // fixture.detectChanges(); // ditto
}));

it("banner should hide after 500ms", fakeAsync(() => {
  spyOn(component, 'closeAlert').and.callThrough(); // set up a spy so we can test later
  component.errorData = {
    _statusMessage: "New alert banner",
    _statusCode: '200',
  };
  component.callback = null;;
  component.autoHide = true;

  fixture.detectChanges(); // <-- this will execute ngOnInit()
  expect(component.errorData).not.toBeNull(); // <-- after ngOnInit, still NOT null
  expect(component.closeAlert).not.toHaveBeenCalled();
  tick(499); // <-- now let 499ms pass ...
  expect(component.errorData).not.toBeNull(); // <-- after all that "fake" time, still NOT null
  expect(component.closeAlert).not.toHaveBeenCalled();
  tick(1); // <-- now tick for just 1 more millisecond ...
  expect(component.errorData).toBeNull(); // <-- now this has become NULL
  expect(component.closeAlert).toHaveBeenCalled(); // <-- and the method was called
  // fixture.whenStable().then(() => {
  //   let banner = fixture.debugElement.query(By.css('.success'));
  //   expect(banner).toBe(null)
  // });
}));

我希望这能帮到你!

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54336414

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档