首页 > 解决方案 > 中 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)


    });


});

谁能帮我这个?我是节点新手。

标签: node.jssinonmedium.com

解决方案


推荐阅读