Как написать контрольный пример жасмина для подключения веб-клиента PDC в угловых - PullRequest
0 голосов
/ 13 мая 2019

Я новичок в тесте Jasmine.Пожалуйста, помогите в написании тестового примера Jasmine для приведенных ниже строк кода, который используется для подключения к веб-клиенту PDC для доступа к устройствам с помощью симулятора.

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:

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();
    });

Ошибка, которая возникает:

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()

Необходим успешный метод теста

...