Angular Unit Test, Jasmine, Karma, example of unit test

yotube
0

Jasmine is the framework we are going to use to create our tests. It has a bunch of functionalities to allow us the write different kinds of tests.

 Jasmine 


karma is a task runner for our tests. It uses a configuration file in order to set the startup file, the reporters, the testing framework, the browser among other things.

 Karma


To run the test you only need to run the command “ng test”.

Karma Config

Karma.config.js

Test entry file

The angular-cli configuration of karma uses the file “test.ts” as the entry point of the tests for the application.





Unit test component


describe('MyPopoverComponent', () => {
let component: MyPopoverComponent;
let fixture: ComponentFixture<MyPopoverComponent>;
let popoverController;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [MyPopoverComponent],
imports: [
IonicModule.forRoot(),
RouterTestingModule,
TranslateModule.forRoot(),
],
providers: [
{
provide: PopoverController,
useClass: class {
dismiss = jasmine.createSpy('dismiss');
},
},
],
}).compileComponents();

fixture = TestBed.createComponent(MyPopoverComponent);
component = fixture.componentInstance;
fixture.detectChanges();
popoverController = TestBed.get(PopoverController);
}));

it('should create', () => {
expect(component).toBeTruthy();
});

it('should create close', () => {
component.close();
expect(component.close).toBeDefined();
});
});

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top