Angular service test - TestBed ignores one of the mocks

298 views Asked by At

I am having an issue with one of my spy objects - somehow TestBed seems to ignore it at all.

The spy for StateService works perfect, but the one for RoutingService keeps failing with: NullInjectorError: No provider for ActivatedRoute! (the real RoutingService uses Activated Route).

Here's my service.ts:

@Injectable({
  providedIn: 'root',
})        
export class UserInteractionService {
  constructor(
   private stateService: StateService,
   private routingService: RoutingService
   ) {}
       
   handleUserInteraction(...) {
     // ...
     this.routingService.updateUrl();
   }
}

Here's service.spec.ts:

describe('UserInteractionService', () => {
  let service: UserInteractionService;

  const mockRoutingService = jasmine.createSpyObj(['updateUrl']);
  const mockStateService = jasmine.createSpyObj(['copyState']);

  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        {
          provide: StateService,
          useValue: mockStateService,
        },
        {
          provide: RoutingService,
          useValue: mockRoutingService, // not working??
        }
      ],
    });

    service = TestBed.inject(UserInteractionService);
  });

  it('should create', () => {
    expect(service).toBeTruthy();
  });
});
0

There are 0 answers