1
JasmineとAngular 4で両方のメソッドをテストしようとしましたが、this.applicationRef
は常に空のオブジェクトが返されます。これを解決するには?ここで単体テストでApplicationRefを模擬する方法
は私のコードです:ここでは
@Injectable()
class Dialog {
....
getRootViewContainerRef(): ViewContainerRef {
const appInstance = this.applicationRef.components[0].instance;
if (!appInstance.viewContainerRef) {
const appName = this.applicationRef.componentTypes[0].name;
throw new Error(`Missing 'viewContainerRef' declaration in ${appName} constructor`);
}
return appInstance.viewContainerRef;
}
}
createOverlay(parentContainerRef: ViewContainerRef): ComponentRef<DialogContainerComponent> {
const rootContainerRef = parentContainerRef;
const rootInjector = rootContainerRef.injector;
const bindings = ReflectiveInjector.resolve([]);
const injector = ReflectiveInjector.fromResolvedProviders(bindings, rootInjector);
const overlayFactory = this.cfr.resolveComponentFactory(DialogContainerComponent);
return rootContainerRef.createComponent(overlayFactory, rootContainerRef.length, injector);
}
は私のテストスクリプトです:
describe('Dialog service',() => {
//let fixture: ComponentFixture<DialogInformationComponent>;
//let component: DialogInformationComponent;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [...],
providers: [
{provide: APP_BASE_HREF, useValue: '/'},
Dialog, DialogContext
]
});
}));
it('Dialog should be showed.', inject([Dialog], (service: Dialog) => {
let res: any;
service.open(DialogInformationComponent, message).subscribe((result) => {
res = result;
});
expected(true).toBeTruethy();
}));
});
しかし、ApplicationRef
は常に空です: