What is TestBed in Jasmine
Asked Answered
H

2

5

I am new to Jasmine with Angular 2, I am frequently working with the TestBed object when writting a Testcase and getting the error:Please call "TestBed.compileComponents" before your test.

How do I solve this error?

@Component({
  moduleId:module.id,
    selector: 'my-app',
    templateUrl: 'app-component.html',

})
Harrietteharrigan answered 6/10, 2016 at 6:10 Comment(3)
Did you try calling TestBed.compileComponents()? TestBed is a mock environment to run Angular2 component tests without the browser.Compulsory
Yes, but now it says Error: Cannot create the component AppComponent as it was not imported into the testing module!Harrietteharrigan
Did you check angular.io/docs/ts/latest/guide/testing.htmlCompulsory
R
14

Please call "TestBed.compileComponents" before your test

This call is required when testing components using templateUrl

Error: Cannot create the component AppComponent as it was not imported into the testing module!

You need to configure the TestBed before each test, adding any components, modules and services you need for the test. It's just like configuring an regular @NgModule from scratch, but you just add what you need.

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

beforeEach(async(() => {
  TestBed.configureTestingModule({
    declarations: [ AppComponent ],
    providers: [],
    imports: []
  })
  .compileComponents();
}));

it('...', () => {
  let fixture = TestBed.createComponent(AppComponent);
});

See Also

Reform answered 6/10, 2016 at 8:32 Comment(2)
Why do we use TestBed?Harrietteharrigan
To configure a module for the testing environment.Reform
F
0

Angular TestBed provides testing environment. We have to provide the components, serives, modules required before each test.

Friary answered 14/9, 2024 at 6:36 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.