The TestBed Class

What is the TestBed class?

The Angular TestBed class helps us by providing an environment for testing our application. It also provides an API for making our components and services available to the Unit Tests.

In AngularJS, we didn’t have the TestBed class, which made it far more complex to access the Controllers and services in the Unit Tests. Thankfully, with the TestBed class, it is far easier to access our components and services in order to test them.

Going back to our example spec file, let’s see how the TestBed class is being used.

import { ComponentFixture, TestBed } from '@angular/core/testing';

import { MyCompComponent } from './my-comp.component';

describe('MyCompComponent', () => {
  let component: MyCompComponent;
  let fixture: ComponentFixture<MyCompComponent>;

  beforeEach(async () => {
    await TestBed.configureTestingModule({
      declarations: [ MyCompComponent ]
    })
    .compileComponents();
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(MyCompComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

Get hands-on with 1400+ tech skills courses.