Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Jasmine mock getters, class variables, class functions
describe('Service Test', () => {
let service: SomeService;
let stubbedService: jasmine.SpyObj<StubbedService>;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{
provide: StubbedService,
useValue: {
...jasmine.createSpyObj('stubbedService', ['serviceFunc1', 'serviceFunc2']),
classVariable: undefined,
classGetter: undefined,
} as jasmine.SpyObj<StubbedService>,
},
],
});
service = TestBed.get(SomeService);
stubbedService = TestBed.get(StubbedService);
});
describe('now we have spies for all StubbedService properties', () => {
it('spy on all', () => {
stubbedServie.classVariable = 'some mocked stuff';
stubbedService.classGetter = 'more mocked stuff';
stubbedService.serviceFunc1.and.returnValue('mocked function return');
stubbedService.serviceFunc2.and.returnValue(of('mocked function observale return'));
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.