4.4 Kiểm thử
Để kiểm thử tự động ứng dụng,nhóm HL308 đã sử dụng công cụ Karma cùng với Jasmine. Karma là công cụ đơn giản cho phép các đoạn mã JavaScript thực thi trên trình duyệt thực tế. Jasmine giúp cho kiểm thử dựa trên hành vi, Jasmine ``có cú pháp rõ ràng và đơn giản trong kiểm thử.
import { TestBed, async, } from '@angular/core/testing';
import { FTRComponent } from './findresult.component';
import { By } from '@angular/platform-browser';
import { DebugElement } from '@angular/core';
describe('AppComponent', () => {
beforeEach(() => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
TestBed.configureTestingModule({
declarations: [
FTRComponent
],
});
TestBed.compileComponents();
});
it('should open the page "Test online"', async(() => {
const fixture = TestBed.createComponent(FTRComponent);
const app = fixture.debugElement.componentInstance;
expect(app).toBeTruthy();
}));
it(`should have as title "Test online"`, async(() => {
const fixture = TestBed.createComponent(FTRComponent);
const app = fixture.debugElement.componentInstance;
expect(app.title).toEqual('Test on ine');
}));
it('should be a right answer when the question is "mối quan hệ giữa li độ và gia tốc trong dao động điều hòa" with 4 choices A:ngược dấu B:cùng dấu C:không xác định được D: cả 4 đều sai and I choose A', async(() => {
const fixture = TestBed.createComponent(FTRComponent);
const btn = fixture.debugElement.query(By.css('#play'));
const choice = fixture.debugElement.query(By.css('#num0'));
var app = fixture.debugElement.componentInstance;
btn.triggerEventHandler('click',null);
app.result = 'A';
app.question = "mối quan hệ giữa li độ và gia tốc trong dao động điều hòa";
app.results = ['A','B','C','D'];
choice.triggerEventHandler('click',null);
fixture.detectChanges();
expect(app.return).toContain('Đúng');
expect(app.question).toContain('= ?');
expect(app.score).toEqual(1);
expect(app.chances).toEqual(2);
}));
it('should be a right answer when the question is "mối quan hệ giữa li độ và gia tốc trong dao động điều hòa" with 4 choices A:ngược dấu B:cùng dấu C:không xác định được D: cả 4 đều sai and I choose A', async(() => {
const fixture = TestBed.createComponent(FTRComponent);
const btn = fixture.debugElement.query(By.css('#play'));
const choice = fixture.debugElement.query(By.css('#num1'));
var app = fixture.debugElement.componentInstance;
btn.triggerEventHandler('click',null);
app.result = 'A';
app.question = "mối quan hệ giữa li độ và gia tốc trong dao động điều hòa";
app.results = ['A','B','C','D'];
choice.triggerEventHandler('click',null);
fixture.detectChanges();
expect(app.return).toContain('Sai');
expect(app.question).toContain('= ?');
expect(app.score).toEqual(0);
expect(app.chances).toEqual(1);
}));