在使用ng test命令运行测试用例时,请使用setRootViewContainerRef设置root ViewContainerRef错误。此问题在项目中使用ng6-toastr之后出现。
下面是TestBed代码:
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [FormsModule, TableModule, DialogModule, HttpClientModule, RouterTestingModule.withRoutes([{
path: 'Orders',
component: PlanOrderComponent
},
{
path: 'plan-order-history',
component: PlanOrderHistoryComponent
}
])],
declarations: [HeaderComponent, SubheaderComponent, PlanOrderComponent, FilterPlanOrderPipe,
PlanOrderHistoryComponent
],
providers: [PlanOrderService,
ConfirmationService, ToastsManager, ToastOptions, ViewContainerRef,
DataServiceService, ClientService, AppService
]
}).compileComponents();
toastsManager = TestBed.get(ToastsManager);
viewContainerRef = TestBed.get(ViewContainerRef);
planOrderService = TestBed.get(PlanOrderService);
dataService = TestBed.get(DataServiceService);
dataService.CHAT_URL = 'ws://localhost:6320/';
fixture = TestBed.createComponent(PlanOrderComponent);
component = fixture.componentInstance;
// toastsManager.setRootViewContainerRef(viewContainerRef);
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL;
jasmine.DEFAULT_TIMEOUT_INTERVAL = 100000;
}));
it('Fetch plan orders from db : Success case', async(() => {
component.bannerLabel = 'Planning Orders';
const clientSer = TestBed.get(ClientService);
const mockPlanOrder: PlanOrderModel[] = [];
spyOn(planOrderService, 'getAllPlanOrders').and.returnValue(Observable.of < PlanOrderModel[] > (mockPlanOrder));
spyOn(clientSer, 'planOrders').and.returnValue(Observable.of < PlanOrderModel[] > (mockPlanOrder));
component.ngAfterViewInit();
fixture.whenStable().then(() => {
expect(component.planOrders.length > 0).toBeTruthy();
});
}));
我尝试设置setRootViewContainerRef,但此后给出的信息为“找不到ToastContain的组件工厂,请在此处输入代码。您是否将其添加到@ NgModule.entryComponents?错误