为什么茉莉花测试在Angular Bootstrap Modal上失败?

时间:2019-06-11 07:32:32

标签: javascript angular typescript angular-bootstrap

我是Angular的新手,所以这可能是一个愚蠢的问题,但是我找不到如何解决stackoverflow上的问题(可能是由于我的知识水平),所以让我发布这个问题,我会非常感谢有人可以帮助我解决问题。

我正在使用Angular Bootstrap Modal(https://ng-bootstrap.github.io/#/components/modal/examples),但始终出现错误消息,仅供测试;

  

错误:找不到用于notifyModalContent的组件工厂。您将其添加到@NgModule.entryComponents了吗?。

(弹出对话框,显示ng服务没有问题)

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

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,数据服务导入等。

1 个答案:

答案 0 :(得分:0)

这是我倾向于设置模态测试的方式。希望对您有所帮助。

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

});