Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/27.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
Angular 角度2+中的单元测试;(点击按钮时调用测试方法)_Angular - Fatal编程技术网

Angular 角度2+中的单元测试;(点击按钮时调用测试方法)

Angular 角度2+中的单元测试;(点击按钮时调用测试方法),angular,Angular,我试图测试方法调用,当点击某个按钮时,似乎什么都不起作用 这是我的HTML文件中的行: <button class="btn-grey" (click)="methodTest()">ODUSTANI</button> 您将需要使用Angular TestBed设置测试并注入服务 import { TestBed, async, ComponentFixture } from '@angular/core/testing'; describe('', () =>

我试图测试方法调用,当点击某个按钮时,似乎什么都不起作用

这是我的HTML文件中的行:

<button class="btn-grey" (click)="methodTest()">ODUSTANI</button>

您将需要使用Angular TestBed设置测试并注入服务

import { TestBed, async, ComponentFixture } from '@angular/core/testing';

describe('', () => {
  let fixture: ComponentFixture<TestComponent>;
  let component: TestComponent;

  beforeEach(async(() => {

    TestBed.configureTestingModule({
       imports: [
         AppModule
       ],
       declarations: [ 
       ],
       providers: [
       ]}).compileComponents()
      .then(() => {
         fixture = TestBed.createComponent(TestComponent);
         comp = fixture.componentInstance;
      });
   }));
});

请给出完整的代码我编辑了原始问题。这是我可以提供的信息。控制台上是否有任何错误消息?可能您的方法中遗漏了一个
}
。问题不在于该方法,它可以正常工作。我正在尝试在单击按钮时为该方法调用编写单元测试。这是在验证按钮是否已单击。
import { TestBed, async, ComponentFixture } from '@angular/core/testing';

describe('', () => {
  let fixture: ComponentFixture<TestComponent>;
  let component: TestComponent;

  beforeEach(async(() => {

    TestBed.configureTestingModule({
       imports: [
         AppModule
       ],
       declarations: [ 
       ],
       providers: [
       ]}).compileComponents()
      .then(() => {
         fixture = TestBed.createComponent(TestComponent);
         comp = fixture.componentInstance;
      });
   }));
});
it('should', async(() => {
   spyOn(component, 'onButtonClick');

   let button = fixture.debugElement.nativeElement.querySelector('button');
   button.click();

   fixture.whenStable().then(() => {
   expect(component.onButtonClick).toHaveBeenCalled();
  })
}));