title: Error: Expected undefined to be truthy. KARMA JASMINE unit test case for angular 8
body: <p>Getting following error while running karma jasmine unit test case in angular 8 </p>
  
  <p><strong>Component: admin-dialog > onModalCancel
  Error : Expected undefined to be truthy.</strong></p>
  
  <p>I had provided my spec and typescript file. pls suggest why am getting Expected undefined to be truthy. appropriate in advance </p>
  
  <p>Spec file </p>
  
  <pre><code>import { AdminDialogComponent } from "./admin-dialog.component";
  import {TestBed, ComponentFixture, async, inject, tick, fakeAsync} from '@angular/core/testing';
  import { FormsModule } from "@angular/forms";
  import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing';
  import {TranslateModule,TranslateService} from "@ngx-translate/core";
  import {Inject, NO_ERRORS_SCHEMA} from "@angular/core";
  import {MAT_DIALOG_DATA, MatDialog, MatDialogModule, MatDialogRef} from '@angular/material/dialog';
  import { OverlayContainer } from '@angular/cdk/overlay';
  import {HttpClientModule} from "@angular/common/http";
  import {RouterTestingModule} from "@angular/router/testing";
  import {DataService} from "../../../services/data.service";
  import {of} from "rxjs/internal/observable/of";
  
  describe('Component: admin-dialog', () =&gt; {
  
    let component: AdminDialogComponent;
    let fixture: ComponentFixture&lt;AdminDialogComponent&gt;;
    let translate: TranslateService;
    let dialog: MatDialog;
    let overlayContainer: OverlayContainer;
    let dataService: DataService;
    beforeEach(() =&gt; {
      TestBed.configureTestingModule({
        imports: [FormsModule, MatDialogModule, HttpClientModule,RouterTestingModule,TranslateModule.forRoot()],
        declarations: [AdminDialogComponent],
        providers: [TranslateService,
          { provide: MatDialogRef, useValue: {addPanelClass: ()=&gt;{'close-modal'} }},
          {
            provide: MAT_DIALOG_DATA, useValue: {
              isNew: false, // I guess it's boolean
              adminObj: {firstName: "AAAAAA", lastName: "AA", password: null, emailId: "test@test.com"}
            }
          },
          {provide: dataService, useValue: {}}
        ],
        schemas: [NO_ERRORS_SCHEMA]
    });
      TestBed.overrideModule(BrowserDynamicTestingModule, {
        set: {
          entryComponents: [AdminDialogComponent]
        }
      });
      TestBed.compileComponents();
      fixture = TestBed.createComponent(AdminDialogComponent);
      component = fixture.componentInstance;
    });
    beforeEach(inject([MatDialog, OverlayContainer],
      (d: MatDialog, oc: OverlayContainer) =&gt; {
        dialog = d;
        overlayContainer = oc;
      })
    );
    afterEach(() =&gt; {
      overlayContainer.ngOnDestroy();
    });
    it('should create', () =&gt; {
      expect(component).toBeTruthy();
    });
    it("onModalCancel", () =&gt; {
        expect(component.onModalCancel()).toBeTruthy();
    });
  
  });
  </code></pre>
  
  <p><strong>Ts file</strong></p>
  
  <pre><code>import {Component, Inject, OnInit} from "@angular/core";
  import {MAT_DIALOG_DATA, MatDialogRef} from "@angular/material";
  import {DataService} from "../../../services/data.service";
  @Component({
    selector: 'admin-dialog',
    templateUrl: './admin-dialog.component.html',
    styleUrls: ['./admin-dialog.component.scss']
  })
  export class AdminDialogComponent implements OnInit {
    public accessHasSelected : any ='';
    public errorObj : any = {};
    public checkBoxSelected : boolean;
    public adminObj: any = {
      active:true,
      firstName:"",
      lastName:"",
     // emailId:"",
      access:[]
    };
    /***
     *
     * @param dialogRef
     * @param data
     * @param fromGroup
     * @param dataService
     */
    constructor(
      public dialogRef: MatDialogRef&lt;AdminDialogComponent&gt;,
      @Inject(MAT_DIALOG_DATA) public data: any,
      private dataService: DataService) {
  
      if (this.data.isNew){
        this.newAdmin();
      }
      else {
        this.editAdmin(this.data.adminObj);
      }
    }
    /*
    * This method will get called while loading new admin modal.
    * */
    newAdmin() {
      this.dataService.getAllAdminAccess().subscribe(
        response =&gt; {
          this.adminObj.access = response.access;
        },
        error =&gt; { });
    }
    /***
     * This method will get called while loading edit existing admin modal.
     * @param response
     */
    editAdmin(response){
      console.log(response);
      this.dataService.getAdminDetails(response.emailId).subscribe(
        response =&gt; {
          //component related success actions
          this.adminObj = response;
        },
        error =&gt; {});
    }
    /**
     *
     */
    ngOnInit() {}
    onModalCancel(): void {
      this.dialogRef.addPanelClass('close-modal');
      setTimeout(()=&gt;{
        this.dialogRef.close();
      },500);
    }
  }
  </code></pre>