This blog represents techniques to setup unit testing environment for doing unit tests for Angular components with external template and CSS files. The blog is applicable for Angular 2/Angular 4/Angular 5 versions.
Using first technique, BeforeEach method is called for two times:
describe('SignupComponent', () => { let fixture: ComponentFixture<SignupComponent>; let component: SignupComponent; beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ SignupComponent ], imports: [ FormsModule ], }).compileComponents(); })); beforeEach(() => { fixture = TestBed.createComponent(SignupComponent); component = fixture.componentInstance; }); it('should create the app', async(() => { expect(component).toBeTruthy(); })); });
In this technique, the synchronous execution of code related with creation of components etc is moved with TestBed.compileComponents.then(…) callback method. Note that the compileComponents method returns a promise so you can perform additional tasks immediately after it finishes.
describe('SignupComponent', () => { let fixture: ComponentFixture<SignupComponent>; let component: SignupComponent; beforeEach(async(() => { TestBed.configureTestingModule({ declarations: [ SignupComponent ], imports: [ FormsModule ], }).compileComponents().then(() => { fixture = TestBed.createComponent(SignupComponent); component = fixture.componentInstance; }); })); it('should create the app', async(() => { expect(component).toBeTruthy(); })); });
In case you are developing web apps using Spring and Angular, check out my book, Building web apps with Spring 5 and Angular. Grab your ebook today and get started.
In recent years, artificial intelligence (AI) has evolved to include more sophisticated and capable agents,…
Adaptive learning helps in tailoring learning experiences to fit the unique needs of each student.…
With the increasing demand for more powerful machine learning (ML) systems that can handle diverse…
Anxiety is a common mental health condition that affects millions of people around the world.…
In machine learning, confounder features or variables can significantly affect the accuracy and validity of…
Last updated: 26 Sept, 2024 Credit card fraud detection is a major concern for credit…