无法加载指令Angular Jasmine Karma的摘要

时间:2019-04-29 09:11:14

标签: angular unit-testing jasmine

问候,我正在尝试用角度7的茉莉花+业力进行单元测试,但总是会收到此错误:

  

失败:非法状态:无法加载指令摘要   DataStorageService。

我知道它与导入或声明有关,但是看不到错误

我的规格文件:

import { CUSTOM_ELEMENTS_SCHEMA, NO_ERRORS_SCHEMA, Component, NgModule } from '@angular/core';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { TranslateModule } from '@ngx-translate/core';
import { IonicModule } from '@ionic/angular';
import { FormsModule } from '@angular/forms';
import { RouterModule } from '@angular/router';
import { HttpClientModule } from '@angular/common/http';
import { BarcodeScanner } from '@ionic-native/barcode-scanner/ngx';
import { DataStorageService } from './post-data-service';
import { AppRoutingModule } from '../app-routing.module';
import { CommonModule } from '@angular/common';
import { AppComponent } from '../app.component';

describe('HomePage', () => {
  let component: DataStorageService;
  let fixture: ComponentFixture<DataStorageService>;
  let service: DataStorageService;

  beforeEach(async(() => {
    fixture = TestBed.createComponent(DataStorageService);
    component = fixture.debugElement.componentInstance;
    TestBed.configureTestingModule({
      declarations: [DataStorageService],
      schemas: [NO_ERRORS_SCHEMA],
      providers: [BarcodeScanner, DataStorageService],
      imports: [
        TranslateModule.forRoot(),
        IonicModule,
        FormsModule,
        RouterModule,
        AppRoutingModule,
        HttpClientModule,
        CommonModule]
    })
      .compileComponents();
  }));
  afterEach(() => {
    service = null;
    fixture = null;
    component = null;
  });
  it('should create', () => {
    expect(component).toBeDefined();
  });
});

我的服务

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';

//endpoint
const endpoindURL = 'https://example.com/backend';

@Injectable({
  providedIn: 'root'
})

export class DataStorageService {
  result: any;
  constructor(private http: HttpClient) {
  }

  sendCodeToServer(data) {
    const headers = new HttpHeaders().set('Content-Type', 'application/json');
    return new Promise((resolve, reject) => {
      this.http.post(endpoindURL, data, { headers: headers })
        .toPromise().then((data) => {
          resolve(data);
        }).catch((err) => {
          console.log(err);
          reject(err);
        });
    });
  }
}

0 个答案:

没有答案