简体   繁体   English

如何在Node.js中的Mocha测试中为函数提供标头

[英]How to provide a header to a function in mocha test in node.js

I am trying to unit test the validate function 我正在尝试对验证功能进行单元测试

test function: 测试功能:

it('validating request', function(done) {
    var API = new API(Id, key , List);
    assert.isTrue(API.validate());

    done();
  }); 

main api: 主要API:

API.prototype.validate = function validate(req, res) {

  var api_req = req.headers['reqH'];
  if (api_req == null || api_req == '') {
    res.send("Error:");
    return;
  }

I am getting the following error: Cannot read property 'headers' of undefined. 我收到以下错误:无法读取未定义的属性“标头”。

In the following line you use headers on req which is one of the parameters of the function 在下面的行中,您在req上使用headers ,它是函数的参数之一

var api_req = req.headers['reqH'];

but when you call the validate function, you didn't pass any parameters to it: 但是,当您调用validate函数时,您没有向其传递任何参数:

assert.isTrue(API.validate());

so req and res are undefined for this call 因此此调用的reqres undefined

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

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