Created
May 28, 2020 01:27
-
-
Save Qarun-Qadir-Bissoondial/f5b73e757aad5ae1d298fc5bcef8d4cd to your computer and use it in GitHub Desktop.
Component test with spies and mocked service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { async, ComponentFixture, TestBed } from '@angular/core/testing'; | |
import { InventoryComponent } from './inventory.component'; | |
import { InventoryService } from '../inventory.service'; | |
import { MockInventoryService } from 'src/testing/inventory.mock.service'; | |
fdescribe('InventoryComponent', () => { | |
let component: InventoryComponent; | |
let fixture: ComponentFixture<InventoryComponent>; | |
let incrementSpy: jasmine.Spy; | |
let decrementSpy: jasmine.Spy; | |
let service: InventoryService; | |
beforeEach(async(() => { | |
TestBed.configureTestingModule({ | |
declarations: [ InventoryComponent ], | |
providers: [ | |
{ provides: InventoryService, useClass: MockInventoryService } | |
] | |
}) | |
.compileComponents(); | |
})); | |
beforeEach(() => { | |
fixture = TestBed.createComponent(InventoryComponent); | |
service = TestBed.inject(InventoryService); | |
incrementSpy = spyOn(service, 'incrementCount'); | |
decrementSpy = spyOn(service, 'decrementCount'); | |
component = fixture.componentInstance; | |
fixture.detectChanges(); | |
}); | |
it('should create', () => { | |
expect(component).toBeTruthy(); | |
}); | |
it('should call "incrementCount()" when "increment()" is called', () => { | |
component.increment(); | |
expect(incrementSpy).toHaveBeenCalled(); | |
}); | |
it('should call "decrementCount()" when "decrement()" is called', () => { | |
component.decrement(); | |
expect(decrementSpy).toHaveBeenCalled(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment