Home > Enterprise >  Angular Testing 'Expected undefined to be truthy.'
Angular Testing 'Expected undefined to be truthy.'

Time:12-27

This error comes up even with just the boiler plate for the spec file. Is there any other issues causing this?

ts file

import { Component, OnInit, Input, Output, EventEmitter, ViewContainerRef } from '@angular/core';
import { NzNotificationService } from 'ng-zorro-antd';
import { environment } from 'environments/environment';
// import { TranslateService } from '@ngx-translate/core';1
// import { Router, ActivatedRoute } from '@angular/router';2
import {
  ValidationUtilService,
  ApiRequestService,
  LookupDataService,
  CommonUtilty,
  I18nService,
  MultiLingualService,
  InteractService,
  ClientService,
  ComponentOutputDO,
  AppMetadataHandler
} from 'app'
import { AbstractControl, FormBuilder, FormControl, FormGroup, FormArray, Validators } from '@angular/forms';
import * as moment from 'moment/moment';
import { take } from 'rxjs/operators';
@Component({
  selector: 'app-view-all-profiles',
  templateUrl: './view-all-profiles.component.html',
  styleUrls: ['./view-all-profiles.component.scss']
})
export class ViewAllProfilesComponent implements OnInit {

  _currentComponentName = 'ViewAllProfilesComponent'

  constructor
  (
    private viewContainerRef: ViewContainerRef,
    // private validationUtilService: ValidationUtilService,
    // private apiRequestService: ApiRequestService,
//     // private route: ActivatedRoute,
//     // private router: Router,
    // private fb: FormBuilder,
    // private commonUtilty: CommonUtilty,
    // private i18nService: I18nService,
    // private multiLingualService: MultiLingualService,
//     private translateService: TranslateService,
    // private lookupDataService: LookupDataService,
    // private clientService: ClientService,
    // private interactService: InteractService,
    // public appMetadataHandler: AppMetadataHandler
    ) { }

It is just the boiler plate and when I debugged, it shows the fixture is undefined. I also noticed that my fixture get defined when I remove all the injected services

CodePudding user response:

In your test file you are recreating the module using configureTestingModule function. The fixture is undefined because some of the services cannot be injected by DI. So you might need to import or mock the dependencies defined in app.module (or feature module). From what I can see from the code you provided you are missing at least RouterTestingModule (to provide Router), TranslateModule (from ngx-translate), ReactiveFormsModule (to provide FormBuilder). If you are not mocking your application services, you might need to import their dependencies too (like HttpClientTestingModule).

CodePudding user response:

inject your services I spec file that has been used in your ts file and mock your service file. createSpyobj for your services and inject your component as well in the spec file. if this works properly please tick this answer. Thankyou

  • Related