【问题标题】:Proxyquire : Proxy-require yargs and pass values to itProxyquire : 代理需要 yargs 并将值传递给它
【发布时间】:2019-07-11 19:16:43
【问题描述】:

我的代码如下 src/example.js:

var argv = require('yargs')
.usage('service 1.\nUsage: $0 -s [host] -n [port]')
.alias('s', 'host') 
.alias('n', 'port')
.describe('host', 'Hostname to be used')
.describe('port', 'Port on which service will run')
.argv;  //--->> Can this line even be stubbed??


function a(){
    console.log(argv.host.toString());
    console.log(argv.port.toString())
}


module.exports = {a}

上面的单元测试用例,我正在尝试编写:

const chai = require('chai');
const proxyquire = require('proxyquire');
const sinon = require('sinon');
var expect = require('chai').expect;

describe("Add service", function() {
    let roo;
    let argv;
    beforeEach(function() {
         var argv = {}
         argv.host = 'xx.xx.xx.xx';
         argv.port = '7677'
         roo = proxyquire('../src/example.js', { 'yargs' : { argv: argv}} );

    })

    it("Response should be logged", function() {
         roo.a()
    })

})

当我运行上面的代码时,我得到以下错误:

Add service
1) "before each" hook for "Response should be logged"



  0 passing (8ms)
  1 failing

  1) Add service "before each" hook for "Response should be logged":
   TypeError: Cannot read property 'toString' of undefined
    at a (src/example.js:14:31)

我在这里做错了什么?

编辑:显然,如果我将 src/example.js 编写如下:

var argv = require("yargs")
                .option('host', {
                       alias: 's',
                       demand: false,
                       describe: 'Hostname to be used',
                       default: "xx.xx.xx.xx"
                }) 
                .option('port', {
                       alias: 'n',
                       demand: true,
                       describe: "sme port",
                       default: "7677"
                })
                .help()
                .alias('help', 'h')
                .argv


function a(done){
   console.log(argv.machineHost.toString());
   console.log(argv.servicePort.toString())
   done(argv.machineHost)
}

module.exports = {a}

并编写如下测试用例:

const chai = require('chai');
const proxyquire = require('proxyquire');
const sinon = require('sinon');
var expect = require('chai').expect;

describe("Add service", function() {
    let roo;
    let argv;
    beforeEach(function() {
         roo = proxyquire('../src/example.js', {} );

    })

    it("Response should be logged", function() {
         roo.a(function(resp){
            chai.expect(resp).to.equal("xx.xx.xx.xx")
         })
    })

})

有效!去搞清楚!这可能是因为我在 yargs 中设置了默认值。但是如果不进行上述更改,我不知道如何让它工作!

【问题讨论】:

    标签: node.js proxyquire yargs


    【解决方案1】:

    这意味着'yargs'对象没有hostport属性。

    试试:

    const chai = require('chai');
    const proxyquire = require('proxyquire');
    const sinon = require('sinon');
    var expect = require('chai').expect;
    
    describe("Add service", function() {
        let roo;
    
        beforeEach(function() {
          roo = proxyquire('../src/example.js', { 'yargs' :  {
           host: 'xx.xx.xx.xx',
           port: '7677',
          }});
         })
    
        it("Response should be logged", function() {
             roo.a()
        })
    })
    

    【讨论】:

    • 不。如果我使用您给出的答案,仍然会出现同样的错误。
    猜你喜欢
    • 2017-04-24
    • 2018-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多