简体   繁体   English

SpyOn on submodule 方法在茉莉花中不返回间谍

[英]SpyOn on submodule method returns no spy in jasmine

I stumble on a problem that I cannot solve.我偶然发现了一个我无法解决的问题。 I tried to spy on a sub-modules method but got the error that the moduleSpy is not a spy with the following error message.我试图监视子模块方法,但收到错误消息,即 moduleSpy 不是间谍,并显示以下错误消息。 The module is an npm package.该模块是一个 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();
    });

})

Does anyone have an idea why the spy is not recognized?有谁知道为什么间谍不被承认?

What you have worked on earlier versions of Angular/TypeScript but not on later versions of it.你在早期版本的 Angular/TypeScript 上所做的工作,但在它的更高版本上没有。

This is a long thread explaining it: https://github.com/jasmine/jasmine/issues/1414这是一个解释它的长线程: https : //github.com/jasmine/jasmine/issues/1414

These are some solutions: Can webpack 4 modules be configured as to allow Jasmine to spy on their members?这些是一些解决方案: 可以将 webpack 4 模块配置为允许 Jasmine 监视其成员吗?

The github thread has some solutions but none of those solutions worked for me. github 线程有一些解决方案,但这些解决方案都不适合我。

My favourite solution was this one though:我最喜欢的解决方案是这个:

Error: supportsScrollBehavior is not declared configurable 错误:supportsScrollBehavior 未声明为可配置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM