-1

Is there any way to test the private method or declaration using "testing-library" framework in angular?

here is my code:

priave name = "somename"
 private setLang(url: string, fetch: boolean) {
    switch (url) {
          case '/SSB/mngt':
              this.SSB.Id = 'newSSb';
              this.pageId = 'ManageEvent';
            break;
          case '/SSB/mngt/add':
            this.SSB.Id = 'newSSb';
            this.pageId = 'ManageEventAdd';
          break;
          case '/SSB/mngt/view':
            this.SSB.Id = 'newSSb';
            this.pageId = 'ManageEventView';
          break;
          case '/SSB/mngt/approve':
            this.SSB.Id = 'newSSb';
            this.pageId = 'ManageEventApprove';
          break;
          default:
            this.SSB.Id = 'setupConfig';
            this.pageId = 'ViewProgMgmt';
            break;
    }

    this.data = [];
    this.data.push(this.selectedLang);
    this.data.push(this.SSB.Id);
    this.data.push(this.pageId);
    this.data.push(fetch);
    this.langId.emit(this.data);

  }

both the private name declaration and setLang method not available there is spec file. What would be the correct way to handle. it seems that, unless i write the test case for my both private declaration, i could not achieve coverage as 100%.

3gwebtrain
  • 14,640
  • 25
  • 121
  • 247
  • You don't test private methods directly. Test things through their public APIs. That shouldn't prevent you from getting a high level of coverage, because the private methods should be invoked as a result of public interactions (if they aren't, what's the point of them?) – jonrsharpe Mar 16 '20 at 12:37
  • but my jest.js show the error like where the private declination exist. here my `setLang` connected to router – 3gwebtrain Mar 16 '20 at 12:39
  • I don't understand what you're trying to say. – jonrsharpe Mar 16 '20 at 12:39
  • I am getting `uncovered lines` under the private method and declaration – 3gwebtrain Mar 16 '20 at 12:40
  • According to what? *Do* they get invoked during the tests you currently have? Give a [mcve]. – jonrsharpe Mar 16 '20 at 12:40
  • @jonrsharpe - here is https://stackblitz.com/edit/angular-private-test?file=src/app/app.component.spec.ts min. but not works – 3gwebtrain Mar 16 '20 at 12:44
  • You have almost 12k rep, you should know to put a [mre] *as text, in the question*. We're talking about tests, so a Stackblitz isn't going to be much help anyway. – jonrsharpe Mar 16 '20 at 12:46
  • @jonrsharpe - please understand, this is testing issue with private declaration in the class. when the test created, the private instances not available there is spec file. if so what can i share here? apart from just asking about? – 3gwebtrain Mar 16 '20 at 12:49
  • If you literally just mean that your test says you can't call private methods, then: yes, **that's what private means**. You also mentioned a coverage issue, but if your private methods are being invoked through the interactions you *can* make from your tests, then give a [mre] to illustrate that. If you want your private methods to be covered by tests, then interact with the SUT through its public API in a way that invokes them. – jonrsharpe Mar 16 '20 at 12:54
  • @jonrsharpe - can you please share me sample if possible? – 3gwebtrain Mar 16 '20 at 13:22
  • A sample of *not* calling something? [The docs](https://angular.io/guide/testing) have lots of examples, and your question doesn't contain enough information to be more specific about how you should test what you've written. In general, test an Angular component through: `@Input`, `@Output`, interactions with injected services and the DOM. For services etc, just injected services and the public methods they expose. – jonrsharpe Mar 16 '20 at 13:24

1 Answers1

1

We don't test private methods with Testing Library as these can change and will break your tests for no reason. If you want to have the code coverage on these lines, you will have to test them from the outside via the HTML.

This will make sure that if the implementation details change over time, your tests still pass and give you confidence in the code you write.

timdeschryver
  • 14,415
  • 1
  • 19
  • 32
  • I am ok. this issue connected with router events not html. can you show/share a sample to test the router events on `ngOnInit` - ? – 3gwebtrain Mar 17 '20 at 06:16