पाठ 30 / 32

कंपोनेंट टेस्ट करना

TestBed, change detection और component state पर assertions के साथ components को अलग से test करें।

लेखक: · 15 साल से वेब डेवलपर · अपडेट: 1 अक्टू॰ 2026

TestBed क्या करता है

TestBed, टेस्ट के लिए एक छोटा Angular मॉड्यूल बनाता है, जिससे आप असली या नकली निर्भरताओं के साथ कंपोनेंट बना सकते हैं और उसका रेंडर हुआ आउटपुट जाँच सकते हैं।

एक न्यूनतम टेस्ट

यह टेस्ट कंपोनेंट बनाता है, चेंज डिटेक्शन ट्रिगर करता है, और इंस्टेंस पर एक मान जाँचता है।

describe('CounterComponent', () => {
  it('increments the count', () => {
    const fixture = TestBed.createComponent(CounterComponent);
    const component = fixture.componentInstance;

    component.increment();
    fixture.detectChanges();

    expect(component.count()).toBe(1);
  });
});