Home > Back-end >  Angular Karma Writing unit tests for get method_name returns 'method_name' is not a functi
Angular Karma Writing unit tests for get method_name returns 'method_name' is not a functi

Time:06-21

My ts code is as follows,

 private _state: State = {
    page: 1,
    pageSize: 4,
    searchTerm: '',
    sortColumn: '',
    sortDirection: ''
  };
      get page() { return this._state.page; }
      get pageSize() { return this._state.pageSize; }
      get searchTerm() { return this._state.searchTerm; }

When I wrote the tests,

it('searchTerm()', () => {
        (component as any)._state = {
            page: 1,
            pageSize: 4,
            searchTerm: '',
            sortColumn: '',
            sortDirection: ''
          };
        let result = (component as any).searchTerm();
        expect(result).toEqual('');
    });

It returns the below error, Chrome 101.0.4951.67 (Windows 10) NgbdTableComplete searchTerm() FAILED TypeError: component.searchTerm is not a function at UserContext. (src/app/ngbd-table-complete/ngbd-table-complete.spec.ts:78:41) at ZoneDelegate.invoke (node_modules/zone.js/dist/zone-evergreen.js:372:1) at ProxyZoneSpec.push.QpwO.ProxyZoneSpec.onInvoke (vendor.js:62723:43) at ZoneDelegate.invoke (node_modules/zone.js/dist/zone-evergreen.js:371:1) at Zone.run (node_modules/zone.js/dist/zone-evergreen.js:134:1) at runInTestZone (vendor.js:63003:38) at UserContext. (vendor.js:63018:24) at

CodePudding user response:

It seems like that should be accessed like an actual property, without the parentheses (). You can find more info about such getters here.

  • Related