【问题标题】:SpyOn on submodule method returns no spy in jasmine子模块方法上的 SpyOn 在茉莉花中没有返回间谍
【发布时间】:2021-12-12 13:46:24
【问题描述】:

我偶然发现了一个我无法解决的问题。我试图监视子模块方法,但收到错误消息,即 moduleSpy 不是间谍,并显示以下错误消息。该模块是一个 npm 包。

Error: <toHaveBeenCalled> : Expected a spy, but got undefined.

import * as module from 'package';
import { TOKEN } from 'injectionToken.ts'

describe('ExampleComponent', () => {
    let component: ExampleComponent;
    let fixture: ComponentFixture<ExampleComponent>;
    let moduleSpy;

    beforeEach(async(() => {
        moduleSpy = spyOn(module.sub, 'subModuleMethod');

        TestBed.configureTestingModule({
            declarations [ExampleComponent],
            providers: [
                { provide: TOKEN, useValue: module }
            ]
        }).compileComponents();
    }));

    beforeEach(() => {
        fixture = TestBed.createComponent(ExampleComponent);
        component = fixture.componentInstance;
    });

    it('test specific function call', () => {
        // press button to test the sub module method
        expect(moduleSpy.subModuleMethod).toHaveBeenCalled();
    });

})

有人知道为什么间谍无法识别吗?

【问题讨论】:

  • 你试过 expect(moduleSpy).toHaveBeenCalled(); ?
  • @enno.void 我试过了,得到了类似的错误信息。 Expected spy subModuleMethod to have been called.

标签: angular unit-testing jasmine karma-runner spyon


【解决方案1】:

您在早期版本的 Angular/TypeScript 上所做的工作,而不是在它的更高版本上所做的工作。

这是一个解释它的长线程:https://github.com/jasmine/jasmine/issues/1414

以下是一些解决方案: Can webpack 4 modules be configured as to allow Jasmine to spy on their members?

github 线程有一些解决方案,但这些解决方案都不适合我。

我最喜欢的解决方案是这个:

Error: supportsScrollBehavior is not declared configurable

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-02
    • 1970-01-01
    • 2020-01-28
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 2012-08-15
    相关资源
    最近更新 更多