在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
领取专属 10元无门槛券
手把手带您无忧上云