17.10.2018 Views

Angular

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

selector: 'my-app',<br />

template: '{{title}}'<br />

})<br />

export class MyAppComponent{<br />

title = 'welcome';<br />

}<br />

For angular testing, angular provide its testing utilities along with the testing framework which helps in writing the<br />

good test case in angular. <strong>Angular</strong> utilities can be imported from @angular/core/testing<br />

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

import { MyAppComponent } from './banner-inline.component';<br />

describe('Tests for MyAppComponent', () => {<br />

let fixture: ComponentFixture;<br />

let comp: MyAppComponent;<br />

beforeEach(() => {<br />

TestBed.configureTestingModule({<br />

declarations: [<br />

MyAppComponent<br />

]<br />

});<br />

});<br />

beforeEach(() => {<br />

fixture = TestBed.createComponent(MyAppComponent);<br />

comp = fixture.componentInstance;<br />

});<br />

it('should create the MyAppComponent', () => {<br />

expect(comp).toBeTruthy();<br />

});<br />

});<br />

In the above example, there is only one test case which explain the test case for component existence. In the above<br />

example angular testing utilities like TestBed and ComponentFixture are used.<br />

TestBed is used to create the angular testing module and we configure this module with the<br />

configureTestingModule method to produce the module environment for the class we want to test. Testing<br />

module to be configured before the execution of every test case that's why we configure the testing module in the<br />

beforeEach function.<br />

createComponent method of TestBed is used to create the instance of the component under test. createComponent<br />

return the ComponentFixture. The fixture provides access to the component instance itself.<br />

GoalKicker.com – <strong>Angular</strong> 2 Notes for Professionals 218

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!