测试当公共方法调用时未调用私人方法

发布于 2025-01-24 19:59:11 字数 728 浏览 2 评论 0原文

我有一个授权者#getToken方法,该方法返回令牌。第一次被调用时,它通过将其发布到API来验证;此后每个呼叫都会返回从API呼叫中收到的令牌。我需要测试击中API的私有方法不会被拨打(通过#authenticate),当令牌已经存在时。 #sessionisvalid是检查现有令牌的功能。这是我的测试:

        it('does not authenticate', async () => {
            authorizer['sessionIsValid'] = jest.fn(() => true);

            await authorizer.getToken();
            expect(authorizer['authenticate']).not.toHaveBeenCalled();
        });

开玩笑正在抱怨

    Matcher error: received value must be a mock or spy function

我已经尝试过jest.spyon(授权器,'authenticate'),但获取type''authenticate''''''无法分配给类型'的参数' “ getToken”'

我不能完全连接这些点。关于如何实现这一目标有什么想法吗?

I have an authorizer#getToken method that returns a token. The first time it gets called, it authenticates by posting to an API; each call thereafter returns the token that was received from the API call. I need to test that the private method that hits the API does not get called (via #authenticate) when a token already exists. #sessionIsValid is the function that checks for an existing token. Here's my test:

        it('does not authenticate', async () => {
            authorizer['sessionIsValid'] = jest.fn(() => true);

            await authorizer.getToken();
            expect(authorizer['authenticate']).not.toHaveBeenCalled();
        });

Jest is complaining with

    Matcher error: received value must be a mock or spy function

I've tried jest.spyOn(authorizer, 'authenticate') but get Argument of type '"authenticate"' is not assignable to parameter of type '"getToken"'.

I can't quite connect the dots here. Any thoughts on how to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文