测试当公共方法调用时未调用私人方法
我有一个授权者#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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论