Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 角度2:测试中未更新模板_Unit Testing_Angular_Angular2 Testing - Fatal编程技术网

Unit testing 角度2:测试中未更新模板

Unit testing 角度2:测试中未更新模板,unit-testing,angular,angular2-testing,Unit Testing,Angular,Angular2 Testing,我只是想让这一切顺利 it('should have expected <h1> text', async(() => { let fixture = TestBed.createComponent(AppComponent); fixture.detectChanges(); const sectionEl = fixture.debugElement.query(By.css("section")); spyOn(fixture.debugElement

我只是想让这一切顺利

it('should have expected <h1> text', async(() => {
  let fixture = TestBed.createComponent(AppComponent);
  fixture.detectChanges();

  const sectionEl = fixture.debugElement.query(By.css("section"));

  spyOn(fixture.debugElement.componentInstance, "runMe");

  sectionEl.nativeElement.click();
  expect(fixture.debugElement.componentInstance.runMe).toHaveBeenCalled();

  expect(sectionEl.nativeElement.textContent).toBe("changed!");
it('should have expected text',async(()=>{
让fixture=TestBed.createComponent(AppComponent);
fixture.detectChanges();
const sectionEl=fixture.debugElement.query(By.css(“section”);
spyOn(fixture.debugElement.componentInstance,“runMe”);
sectionEl.nativeElement.click();
expect(fixture.debugElement.componentInstance.runMe).toHaveBeenCalled();
期望(sectionEl.nativeElement.textContent).toBe(“已更改!”);

因此,
runMe
函数没有更改该部分的文本,但是调用了spy shows
runMe

如果没有更完整的示例,很难判断。但是单击stuff,它通常涉及异步事件处理。因此,我们需要等待事件处理完成。在测试中,我们如何才能做到这一点通过调用fixture.whenStable,返回一个承诺。我们可以在承诺解决后继续测试

sectionEl.nativeElement.click();
fixture.whenStable().then(() => {
  expect(fixture.debugElement.componentInstance.runMe).toHaveBeenCalled();

  fixture.detectChanges();
  expect(sectionEl.nativeElement.textContent).toBe("changed!");
});

如果看不到更完整的示例,很难说。但是单击stuff通常会涉及异步事件处理。因此,我们需要等待事件处理完成。在测试中,我们可以通过调用
fixture.whenStable
,它返回一个承诺。当承诺解决时,我们可以继续测试

sectionEl.nativeElement.click();
fixture.whenStable().then(() => {
  expect(fixture.debugElement.componentInstance.runMe).toHaveBeenCalled();

  fixture.detectChanges();
  expect(sectionEl.nativeElement.textContent).toBe("changed!");
});

你能编辑你的帖子,为你的
runMe
函数包含代码吗?你能编辑你的帖子,为你的
runMe
函数包含代码吗?我之前在我自己的项目中尝试过这个,但不起作用。我在angular 2 quickstart github repo的本地克隆副本中也尝试过类似的方法,但不起作用。你应该我发布了一个完整的例子来进一步帮助Peeskillet,我正在尝试为此设置一个Plunkr。你能帮助我在src/test.spec.ts中使测试床工作吗。这是Plunkr-我之前在我自己的项目中尝试过这个,但它不起作用。我还在angular 2 quickstart github repo的克隆本地副本中也尝试过类似的东西,但它不起作用。您可能应该发布一个完整的示例以获得进一步的帮助Peeskillet,我正在尝试为此设置一个Plunkr。您可以帮助我在src/test.spec.ts中使测试台工作吗。这是Plunkr-