The TestBed Class
Learn what TestBed class is, how we can inject services into the TestBed class, and what benefits it brings to our application.
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.