gpt4 book ai didi

javascript - 为什么 Angular Bootstrap 模态的 Jasmine 测试失败?

转载 作者:行者123 更新时间:2023-12-03 23:11:38 24 4
gpt4 key购买 nike

我对 Angular 很陌生,所以这可能是一个愚蠢的问题,但我找不到如何在 stackoverflow 上解决我的问题(可能是因为我的知识水平),所以让我发布这个问题,如果有人我将非常感激可以帮我解决。

我正在使用 Angular Bootstrap 模态 ( https://ng-bootstrap.github.io/#/components/modal/examples ) 但始终收到仅用于测试的错误消息;

Error: No component factory found for notifyModalContent. Did you add it to @NgModule.entryComponents?.



(对话框弹出 ng serve 没有问题)

我查了 app.module.ts包含 declarations: myModalContent , imports: NgbModule(or NgbModule.forRoot()) , entryComponents: [notifyModalContent] .
我还添加了 { provide: NgbActiveModal, useClass: NgbActiveModal }product.component.spec.ts 中的 overrideComponent但仍然无法正常工作。

app.module.ts

import { NgbModule } from '@ng-bootstrap/ng-bootstrap';

import { ProductComponent } from './product/product.component';
import { myModalContent } from './my-dialog/my-dialog.component';

@NgModule({
declarations: [
ProductComponent,
myModalContent,
],
imports: [
NgbModule,
],
entryComponents: [myModalContent],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }


product.component.spec.ts

import { ProductComponent } from './product.component';
import { myModalContent } from '../my-dialog/my-dialog.component';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';


describe('ProductComponent', () => {
let component: ProductComponent;
let fixture: ComponentFixture<ProductComponent>;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ProductComponent, myModalContent ],
})
.overrideComponent(ProductComponent, {
set: {
providers: [
{ provide: DataService, useClass: MockDataService },
{ provide: NgbActiveModal, useClass: NgbActiveModal }
]
}
})
.compileComponents();
}));

product.component.ts

import { NgbModal } from "@ng-bootstrap/ng-bootstrap";
import { myModalContent } from "../my-dialog/my-dialog.component";

@Component({
selector: "app-product",
templateUrl: "./product.component.html",
styleUrls: ["./product.component.scss"]
})
export class ProductComponent implements OnInit {

constructor(
private modalService: NgbModal
) {}

errorDialog() {
const modalRef = this.modalService.open(MyModalContent, {
centered: true
});
modalRef.componentInstance.name = "Open dialog";
}

}


我删除了一些通用部分,例如异步、ComponentFicture、数据服务导入等。

最佳答案

这是我倾向于设置模态测试的方式。希望能帮助到你。

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();
});

});

关于javascript - 为什么 Angular Bootstrap 模态的 Jasmine 测试失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56538656/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com