I am new to Jasmine test case. Please help in writing the Jasmine test case for below explained code lines which is used for connecting to PDC web client to access the devices using simulator.

component.ts file:

1. (<any>window).PdcWebClient.connect(this.sucesscallBack.bind(this), this.failureconnectcallBack, "http://localhost:*****/signalr");


        2.  setTimeout(() => {
        if (typeof this.devices !== "undefined") {
          if (this.devices.find(x => x.DeviceType === *****)) {
            // if (sessionStorage.getItem('scannerTest') === 'true')
              this.connectScanner(this.devices);
          }
        }
      }, 3000);
    3. connectScanner(data) {
        // const deviceName = '*****';
        const deviceName = 'Simulator';
        const port = 'COM12';
        const deviceType = ******;
        const scannerBaudrate = '******';
        for(let deviceData of data)
        {
         if(deviceData.DeviceType === ******) {
         (<any>window).PdcWebClient.configureDevice(this.successScannercallBack.bind(this), this.failureScannerCallBack.bind(this), deviceData.DeviceType, deviceData.DeviceModel, deviceData.ComPort, deviceData.Baudrate);

        }
      }
    }

component.spec.ts file:

beforeEach(async(() => {
      TestBed.configureTestingModule({
        declarations: [ManuLiteComponent],
        providers: [{ provide: Router, useClass: RouterStub },
                    { provide: service, useClass: DataservicesService },
                    { provide: scmservice, useClass: ScmService },
                    { provide: xml2jsonservice, useClass: NgxXml2jsonService },
                    { provide: changedetecter, useClass: ChangeDetectorRef },
                    { provide: httpservice, useClass: HttpClient },
                    { provide: toastr, useClass: ToastrManager },
                    {provide: translateservice, useClass: TranslateService},
                    {provide:logservice, useClass:LogService}

                   ],
        imports: [FormsModule, ReactiveFormsModule, HttpClientTestingModule,  MatButtonModule,
          MatFormFieldModule,MatAutocompleteModule,
          MatInputModule,
          MatRippleModule, MatSelectModule, HttpClientModule,ToastrModule.forRoot(),TranslateModule.forRoot({
          loader: {
            provide: TranslateLoader,
            useFactory: HttpLoaderFactory,
            deps: [HttpClient]
          }
        }),  ]
      })
        .compileComponents();
    }));    


fit('ConnectScanner Method has to be called 1', () => {
      component.ngOnInit(); 

      expect(component.connectScanner("any")).toHaveBeenCalled();
    });

Error which occurs:

TypeError: Cannot read property 'connect' of undefined
            at TestComponent.connect [as ngOnInit] (src/app/test/test.component.ts:80:29)
            at checkAndUpdateDirectiveInline (node_modules/@angular/core/fesm5/core.js:20665:1)
            at checkAndUpdateNodeInline (node_modules/@angular/core/fesm5/core.js:21929:1)
            at checkAndUpdateNode (node_modules/@angular/core/fesm5/core.js:21891:1)
            at debugCheckAndUpdateNode (node_modules/@angular/core/fesm5/core.js:22525:1)
            at debugCheckDirectivesFn (node_modules/@angular/core/fesm5/core.js:22485:1)
            at Object.eval [as updateDirectives] (ng:///DynamicTestModule/ManuLiteComponent_Host.ngfactory.js:10:5)
            at Object.debugUpdateDirectives [as updateDirectives] (node_modules/@angular/core/fesm5/core.js:22477:1)
            at checkAndUpdateView (node_modules/@angular/core/fesm5/core.js:21873:1)
            at callWithDebugContext (node_modules/@angular/core/fesm5/core.js:22767:1)
        Error: <toHaveBeenCalled> : Expected a spy, but got undefined.
        Usage: expect(<spyObj>).toHaveBeenCalled()

Need a successful test case method

0 Answers