Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Qarun-Qadir-Bissoondial/f5b73e757aad5ae1d298fc5bcef8d4cd to your computer and use it in GitHub Desktop.
Save Qarun-Qadir-Bissoondial/f5b73e757aad5ae1d298fc5bcef8d4cd to your computer and use it in GitHub Desktop.
Component test with spies and mocked service
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