我在单元测试方面有很多问题,这是我第一次做单元测试。嗯,我在浏览器插件测试上有一个错误:
Error: <toHaveBeenCalled> : Expected a spy, but got Function.在我的部分,我有一个:
async goToPage(path: string) {
let url_ = environment.apiUrl + path;
await Browser.open({url : url_,presentationStyle: 'popover'})
}并试图通过多种方式解决这一问题:
it('should use In App Browser', () => {
const browser = Browser;
const spy = spyOn(browser,'open');
const url = '/api/resource/TOS';
component.goToPage(url);
expect(spy.call).toHaveBeenCalled();
});
it('should use In App Browser', () => {
spyOn(Browser,'open');
expect(Browser.open).toHaveBeenCalled();
});
//Error: <toHaveBeenCalled> : Expected a spy, but got Function.
it('should use In App Browser', () => {
const browser = Browser;
spyOn(browser,'open');
expect(browser.open).toHaveBeenCalled();
});
//Expected a spy, but got Function.
it('should use In App Browser', () => {
const browser = Browser;
spyOn(browser,'open').and.returnValue(Promise.resolve());;
expect(browser.open).toHaveBeenCalled();
});
//Expected a spy, but got Function.
it('should use In App Browser', async() => {
const browser = Browser;
spyOn(browser, 'open')
expect(browser.open).toHaveBeenCalled();
});
//Error: <toHaveBeenCalled> : Expected a spy, but got Function.
//Usage: expect(<spyObj>).toHaveBeenCalled()
it('should use In App Browser', async() => {
const browser = Browser;
spyOn(browser, 'open')
expect(await browser.open).toHaveBeenCalled();
});
//Error: <toHaveBeenCalled> : Expected a spy, but got Function.
it('should create', (done) => {
let browser = Browser;
spyOn<any>(browser, 'open');
const url_ = environment.apiUrl + '/api/resource/About';
fixture.whenStable().then(() => {
expect(browser.open).toHaveBeenCalled(); expect(browser.open).toHaveBeenCalledWith({url : url_,presentationStyle: 'popover'});
done();
});
});
//Unhandled promise rejection: Error: <toHaveBeenCalled> : Expected a spy, but got Function.
//Usage: expect(<spyObj>).toHaveBeenCalled()你知道怎么解决吗?谢谢!
发布于 2022-06-16 08:05:20
看看这个官方指南:https://capacitorjs.com/docs/guides/mocking-plugins,您必须创建一个__mocks__/@capacitor文件夹(参见这里)。
https://stackoverflow.com/questions/72637702
复制相似问题