中型SDK测试用例

时间:2018-10-08 10:22:03

标签: node.js sinon medium.com

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

0 个答案:

没有答案