Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is TestBed in Jasmine

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',

})
like image 584
fruitjs Avatar asked Dec 11 '22 14:12

fruitjs


1 Answers

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

  • Angular testing docs for many more complete examples.
like image 97
Paul Samsotha Avatar answered Dec 13 '22 22:12

Paul Samsotha