简体   繁体   English

Mocking 嵌套 function 调用 Jasmine

[英]Mocking Nested function calls in Jasmine

I am currently trying to test my typescript functions using Jasmine:我目前正在尝试使用 Jasmine 测试我的 typescript 函数:

//AB.ts

export async function A() {
}
export async function B() {
    A();
}

I am trying to unit test the function B by mocking out A. I want to see if A is called.我正在尝试通过 mocking 对 function B 进行单元测试。我想看看是否调用了 A。 Here is what I have:这是我所拥有的:

//AB.spec.ts
import * as AB from './AB'

describe('AB has a function B that ', () => {
    it('calls A', async () => {
        let ASpy: jasmine.Spy;
        ASpy = spyOn(AB, 'A').and.returnValue(Promise.resolve({}));
        await AB.B();
        expect(ASpy).toHaveBeenCalled();
    });
});

When I run this test, I am told that ASpy was never called.当我运行这个测试时,我被告知从未调用过 ASpy。 However upon further investigation, I found that the A function was definitely called.但是经过进一步调查,我发现肯定调用了 A function。 So it seems like the A function was called but the mock I created for the function was not triggered.所以似乎调用了 A function 但我为 function 创建的模拟没有被触发。 I've tried implementing the A function in a different file and calling that in B and mocking that out in AB.spec.ts;我尝试在不同的文件中实现 A function 并在 AB.spec.ts 中调用 B 和 mocking ; in that scenario, the test passes as the expect determines that the ASpy was called.在这种情况下,测试通过,因为期望确定调用了 ASpy。 I'm not sure why having the two functions in the same file together breaks the test but putting them in separate files passes the tests.我不确定为什么将两个函数放在同一个文件中会破坏测试,但将它们放在单独的文件中会通过测试。 Anyway, I'm hope to hear from someone soon.无论如何,我希望很快能收到某人的来信。 I'm fine with putting the functions in separate files but I want to avoid it as much as possible in future projects.我可以将函数放在单独的文件中,但我想在未来的项目中尽可能避免它。

I see that you are using我看到你正在使用

Aspy = spyOn(AB,'A'); 

where AB is not an object of a component / class, but it is just a constant that you've used to import these global functions to your spec file.其中 AB 不是组件 / class 的 object,但它只是您用来将这些全局函数导入规范文件的常量。

So the problem really doesn't lie with any of your code for testing "async" functions specifically, but it lies with the way you are creating spies for your functions without class / component objects.因此,问题实际上不在于您用于专门测试“异步”函数的任何代码,而在于您为没有 class / 组件对象的函数创建间谍的方式。 You might want to check this thread and modify your original solution to see if revisiting your spies using one of the solutions in the below thread, allows your original tests to run fine..您可能想要检查此线程并修改您的原始解决方案,以查看使用以下线程中的解决方案之一重新访问您的间谍是否允许您的原始测试运行良好..

Using Jasmine to spy on a function without an object 使用 Jasmine 监视没有 object 的 function

I was able to find a way to properly mock out A inside of B. Just needed to add the exports keyword to my function call:我能够找到一种在 B 中正确模拟 A 的方法。只需将 export 关键字添加到我的 function 调用中:

export async function B() {
    exports.A();
}

This allows the scope of B to see the definition of A while also allowing the.spec file to see that A is reverencing the exported method of A.这允许 B 的 scope 看到 A 的定义,同时也允许.spec 文件看到 A 正在尊重 A 的导出方法。

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

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