【问题标题】:There is a way to callThrough with jasmine.createSpyObj?有没有办法通过 jasmine.createSpyObj 调用?
【发布时间】:2021-09-01 14:20:42
【问题描述】:

如何将 callThrough 与 jasmine.createSpyObj 和带有方法返回的对象一起使用?

下面的例子不起作用。

const exampleSpy = jasmine.createSpyObj('ExampleSpy', {
        method1: () => Promise.resolve(true),
        method2: () => 'testResult'
});
exampleSpy.and.callThrough();

const res = exampleSpy.method2();
expect(res).toBe('testResult');

【问题讨论】:

  • 不应该是exampleSpy.method2.and.callThrough()吗?
  • 你好安德烈,谢谢..我找到了问题,我会回答我的问题

标签: angular typescript testing jasmine karma-jasmine


【解决方案1】:

问题在于定义间谍的方式。 在 jasmine.createSpyObject 部分阅读jasmine docs,我发现了如何正确创建间谍。

const exampleSpy = jasmine.createSpyObj('ExampleSpy', {
        method1: Promise.resolve(true),
        method2: 'testResult'
});

const res = exampleSpy.method2();
expect(res).toBe('testResult');

对象可能像 {key: returnValue }

不喜欢 {key: ()=> returnValue}

【讨论】:

    猜你喜欢
    • 2022-11-18
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    • 2023-03-27
    • 2019-11-17
    • 1970-01-01
    相关资源
    最近更新 更多