Mocking private service in component
See original GitHub issueHello! Thank you for the great library 😃 The question is how to modify the mock of service in the tested component. I have tried to use MockInstance, but looks like it only works for mocked components. Example:
@Component({
selector: 'test',
templateUrl: './test.html'
})
export class ComponentToTest {
constructor(private _configLocal: TmConfigLocalService){}
public afterViewInit(){
this._configLocal.treeItems$.subscribe();
}
}
What I have tried:
beforeEach(()=>{
MockInstance(ComponentToTest , {
init: (_instance, injector) => {
injector!.get(TmConfigLocalService).treeItems$ = of([]);
},
});
})
it('should create', () => {
const fixture = MockRender(ComponentToTest); // error
expect(fixture.point.componentInstance).toBeDefined();
});
Issue Analytics
- State:
- Created 3 years ago
- Comments:14 (8 by maintainers)
Top Results From Across the Web
Mocking private service in component · Issue #198 - GitHub
The question is how to modify the mock of service in the tested component. I have tried to use MockInstance, but looks like...
Read more >Angular Testing: Mock Private Functions | by David Dal Busco
To mock a private function with Jasmine, we can spy on our service private function searchDoggos and use a fake callback, callFake ,...
Read more >Unit testing with private service injected using jasmine angular2
I have a problem trying to unit test an angular service. I want to verify that this service is properly calling ...
Read more >Testing services - Angular
To test a service, you set the providers metadata property with an array of the services that you'll test or mock. app/demo/demo.testbed.spec.ts (provide ......
Read more >The correct way of mocking dependencies in the Angular unit ...
The code above shows a simple and quick solution. You can create a mock class that extends the UserService . Then you can...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
thanks, I play with it on the weekend and release an update later on.
That’s right,
if you use
useValue
- thenngMocks
doesn’t touch provided value, despiteMockService
was used.if you use
.mock(TmConfigLocalService)
- it’s a bit more intelligent thanMockService(TmConfigLocalService)
and causes a call ofMockInstance
on initialisation ofTmConfigLocalService
. Because it uses a factory https://github.com/ike18t/ng-mocks/blob/master/lib/mock-service/mock-service.ts#L336-L343,config.init(instance, injector);
- of your init function.I would recommend to use
.mock(TmConfigLocalService)
,but you can still provide your own mocks like that:
and avoid
MockInstance
at all.