Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 角度8-抽象服务的单元测试_Angular_Unit Testing_Jasmine_Abstract Class - Fatal编程技术网

Angular 角度8-抽象服务的单元测试

Angular 角度8-抽象服务的单元测试,angular,unit-testing,jasmine,abstract-class,Angular,Unit Testing,Jasmine,Abstract Class,我应该为抽象服务编写一个单元测试。我们在应用程序中有两个SSE通道,我们创建了要扩展的基本SSE服务,它提供了一些基本功能 export abstract class BaseSseService { protected zone: NgZone; protected sseChannelUrl: string; private eventSource: EventSource; private reconnectFrequencySec: number = 1; pri

我应该为抽象服务编写一个单元测试。我们在应用程序中有两个SSE通道,我们创建了要扩展的基本SSE服务,它提供了一些基本功能

export abstract class BaseSseService {
  protected zone: NgZone;

  protected sseChannelUrl: string;

  private eventSource: EventSource;
  private reconnectFrequencySec: number = 1;
  private reconnectTimeout: any;

  constructor(channelUrl: string) {
    const injector = AppInjector.getInjector();
    this.zone = injector.get(NgZone);
    this.sseChannelUrl = channelUrl;
  }

  protected abstract openSseChannel(): void;
  //...
}
现在,在它被制作之前,抽象单元测试正在运行。将其更改为抽象类后,在尝试运行应创建的测试时出现此错误:错误:无法解析TestBaseService:的所有参数

我的测试如下所示:

class TestBaseSseService extends BaseSseService {
  openSseChannel() {
    console.log('Test');
  }
}

fdescribe('BaseSseService', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      providers: [
        TestBaseSseService,
        Injector,
        { provide: String, useValue: "test" }
      ]
    });
    AppInjector.setInjector(TestBed.get(Injector));
  })

  describe(':', () => {
    function setup() {
      return TestBed.get(TestBaseSseService)
    }

    it('should be created', () => {
      const service = setup();
      expect(service).toBeTruthy();
    });
  });  
});

问题是,我是否应该为抽象服务的单元测试而烦恼,如果是,如何解决这个问题?如您所见,模拟服务并不能解决这一问题,将BaseSeService添加到测试床提供者也不能解决这一问题。

它试图从抽象类提供channelUrl,但这显然不是提供者或InjectionToken

您可以更改服务以处理此问题:

class TestBaseSseService extends BaseSseService {
  constructor() {
    super('url');
  }

  openSseChannel() {
    console.log('Test');
  }
}

是的,测试抽象类总是很好的。通过这种方式,您可以确保无论何时实施它,它在正确测试时都会起同样的作用

这就解决了问题。而且不再需要在测试平台提供商中提供Channerlur。谢谢