Thuta Learning
Angular
AdvancedWeb Developmentအလယ်အလတ်

Testing Basics

Angularသင်ခန်းစာ 4325 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Testing Basics ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်
  • နမူနာ UI ကို run၊ state/output ခန့်မှန်းပြီး edge/error cases စမ်းနိုင်ရန်
  • လက်တွေ့ project တွင် typed safe pattern နဲ့ common pitfalls ကိုခွဲသိနိုင်ရန်

ရိုးရိုးလေး ရှင်းပြမယ်

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 ကိုသေချာကိုင်ပါ။

Mental Model
  1. App behavior
  2. စစ်ဆေး + optimize
  3. ယုံကြည်ရတဲ့ UI

Testing Basics ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

အဓိက Syntax / Pattern

typescript
// 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

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 ကိုသေချာကိုင်ပါ။

Code ကို အဆင့်လိုက်ဖတ်မယ်

  1. App behavior// app.component.spec.ts

    Angular သည် Jasmine နှင့် Karma ဖြင့် unit testing များကို support လုပ်သည်။

  2. စစ်ဆေး + optimizeit('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 ကိုသေချာကိုင်ပါ။

  3. ယုံကြည်ရတဲ့ UI// Run tests: ng test

    Form component တစ်ခုအတွက် initial render, invalid errors, successful submit, service failure နဲ့ keyboard behavior tests ရေးပါ။ HTTP dependency ကိုtest provider ဖြင့်ထည့်ပါ။

Execution Flow
  1. App behavior
  2. စစ်ဆေး + optimize
  3. ယုံကြည်ရတဲ့ UI

Success

Testing Basics ရဲ့ core concept နဲ့ Angular data flow ကိုရှင်းပြနိုင်ရန်

Failure

Private method အားလုံးကိုတိတိကျကျ assert လုပ်ခြင်းက refactor တိုင်း test ပျက်ပြီး user behavior မကာကွယ်ပါ။

Angular သည် Jasmine နှင့် Karma ဖြင့် unit testing များကို support လုပ်သည်။

typescript
// 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
You should see
(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 ကိုခန့်မှန်းပါ
typescript
// 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

Testing Basics ရဲ့ အဓိကသဘောတရားက ဘာလဲ။

Quick Quiz 2 / 3

ဘယ်အမှားကို အရင်ဆုံးရှောင်သင့်လဲ။

Quick Quiz 3 / 3

Real project မှာ ဘယ်လိုအသုံးချသင့်လဲ။

ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ

  • 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

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Private method အားလုံးကိုတိတိကျကျ assert လုပ်ခြင်းက refactor တိုင်း test ပျက်ပြီး user behavior မကာကွယ်ပါ။
  • Happy path တစ်ခုအောင်တာနဲ့ feature မှန်ပြီဟုယူဆခြင်းက empty, loading, error နဲ့ cleanup bugs များကိုလွတ်စေနိုင်ပါတယ်။

လက်တွေ့လေ့ကျင့်ခန်း

Testing Basics နမူနာကို copy မလုပ်ဘဲ ကိုယ်တိုင်ပြန်ရေးပါ။ Normal state နှစ်ခု၊ edge case တစ်ခုနှင့် failure state တစ်ခုထည့်ပြီး expected UI နဲ့ state transition ကိုမ run မီရေးထားပါ။

အလုပ်ဖြစ်ကြောင်း ဒီလိုသိမယ်: (Tests များ run လုပ်လျှင် component ၏ behavior ကို verify လုပ်ပြီး pass/fail results ပြသမည်)

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။