1

我有一个返回对象的函数。在函数中,我调用了 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)


    });


});

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

4

0 回答 0