question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Mocking private service in component

See original GitHub issue

Hello! 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:closed
  • Created 3 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
satanTimecommented, Oct 8, 2020

thanks, I play with it on the weekend and release an update later on.

2reactions
satanTimecommented, Oct 8, 2020

That’s right,

if you use useValue - then ngMocks doesn’t touch provided value, despite MockService was used.

if you use .mock(TmConfigLocalService) - it’s a bit more intelligent than MockService(TmConfigLocalService) and causes a call of MockInstance on initialisation of TmConfigLocalService. 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:

.mock(TmConfigLocalService, {
  treeItems$: of([])
})

and avoid MockInstance at all.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found