Javascript 为什么jasmine测试在角度引导模式下失败?
我对Angular非常陌生,所以这可能是一个愚蠢的问题,但我找不到如何解决stackoverflow上的问题(可能是因为我的知识水平),所以让我发布这个问题,如果有人能帮我解决,我将非常感激 我使用的是Angular Bootstrap Modal(),但只有测试时才会出现错误消息 错误:找不到notifyModalContent的组件工厂。您是否已将其添加到Javascript 为什么jasmine测试在角度引导模式下失败?,javascript,angular,typescript,angular-bootstrap,Javascript,Angular,Typescript,Angular Bootstrap,我对Angular非常陌生,所以这可能是一个愚蠢的问题,但我找不到如何解决stackoverflow上的问题(可能是因为我的知识水平),所以让我发布这个问题,如果有人能帮我解决,我将非常感激 我使用的是Angular Bootstrap Modal(),但只有测试时才会出现错误消息 错误:找不到notifyModalContent的组件工厂。您是否已将其添加到@NgModule.entryComponents (弹出ng serve的对话框,没有问题) 我选中了app.module.ts包含声明
@NgModule.entryComponents
(弹出ng serve的对话框,没有问题)
我选中了app.module.ts
包含声明:myModalContent
,导入:NgbModule(或NgbModule.forRoot())
,入口组件:[notifyModalContent]
。
另外,我在product.component.spec.ts
中的overrideComponent中添加了{provide:NgbActiveModal,useClass:NgbActiveModal}
,但仍然不起作用
app.module.ts
从'@ng bootstrap/ng bootstrap'导入{NgbModule};
从“./product/product.component”导入{ProductComponent};
从“./my dialog/my dialog.component”导入{myModalContent};
@NGD模块({
声明:[
产品组件,
myModalContent,
],
进口:[
NGB模块,
],
entryComponents:[myModalContent],
提供者:[],
引导:[AppComponent]
})
导出类AppModule{}
产品.部件.规格.ts
从“/product.component”导入{ProductComponent};
从“../my dialog/my dialog.component”导入{myModalContent};
从'@ng bootstrap/ng bootstrap'导入{NgbActiveModal};
描述('ProductComponent',()=>{
let组件:ProductComponent;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[ProductComponent,myModalContent],
})
.overrideComponent(产品组件{
设置:{
供应商:[
{provide:DataService,useClass:MockDataService},
{提供:NgbActiveModal,useClass:NgbActiveModal}
]
}
})
.compileComponents();
}));
product.component.ts
从“@ng bootstrap/ng bootstrap”导入{NgbModal}”;
从“./my dialog/my dialog.component”导入{myModalContent};
@组成部分({
选择器:“应用程序产品”,
templateUrl:“./product.component.html”,
样式URL:[“/product.component.scss”]
})
导出类ProductComponent实现OnInit{
建造师(
专用modalService:NgbModal
) {}
errorDialog(){
const modalRef=this.modalService.open(MyModalContent{
对
});
modalRef.componentInstance.name=“打开对话框”;
}
}
我删除了一些通用部分,如异步、组件、数据服务导入等。这是我设置模态测试的方式。希望能有所帮助
import { ProductComponent } from './product.component';
import { myModalContent } from '../my-dialog/my-dialog.component';
import { NgbActiveModal, NgbModal, NgbModule } from '@ng-bootstrap/ng-bootstrap';
describe('ProductComponent', () => {
let component: ProductComponent;
let fixture: ComponentFixture<ProductComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
ProductComponent,
myModalContent
],
imports: [
NgbModule,
],
providers: [
{ provide: DataService, useClass: MockDataService },
NgbActiveModal,
NgbModal
]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ProductComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
});
从“/product.component”导入{ProductComponent};
从“../my dialog/my dialog.component”导入{myModalContent};
从'@ng bootstrap/ng bootstrap'导入{NgbActiveModal,NgbModal,NgbModule};
描述('ProductComponent',()=>{
let组件:ProductComponent;
let夹具:组件夹具;
beforeach(异步(()=>{
TestBed.configureTestingModule({
声明:[
产品组件,
myModalContent
],
进口:[
NGB模块,
],
供应商:[
{provide:DataService,useClass:MockDataService},
NgbActiveModal,
NGB模式
]
})
.compileComponents();
}));
在每个之前(()=>{
fixture=TestBed.createComponent(ProductComponent);
组件=fixture.componentInstance;
fixture.detectChanges();
});
});
如果您能提供您的product.component.ts,诊断问题会很有帮助。我添加了product.component.ts
。它实际上是很长的代码,所以我只提供了模态部分。我希望这足够了,谢谢!