我正在尝试测试一种启动模态的方法,以前有人这样做过吗,有人可以指出我正确的方向吗?我编写了以下测试套件。
describe('bdb modal provider test suite', () => {
let bdbModal: BdbModalProvider;
let modal: Modal;
let modalCtrl: ModalController;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
BdbModalProvider,
{ provide: ModalController, useClass: ModalControllerMock }
]
});
bdbModal = TestBed.get(BdbModalProvider);
modalCtrl = TestBed.get(ModalController);
});
beforeEach(() => {
modal = modalCtrl.create('ModalErrorPage', {});
});
it('should launch error modal', () => {
spyOn(modal, 'present');
bdbModal.launchErrModal('testing', 'error modal', 'OK');
expect(modal.present).toHaveBeenCalled();
});
});
这是错误日志:
should launch error modal bdb modal provider test suite Expected spy present to have been called. at UserContext. (webpack:///src/providers/bdb-modal/bdb-modal.spec.ts:31:30 <- test-config/karma-test-shim.js:140545:31)
最佳答案
1.创建模态和模态 Controller 的 spy 。
modalSpy = jasmine.createSpyObj('Modal', ['present']);
modalCtrlSpy = jasmine.createSpyObj('ModalController', ['create']);
modalCtrlSpy.create.and.callFake(function () {
return modalSpy;
});
2.在测试台中添加如下条目:
TestBed.configureTestingModule({
declarations: [
..............
],
providers: [
..............
{
provide: ModalController,
useValue: modalCtrlSpy
}
..............
],
imports: [
............
]
}).compileComponents();
}));
3. 测试用例如下:
it('#display() should display modal', () => {
.........
expect(modalSpy.present).toHaveBeenCalled();
.........
});
关于angular - 测试 ModalController Ionic 3 spyOn 方法未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50824441/