繁体   English   中英

如何仅从原型函数中获取参数值

[英]How to get only argument value from prototype function

下面是我的构造函数“ Req”和原型函数“ get”,以进行http请求。

 var request = require('request'); function Req(url, type, body) { this.url = url; this.username = "##############"; this.password = "##############"; this.type = type; this.body = body this.authenticationHeader = "Basic " + new Buffer(this.username + ":" + this.password).toString("base64"); } Req.prototype.get = function (callback) { request( { url: this.url, headers: { "Authorization": this.authenticationHeader } }, function (error, response, body) { if (error) { console.log("Error in Get Request is " + error) } else if (body) { console.log("******************GET Response is****************" + "\\n" + body) } callback(); } ); } 
这工作正常,我在Cucumber中这样调用此原型函数

 var testObject; Given('I have request with {string}, {string} and {string}', function (url, type, body) { testObject = new Req(url, type, body) }); When('the request is triggered', function (callback) { testObject.get(callback); }) 

但是我只想在“然后”步骤中显示响应值为“身体”的值

 Then('I should get the result', function () { How to do here?? }); 

感谢您的帮助。

errorbody值传递给回调,以便可以看到这些值。

Req.prototype.get = function(callback) {
    request({
            url: this.url,
            headers: {
                "Authorization": this.authenticationHeader
            }
        }, function(error, response, body) {
            if (error) {
                console.log("Error in Get Request is " + error)
                callback(error);
            } else {
                console.log("******************GET Response is****************" + "\n" + body)
                callback(null, body);
            } 
        }
    );
}

someObj.get(function(err, body) {
    if (!err) {
        console.log(body);
    }
});

仅供参考,我不了解量角器和黄瓜,所以我向您展示了一种纯Javascript方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM