简体   繁体   English

覆盖测试平台提供者以在单元测试中使用其他注入值

[英]Override testbed provider to use other inject value in unit test

I am trying to override testbed provider because I have to test for the two scenarios in which the value I am injecting into the modal are different. 我试图覆盖测试平台提供程序,因为我必须测试两种情况,即我向模式中注入的值不同。

I have tried something like this till now but had no luck. 到目前为止,我已经尝试过类似的方法,但是没有运气。

  theTestBed.overrideProvider('content', { useValue: 'Pending' });
  theTestBed.compileComponents();
  fixture = theTestBed.createComponent(RequestChangeStatusModalComponent);
  component = fixture.componentInstance;
  component.ngOnInit();
  fixture.changeDetectorRef.detectChanges();
  fixture.detectChanges();
  statusOptions = component.changeStatusData.statusOptions;
  expect(statusOptions[0]).toBe('Budgeted Line Item');
  expect(statusOptions[1]).toBe('Considered for Board');

The situation is that I have a modal component to which I am injecting requestType parameter. 这种情况是我有一个模态组件,我向其中注入了requestType参数。

    @Inject('content') private requestType: string

In ngOnInit(), I am setting dropdown options depending upon what the requestType is. 在ngOnInit()中,我根据requestType是设置下拉选项。

Now, I am writing unit tests for the component. 现在,我正在为组件编写单元测试。 There are two values for requestType - Approved and Pending . 有两个值requestType - ApprovedPending

So, I am passing the Approved value to the modal when I am initializing the testbed. 因此,在初始化测试台时,我会将Approved值传递给模态。

theTestBed = TestBed.configureTestingModule({
  declarations,
  imports,
  [{
    provide: 'content',
    useValue: 'Approved'
  }]
});

But the thing is how do I reset the provider to pass the other requestType as Pending to the modal? 但是问题是如何重置提供程序以将其他requestType作为Pending传递给模式?

Can anyone tell me how can I achieve the desired outcome? 谁能告诉我如何达到理想的结果?

Maybe I can test what I am testing without overriding provider? 也许我可以在不覆盖提供程序的情况下测试我要测试的内容?

If it is a provider declared on a module level you can try to 'override' it during the configureTestingModule() call and remove the overrideProvider() call: 如果它是在模块级别声明的提供程序,则可以尝试在configureTestingModule()调用期间“覆盖”它,并删除overrideProvider()调用:

  TestBed.configureTestingModule({
    imports:   [],
    declarations: [RequestChangeStatusModalComponent],
    providers: [
      { provide: 'content', useValue: 'Pending'}
    ]
  });
  TestBed.compileComponents();

If it is a service provider that is declared on a component level you will have to use overrideComponent to be able to ovveride its providers: 如果它是在组件级别上声明的服务提供者,则必须使用overrideComponent才能检查其提供者:

  TestBed.configureTestingMod
  TestBed.overrideComponent(HeroDetailComponent, {
    set: {
      providers: [
        { provide: HeroDetailService, useClass: HeroDetailServiceSpy }
      ]
    }
  })

UPDATE Moving the createComponent() from the beforeEach into every it testcase did solve the problem in my test scenario. 更新createComponent()beforeEach移到每个it测试用例中,确实解决了我的测试场景中的问题。

  beforeEach(() => {
    // the createComponent() in the beforeEach causes the problem 
    //
    // fixture = TestBed.createComponent(FlightSearchComponent);
    // component = fixture.componentInstance;
    // fixture.detectChanges();
  });

  it('should use content Pending', () => {
    fixture = TestBed.createComponent(FlightSearchComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    expect(component.value).toBe('Pending');
  });

  it('should use content Approved', () => {
    TestBed.overrideProvider('content', { useValue: 'Content 2'});
    fixture = TestBed.createComponent(FlightSearchComponent);
    fixture.detectChanges();
    expect(component.value).toBe('Approved');
  });

The problem is that with two calls to createComponent the second call is using already instantiated ComponentFactories and ModuleFactories. 问题在于,通过两次调用createComponent ,第二次调用使用的是已实例化的ComponentFactories和ModuleFactories。

We can see that very good in the Angular source of TestBed itself: 我们可以在AngularTestBed本身中看到非常好的:

  createComponent<T>(component: Type<T>): ComponentFixture<T> {
    this._initIfNeeded();
    ..
  }

  private _initIfNeeded(): void {
    if (this._instantiated) {
      return;
    }
    ..
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 单元测试 - 如何通过在 TestBed 提供程序中使用 useValue 将依赖项注入到 TestBed - Angular Unit Test - How to inject dependencies to TestBed by using useValue in the TestBed provider 覆盖特定测试的 TestBed 提供程序 - Override a TestBed Provider for specific test Angular 单元测试:覆盖提供者 - Angular Unit Test: Override the Provider Angular 2 单元测试:如何覆盖 TestBed 中的单个提供程序(用于服务的单元测试,而不是具有服务依赖项的组件)? - Angular 2 unit tests: How to override a single provider in TestBed (for unit tests for a service, not a component, with service dependencies)? 在 Angular 和 JEST 的单元测试中没有 @Inject(&#39;environment&#39;) 的提供者 - No provider for @Inject('environment') in Unit Test with Angular & JEST Angular 9 TestBed.inject 和提供者覆盖 - Angular 9 TestBed.inject & Provider Overrides 如果我先前校准过testBed.get(),是否必须在单元测试中注入服务? - Do I have to inject the service in the unit test if I cal testBed.get() previously? Angular 单元测试 - new Service() 和 Testbed.inject() 有什么区别? - Angular Unit Test - What is the difference between new Service() and Testbed.inject()? 无法在角度测试用例中使用 TestBed.overrideProvider 覆盖 MAT_DIALOG_DATA 提供程序 - Unable to override MAT_DIALOG_DATA provider using TestBed.overrideProvider in angular test cases 带有.a​​sObservable()的Angular 2单元测试(TestBed)服务 - Angular 2 Unit Test (TestBed) service with .asObservable()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM