首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >角度测试:在电容浏览器插件中“期望间谍,但得到功能”

角度测试:在电容浏览器插件中“期望间谍,但得到功能”
EN

Stack Overflow用户
提问于 2022-06-15 20:52:33
回答 1查看 50关注 0票数 0

我在单元测试方面有很多问题,这是我第一次做单元测试。嗯,我在浏览器插件测试上有一个错误:

代码语言:javascript
复制
Error: <toHaveBeenCalled> : Expected a spy, but got Function.

在我的部分,我有一个:

代码语言:javascript
复制
    async goToPage(path: string) {
        let url_ = environment.apiUrl + path;
        await Browser.open({url : url_,presentationStyle: 'popover'})
    }

并试图通过多种方式解决这一问题:

代码语言:javascript
复制
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()

你知道怎么解决吗?谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-16 08:05:20

看看这个官方指南:https://capacitorjs.com/docs/guides/mocking-plugins,您必须创建一个__mocks__/@capacitor文件夹(参见这里)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72637702

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档