0
我已經在angular2中編寫了一個compo組件,併爲它編寫了單元測試代碼。由於我是初學者,所以我無法測試創建的組件。所以請幫助我解決這個問題。爲它組合組件的單元測試
import { Component, Input, Output, EventEmitter } from "@angular/core";
import { ComboInterface } from './ComboInterface';
@Component({
moduleId: module.id,
selector: 'combo-compo',
template: `
<select name="theme" class="form-control" [ngModel]="selectedObject" (ngModelChange)="onChangeObj($event)">
<option [ngValue]="theme" *ngFor="let theme of dataObject" >{{theme.value}}</option>
</select>
`
})
export class ComboComponent {
selectedObject: ComboInterface;
@Input() dataObject: Array<ComboInterface>;
@Output() onComboChange = new EventEmitter();
onChangeObj(newObj: ComboInterface) {
this.selectedObject = newObj;
this.onComboChange.emit(this.selectedObject);
}
}
我已經寫單元測試代碼下面給出
describe('ComboComponent Unit Test',() => {
let comp: ComboComponent;
let fixture: ComponentFixture<ComboComponent>;
let de: DebugElement;
let el: HTMLElement;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [ComboComponent], // declare the test component
imports: [ReactiveFormsModule, FormsModule]
});
fixture = TestBed.createComponent(ComboComponent);
comp = fixture.componentInstance;
});
it('should create ComboComponent',() => expect(comp).toBeDefined());
it('should create a ComboComponent with values',() => {
comp.dataObject = [
{
'value': 'Victoria Cantrell test',
'key': '0839',
}, {
'value': 'Pearl Crosby Test',
'key': '8262'
}, {
'value': 'Colette Foley Test',
'key': '8968'
}
];
comp.onChangeObj(comp.dataObject[0]);
});
});
該代碼是通過運行npm test
運行良好,但我如何測試組件創建??
但是數據沒有顯示在組合組件中? – Keshav
顯示的數據是端到端測試。在這裏,你是單元測試。你必須測試你的函數是否工作。在端到端測試中,您將測試它是否正確顯示 – trichetriche
請問您是否會發布e2e測試代碼? – Keshav