ရိုးရိုးလေး ရှင်းပြမယ်
Testing Basics ကို ရိုးရိုးလေးနားလည်မယ်
Angular သည် Jasmine နှင့် Karma ဖြင့် unit testing များကို support လုပ်သည်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Private method အားလုံးကိုတိတိကျကျ assert လုပ်ခြင်းက refactor တိုင်း test ပျက်ပြီး user behavior မကာကွယ်ပါ။
ဖြစ်လေ့ရှိတဲ့အမှား
Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Angular testing မှာ pure class/service unit tests, component behavior tests, HTTP/router integration tests နဲ့ user-flow E2E tests ကိုrisk အလိုက်ပေါင်းသုံးပါတယ်။ Test က implementation details ထက် user-observable behavior, accessible queries နဲ့ dependency contracts ကိုစစ်သင့်ပါတယ်။ Async work ကိုdeterministic control လုပ်ပြီး fixture/change detection timing, cleanup နဲ့ regression cases ကိုသေချာကိုင်ပါ။
- App behavior
- စစ်ဆေး + optimize
- ယုံကြည်ရတဲ့ UI
Testing Basics ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
// app.component.spec.ts
import { TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [AppComponent]
}).compileComponents();
});
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app).toBeTruthy();
});
it('should have title "my-app"', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app.title).toEqual('my-app');
});
it('should render title', () => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.nativeElement;
expect(compiled.querySelector('h1').textContent)
.toContain('my-app');
});
});
// Run tests: ng testAngular testing မှာ pure class/service unit tests, component behavior tests, HTTP/router integration tests နဲ့ user-flow E2E tests ကိုrisk အလိုက်ပေါင်းသုံးပါတယ်။ Test က implementation details ထက် user-observable behavior, accessible queries နဲ့ dependency contracts ကိုစစ်သင့်ပါတယ်။ Async work ကိုdeterministic control လုပ်ပြီး fixture/change detection timing, cleanup နဲ့ regression cases ကိုသေချာကိုင်ပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- App behavior
// app.component.spec.tsAngular သည် Jasmine နှင့် Karma ဖြင့် unit testing များကို support လုပ်သည်။
- စစ်ဆေး + optimize
it('should have title "my-app"', () => {Angular testing မှာ pure class/service unit tests, component behavior tests, HTTP/router integration tests နဲ့ user-flow E2E tests ကိုrisk အလိုက်ပေါင်းသုံးပါတယ်။ Test က implementation details ထက် user-observable behavior, accessible queries နဲ့ dependency contracts ကိုစစ်သင့်ပါတယ်။ Async work ကိုdeterministic control လုပ်ပြီး fixture/change detection timing, cleanup နဲ့ regression cases ကိုသေချာကိုင်ပါ။
- ယုံကြည်ရတဲ့ UI
// Run tests: ng testForm component တစ်ခုအတွက် initial render, invalid errors, successful submit, service failure နဲ့ keyboard behavior tests ရေးပါ။ HTTP dependency ကိုtest provider ဖြင့်ထည့်ပါ။
- App behavior
- စစ်ဆေး + optimize
- ယုံကြည်ရတဲ့ UI
Success
Testing Basics ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
Failure
Private method အားလုံးကိုတိတိကျကျ assert လုပ်ခြင်းက refactor တိုင်း test ပျက်ပြီး user behavior မကာကွယ်ပါ။
Angular သည် Jasmine နှင့် Karma ဖြင့် unit testing များကို support လုပ်သည်။
// app.component.spec.ts
import { TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [AppComponent]
}).compileComponents();
});
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app).toBeTruthy();
});
it('should have title "my-app"', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app.title).toEqual('my-app');
});
it('should render title', () => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.nativeElement;
expect(compiled.querySelector('h1').textContent)
.toContain('my-app');
});
});
// Run tests: ng test(Tests များ run လုပ်လျှင် component ၏ behavior ကို verify လုပ်ပြီး pass/fail results ပြသမည်)Real-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Form component တစ်ခုအတွက် initial render, invalid errors, successful submit, service failure နဲ့ keyboard behavior tests ရေးပါ။ HTTP dependency ကိုtest provider ဖြင့်ထည့်ပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Testing Basics နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
// app.component.spec.ts
import { TestBed } from '@angular/core/testing';
import { AppComponent } from './app.component';
describe('AppComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [AppComponent]
}).compileComponents();
});
it('should create the app', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app).toBeTruthy();
});
it('should have title "my-app"', () => {
const fixture = TestBed.createComponent(AppComponent);
const app = fixture.componentInstance;
expect(app.title).toEqual('my-app');
});
it('should render title', () => {
const fixture = TestBed.createComponent(AppComponent);
fixture.detectChanges();
const compiled = fixture.nativeElement;
expect(compiled.querySelector('h1').textContent)
.toContain('my-app');
});
});
// Run tests: ng testရလဒ်: Testing Basics ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Form component တစ်ခုအတွက် initial render, invalid errors, successful submit, service failure နဲ့ keyboard behavior tests ရေးပါ။ HTTP dependency ကိုtest provider ဖြင့်ထည့်ပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- App behavior → စစ်ဆေး + optimize → ယုံကြည်ရတဲ့ UI
- Angular testing မှာ pure class/service unit tests, component behavior tests, HTTP/router integration tests နဲ့ user-flow E2E tests ကိုrisk အလိုက်ပေါင်းသုံးပါတယ်။ Test က implementation details ထက် user-observable behavior, accessible queries နဲ့ dependency contracts ကိုစစ်သင့်ပါတယ်။ Async work ကိုdeterministic control လုပ်ပြီး fixture/change detection timing, cleanup နဲ့ regression cases ကိုသေချာကိုင်ပါ။
- Private method အားလုံးကိုတိတိကျကျ assert လုပ်ခြင်းက refactor တိုင်း test ပျက်ပြီး user behavior မကာကွယ်ပါ။
- Form component တစ်ခုအတွက် initial render, invalid errors, successful submit, service failure နဲ့ keyboard behavior tests ရေးပါ။ HTTP dependency ကိုtest provider ဖြင့်ထည့်ပါ။
Angular — Testing — Angular