3

My problem is similar to Jasmine Angular 9 test failing because 'unreachable' stack trace at injectableDefOrInjectorDefFactory, but the solution there did not work for me.

Here is my .spec.ts file:

import { TestBed } from '@angular/core/testing';

import { DataService } from './data.service';
import { AngularFirestore } from '@angular/fire/firestore';
import { DataServiceMock, FirestoreStub } from './mock.data.service';

describe('DataService', () => {

  let service: DataService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        { provide: DataService, useClass: DataServiceMock },
        { provide: AngularFirestore, useClass: FirestoreStub },
      ]
    }).compileComponents();
    service = TestBed.get(DataService);
    service.loadData();
  });

  it('should be created', () => {
    expect(service).toBeTruthy();
  });

  it('should get have 2 products in its data', () => {
    expect(service.getData()).toEqual([
      {
        name: 'Camomile',
        description: 'Camomile oil',
        uses: ['topical', 'ingest'],
        benefits: ['headache', 'anxiety'],
      },
      {
        name: 'Cardamom',
        description: 'oil from the cardamom seed',
        uses: ['surface cleaning'],
        benefits: ['joint pain', 'hygiene'],
      }
    ]);
  });
});

For the "DataService > should be created", I get

Error: unreachable
    at injectableDefOrInjectorDefFactory (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:17298:1)
    at providerToFactory (http://localhost:9876/_karma_webpack_/node_modules/@angular/core/__ivy_ngcc__/fesm2015/core.js:17398:1)

Any help with figuring this out would be appreciated. If you need to see other code, let me know. Thanks much!

Olivia Stork
  • 4,660
  • 5
  • 27
  • 40
VictorNorman
  • 155
  • 9
  • 4
    You tried changing `useClass` to `useValue`? – AliF50 Jul 28 '20 at 14:44
  • Yes: it makes no difference. – VictorNorman Jul 28 '20 at 15:00
  • Try removing `.compileComponents()`. I am thinking this could be causing issues. Whenever `compileComponents` is ran, it is ran in an `async` zone but it is not needed for services, only for component tests. – AliF50 Jul 28 '20 at 15:05
  • Sorry. That didn't help either. I also tried replacing TestBed.get() with TestBed.inject(). It didn't change anything either. – VictorNorman Jul 28 '20 at 18:43
  • 1) async is imported from @angular/testing/core(something like this) and it is not async/await (just to be sure) 2) it is not clear what are loadData and getData - are those observables? 3) with provide: DataService, useClass: DataServiceMock - you testing own Mock - why do you need to test if you wrote it? - if you need to test DataService - don't mock it – andriishupta Jul 28 '20 at 18:56
  • I'm mocking it because the service will have internal logic that needs to be tested. When deployed it will get data from firebase; but for testing, I need to separate the internal logic from the code that gets the data from a remote source, and test the code that handles the internal logic. Neither loadData() nor getData() are async. And, I haven't added tests for the other methods I have yet to create. – VictorNorman Jul 28 '20 at 19:08
  • You need to be mocking the dependencies of `DataService`, i.e. the `FireStore`. If `DataService.getData()` is using some method on the `FireStore`, this is the method you need to mock/stub. – Amnon Jun 29 '21 at 10:44

1 Answers1

1

mistake: {provide: FeaturesToggleService, useClass: {}},

correct: {provide: FeaturesToggleService, useValue: {}}