I'm new to angular testing and I want to create tests for my service.
The problem is my service (foo) is inject another service (bar) in its constructor.
So I should create a mock to bar? mock every function inside bar? if so, I need to do that for every package I import? write my own functions as mock?
You can create either a mock/stub or make use of jasmine spies.
export const jobServiceStub: Partial<JobService> = {
getUser() {
return {};
}
}
describe('SampleService', () => {
let service: SampleService;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
//replacing service with stub
{ provide: JobService, useValue: jobServiceStub },
UtilityService
]
});
service = TestBed.inject(SampleService);
});
it('should get all data', () => {
const utilityService = TestBed.inject(UtilityService);
const spy = spyOn(utilityService, 'getValues').and.returnValue(2);
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With