Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.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
Javascript 重写单元测试中的注入令牌_Javascript_Angular_Unit Testing_Jestjs - Fatal编程技术网

Javascript 重写单元测试中的注入令牌

Javascript 重写单元测试中的注入令牌,javascript,angular,unit-testing,jestjs,Javascript,Angular,Unit Testing,Jestjs,我有一个带有注入令牌的简单服务,用于为服务提供某种配置。一切正常。尽管我无法在单个规范文件中测试所有可能的场景。出于某种原因,我不允许覆盖提供者中定义的注入令牌 token.config.ts export interface MyConfig = { test: boolean; } export const ConfigToken = new InjectionToken<MyConfig>('MyConfig'); @Injectable() export clas

我有一个带有注入令牌的简单服务,用于为服务提供某种配置。一切正常。尽管我无法在单个规范文件中测试所有可能的场景。出于某种原因,我不允许覆盖提供者中定义的注入令牌

token.config.ts

export interface MyConfig = {
    test: boolean;
}

export const ConfigToken = new InjectionToken<MyConfig>('MyConfig');
@Injectable()
export class TokenRefreshService {

  public autoRefreshStarted = false;

  constructor(
    @Inject(ConfigToken) private config: MyConfig
  ) {}

  public initAutoRefresh(): void {
    if ( this.config.test === true ) {
      this.autoRefreshStarted = true;
    }
  }
}
describe('TokenRefreshService (with-auto-refresh)', () => {

  let service: TokenRefreshService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        {
          provide: ConfigToken,
          useValue: {}
        },
        TokenRefreshService
      ]
    });

    service = TestBed.get( TokenRefreshService );
  });

  it('should create an instance', () => {
    expect( service ).toBeDefined();
  });

  it('should not start auto-refresh', () => {

    service.initAutoRefresh();

    expect( service.autoRefreshStarted ).toBeFalsy();
  });


  it('should start auto-refresh', () => {
    TestBed.overrideProvider( ConfigToken, { useValue: { test: true  } }); /// doesn't override the config token at all ///

    service.initAutoRefresh();

    expect( service.autoRefreshStarted ).toBeTruthy();
  });

});
token.service.spec.ts

export interface MyConfig = {
    test: boolean;
}

export const ConfigToken = new InjectionToken<MyConfig>('MyConfig');
@Injectable()
export class TokenRefreshService {

  public autoRefreshStarted = false;

  constructor(
    @Inject(ConfigToken) private config: MyConfig
  ) {}

  public initAutoRefresh(): void {
    if ( this.config.test === true ) {
      this.autoRefreshStarted = true;
    }
  }
}
describe('TokenRefreshService (with-auto-refresh)', () => {

  let service: TokenRefreshService;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        {
          provide: ConfigToken,
          useValue: {}
        },
        TokenRefreshService
      ]
    });

    service = TestBed.get( TokenRefreshService );
  });

  it('should create an instance', () => {
    expect( service ).toBeDefined();
  });

  it('should not start auto-refresh', () => {

    service.initAutoRefresh();

    expect( service.autoRefreshStarted ).toBeFalsy();
  });


  it('should start auto-refresh', () => {
    TestBed.overrideProvider( ConfigToken, { useValue: { test: true  } }); /// doesn't override the config token at all ///

    service.initAutoRefresh();

    expect( service.autoRefreshStarted ).toBeTruthy();
  });

});
我想在没有为服务提供配置或提供了不正确数据的配置等情况下测试场景。因此,我确实需要一种方法以某种方式覆盖传递给
令牌服务的注入令牌。但无论我尝试了什么,它都会不断返回TestBed.configureTestingModule中定义的相同数据


我做错什么了吗?还是有更简单的方法

好吧,你在每次之前都会得到服务,然后试着override@Alexus你找到解决方法了吗?没有,我没有找到任何有用的方法来解决这个问题。