Skip to main content

ngMocks.stubMember

ngMocks.stubMember facilitates injection of existing spies or defined values into instances.

// overriding the methodngMocks.stubMember(service, method, customCallback);
// overriding the property's valuengMocks.stubMember(service, property, customValue);
// overrding the getter, does not touch the existing setterngMocks.stubMember(service, property, customGetter, 'get');
// overrding the setter, does not touch the existing getterngMocks.stubMember(service, property, customSetter, 'set');

It returns the passed value, therefore, this allows fast chains for spies and mocks.

ngMocks.stubMember(service, 'handler', jasmine.createSpy('handler'))  .and.returnValue('fake');
ngMocks.stubMember(service, 'read', jasmine.createSpy('read'), 'set')  .and.toThrowError();

If we need to stub a method of a service in Angular tests:

const service = TestBed.inject(Service);ngMocks.stubMember(service, 'handler', () => 'fake');// service.handler() === 'fake'

If we need to stub a property of a component in Angular tests:

const component = TestBed.createComponent(Component)  .componentInstance;ngMocks.stubMember(service, 'name', 'mock');// service.name === 'mock'

If we need to stub a getter in Angular tests:

const service = TestBed.inject(Service);ngMocks.stubMember(service, 'name', () => 'mock', 'get');// service.name === 'mock'

If we need to stub a setter in Angular tests:

const service = TestBed.inject(Service);let value: any;ngMocks.stubMember(service, 'name', v => (value = v), 'set');// value === undefinedservice.name = 'fake';// value === 'fake'