[英]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
- Approved
和Pending
。
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: 我们可以在Angular的TestBed
本身中看到非常好的:
createComponent<T>(component: Type<T>): ComponentFixture<T> {
this._initIfNeeded();
..
}
private _initIfNeeded(): void {
if (this._instantiated) {
return;
}
..
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.