如何为PDC Web客户端编写Jasmine测试用例

时间:2019-05-13 07:22:19

标签: jasmine angular-testing

我是Jasmine测试案例的新手。 请帮助编写下面解释的代码行的Jasmine测试用例,该代码行用于连接到PDC Web客户端以使用模拟器访问设备。

component.ts文件:

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

需要成功的测试用例方法

0 个答案:

没有答案