在Angular2中对复选框进行单元测试的方法如下:
import { Component } from '@angular/core';
@Component({
selector: 'app-checkbox',
template: `
<input type="checkbox" [(ngModel)]="isChecked">
`
})
export class CheckboxComponent {
isChecked: boolean = false;
}
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { CheckboxComponent } from './checkbox.component';
describe('CheckboxComponent', () => {
let component: CheckboxComponent;
let fixture: ComponentFixture<CheckboxComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [CheckboxComponent]
})
.compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(CheckboxComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should toggle isChecked value when checkbox is clicked', () => {
const checkbox = fixture.nativeElement.querySelector('input');
checkbox.click();
expect(component.isChecked).toBe(true);
checkbox.click();
expect(component.isChecked).toBe(false);
});
});
ng test
云原生正发声
GAME-TECH
云+社区技术沙龙[第28期]
云+社区技术沙龙[第9期]
腾讯云GAME-TECH沙龙
云+社区技术沙龙[第8期]
腾讯云GAME-TECH沙龙
云+社区技术沙龙[第6期]
领取专属 10元无门槛券
手把手带您无忧上云