I'm learning Unit testing and I've
@Input() showSearchTab: Boolean = false;
@Input() currentTab: string = null;
ngOnInit(): void {
if (this.showSearchTab) {
this.currentTab = 'search';
}
}
This is the unit test:
describe('ngOnInit()', () => {
it('should set all the defaults', () => {
specManager.component.ngOnInit();
expect(specManager.component.currentTab).toBe('search');
});
});
I get an error expected null to be 'search'
As per your logic in ngOnInit
function currentTab
will have value search
if showSearchTab
is true
.
Thus, before invoking specManager.component.ngOnInit()
in your test case, set specManager.component.currentTab = true;
Following your component test, at the moment the test hits the expectation, showSearchTab is set to false, so it doesn't pass the if condition. To have a fully tested scenario, you should do:
describe('ngOnInit()', () => {
describe('and showSearchTab is set to false', () => {
it('should set all the defaults', () => {
specManager.component.showSearchTab = false;
specManager.component.ngOnInit();
expect(specManager.component.currentTab).toBeNull();
});
});
describe('and showSearchTab is set to true', () => {
it('should set all the defaults', () => {
specManager.component.showSearchTab = true;
specManager.component.ngOnInit();
expect(specManager.component.currentTab).toEqual('search');
});
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.