node.js - 中 sdk 的测试用例
问题描述
我有一个返回对象的函数。在函数中,我调用了 medium-sdk 的函数。我想存根对库函数的调用想要测试回调函数的返回值。下面是我需要测试的函数。
getOAuthAccessToken: function (clientKey, clientSecret, code, redirectUri) {
let orgInstallation = {
client_key: clientKey,
client_secret: clientSecret
};
let client = this.getInstance(orgInstallation);
client.exchangeAuthorizationCode(code, redirectUri, (err, tokenResponse) => {
if(tokenResponse){
return {
consumer_key: clientKey,
consumer_secret: clientSecret,
token: tokenResponse.access_token
};
}
debugError(`There was an error retrieving access token: ${ _.get(err, 'message') }`);
return null;
});
},
我正在尝试这样做:
beforeEach(() => {
client = new Medium.medium.MediumClient({
clientId: clientKey,
clientSecret: clientSecret
});
stubbed = sinon.stub(client, 'exchangeAuthorizationCode')
});
afterEach(() => {
stubbed.restore();
});
it('should return the access token', (done) => {
stubbed.withArgs(code,redirectUri).yields(null, tokenResponse)
Medium.getOAuthAccessToken(clientKey,clientSecret,code,redirectUri)
});
});
谁能帮我这个?我是节点新手。
解决方案
推荐阅读
- openam - OpenAM:MongoDB 或 SQL-DB 作为外部身份存储/存储库
- python - ttk.treeview selection_set 到具有特定 id 的项目
- python - 使用python将csv文件转换为txt文件时,不同行上的行之间的间距不同
- java - 使用 HTTPS TLS 1.2 配置 JBoss EAP 7
- python - 在 python 中实现构建器模式
- python - 将 BaseSensorOperator 中的模板字段用于 SFTPSensor
- ruby-on-rails - 在 Rails 6.1 中更改 has_secure_token 最小长度
- php - 在 Laravel 上访问外键
- javascript - 如何确保 axios 请求使用的是来自 redux 商店的最新令牌?
- database - MVIEW 快速刷新总是在递增之前从主表执行选择计数(*)