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