[英]Jasmine spy not working on Capacitor Plugin in Ionic App
I'm trying to set up an unit test for an Ionic App.我正在尝试为 Ionic 应用程序设置单元测试。 In my specs I have:
在我的规格中,我有:
it('should not change status bar on browser', async () => {
spyOn(Plugins.StatusBar, 'setStyle');
const service: AppService = TestBed.inject(AppService);
await service.initApp();
expect(Plugins.StatusBar.setStyle).not.toHaveBeenCalled();
});
But it fails with:但它失败了:
Error: <toHaveBeenCalled> : Expected a spy, but got Function.
Usage: expect(<spyObj>).toHaveBeenCalled() in node_modules/jasmine-core/lib/jasmine-core/jasmine.js (line 5180)
<Jasmine>
http://localhost:9876/_karma_webpack_/main.js:1161:112
<Jasmine>
fulfilled@http://localhost:9876/_karma_webpack_/main.js:716:62
run@http://localhost:9876/_karma_webpack_/polyfills.js:3595:53
http://localhost:9876/_karma_webpack_/polyfills.js:4329:43
runTask@http://localhost:9876/_karma_webpack_/polyfills.js:3639:61
drainMicroTaskQueue@http://localhost:9876/_karma_webpack_/polyfills.js:4041:46
<Jasmine>
I've setup a mock in my project for testing with this same object.我在我的项目中设置了一个模拟,用于使用相同的对象进行测试。 Your problem is that you're trying to call
spyOn()
on an object that isn't a mock.你的问题是你试图在一个不是模拟的对象上调用
spyOn()
。
The following incorporates elements that worked for my tests:以下包含适用于我的测试的元素:
it('should not change status bar on browser', async () => {
statusBarSpy = jasmine.createSpyObj<StatusBarPlugin>('StatusBar', ['setStyle']);
const service: AppService = TestBed.inject(AppService);
await service.initApp();
expect(statusBarSpy.setStyle).not.toHaveBeenCalled();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.