[英]How create unit test to lib ngx-smart-modal in angular
I'm trying create tests with jasmine in a Angular 12 project and I would to test the receive of data coming of an modal opened with ngx-smart-modal
.我正在尝试在 Angular 12 项目中使用 jasmine 创建测试,我将测试使用
ngx-smart-modal
打开的模态的数据接收。 My test is structured creating and opening the modal on beforeEach
function because the behavior which I want to test is on construct.我的测试是结构化的,在
beforeEach
function 上创建和打开模态,因为我要测试的行为是在构造上。
Adding a little more of context, I'm testing a component which i call with Ngx Smart Modal trying to reproduce the same comportment calling on the component.spec.ts file a function to open it as modal.添加更多上下文,我正在测试一个组件,我使用 Ngx Smart Modal 调用该组件,试图重现调用 component.spec.ts 文件 function 以将其作为模态打开的相同组件。 I'm giving a main error whos is these: enter image description here
我给出的主要错误是谁:在此处输入图像描述
Here is the error dump:这是错误转储:
TypeError: Cannot read properties of null (reading 'markForCheck')
at NgxSmartModalComponent.markForCheck (http://localhost:9876/_karma_webpack_/webpack:/node_modules/ngx-smart-modal/__ivy_ngcc__/esm2015/ngx-smart-modal.js:295:1)
at NgxSmartModalComponent.setData (http://localhost:9876/_karma_webpack_/webpack:/node_modules/ngx-smart-modal/__ivy_ngcc__/esm2015/ngx-smart-modal.js:246:1)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/src/app/core/components/commom/verify-trade-document-infos/verify-trade-document-infos.component.spec.ts:32:8)
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-evergreen.js:364:1)
at ProxyZoneSpec.push.QpwO.ProxyZoneSpec.onInvoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:292:1)
at ZoneDelegate.invoke (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-evergreen.js:363:1)
at Zone.run (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-evergreen.js:123:1)
at runInTestZone (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:545:1)
at UserContext.<anonymous> (http://localhost:9876/_karma_webpack_/webpack:/node_modules/zone.js/dist/zone-testing.js:560:1)
at <Jasmine>
describe('PseudoComponentComponent', () => {
let component: PseudoComponentComponent;
let fixture: ComponentFixture<PseudoComponentComponent>;
let modal: NgxSmartModalComponent;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [NgxSmartModalModule, SharedModule, CoreModule],
declarations: [PseudoComponentComponent],
providers: [
{ provide: NgxSmartModalService, useValue: ngxSmartModalServiceStub },
],
}).compileComponents().
});
beforeEach(() => {
fixture = TestBed.createComponent(PseudoComponentComponent);
component = fixture.componentInstance;
modal = ngxSmartModalServiceStub
.create('myIdenfier', PseudoComponentComponent)
.setData({
mainData: new PseudoClass(),
type: 'dataType',
});
fixture.detectChanges();
});
it('should have a modalInitialized', () => {
expect(component.data).toBeInstanceOf(PseudoClass);
});
});
class NgxSmartModalServiceStub {
ngxSmartModalService: Partial<NgxSmartModalService>;
closeAll = () => null;
closeLatestModal = () => null;
getModal = (id: string): NgxSmartModalComponent =>
new NgxSmartModalComponent(null, null, null, {}, {});
getModalData = (id: string): any => this.getModal(id).getData();
getTopOpenedModal = (): NgxSmartModalComponent =>
new NgxSmartModalComponent(null, null, null, {}, {});
create = <T>(
id: string,
content: Content<T>,
options?: INgxSmartModalOptions
): NgxSmartModalComponent =>
new NgxSmartModalComponent(null, null, null, {}, {});
getStub() {
this.ngxSmartModalService = {
closeAll: this.closeAll,
closeLatestModal: this.closeLatestModal,
getModal: this.getModal,
get: this.getModal,
// getTopOpenedModal: this.getTopOpenedModal,
create: this.create,
};
return this.ngxSmartModalService;
}
}
const ngxSmartModalServiceStub = new NgxSmartModalServiceStub();
export { ngxSmartModalServiceStub };
I think, the setup is incomplete.我认为,设置不完整。 You're code seems to be missing the
TestBed.configureTestingModule()
.您的代码似乎缺少
TestBed.configureTestingModule()
。
Check this example from https://angular.io/guide/testing-components-basics#componentfixture从https://angular.io/guide/testing-components-basics#componentfixture检查这个例子
describe('BannerComponent (with beforeEach)', () => {
let component: BannerComponent;
let fixture: ComponentFixture<BannerComponent>;
beforeEach(() => {
TestBed.configureTestingModule({declarations: [BannerComponent]}); // <-- do not forget this
fixture = TestBed.createComponent(BannerComponent);
component = fixture.componentInstance;
});
it('should create', () => {
expect(component).toBeDefined(); // <-- do this test to check if the component is defined
});
});
Hello to everyone whos will be here, I found a solution its more simple than like: so lets to code: TL:DR.大家好,大家好,我找到了一个比这样更简单的解决方案:所以让我们编写代码:TL:DR。 Create yout modal component in describe function and open on beforeEach function.
在描述 function 中创建您的模态组件并在 beforeEach 函数上打开。
The describe function will be like this.描述 function 将是这样的。
describe('ComponentTest', () => {
const modal = ngxSmartModalServiceStub.create('identifier', PseudoComponent)
.setData({ data: new Data() });
})
and the before each function:以及每个 function 之前:
beforeEach(() => {
fixture = TestBed.createComponent(PseudoComponentComponent);
component = fixture.componentInstance;
fixture.detectChanges();
modal.open();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.