Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在Node.js中使用Jasmine测试child process.send_Javascript_Node.js_Unit Testing_Testing_Jasmine Node - Fatal编程技术网

Javascript 在Node.js中使用Jasmine测试child process.send

Javascript 在Node.js中使用Jasmine测试child process.send,javascript,node.js,unit-testing,testing,jasmine-node,Javascript,Node.js,Unit Testing,Testing,Jasmine Node,我有一个Node.js应用程序,它有一个主进程.js和一个子进程.js main process.js如下所示: var childProcess = require('child_process'); var job = childProcess.spawn('node', ["child-process.js"], { detached = true, stdio: ['ipc'] }); Mychild process.js执行一些任务,并使用以下命令通知父进程其状态: expo

我有一个Node.js应用程序,它有一个
主进程.js
和一个
子进程.js

main process.js
如下所示:

var childProcess = require('child_process');
var job = childProcess.spawn('node', ["child-process.js"], {
  detached = true,
  stdio: ['ipc']
});
My
child process.js
执行一些任务,并使用以下命令通知父进程其状态:

exports.init = function() {
   //some processing here
   process.send({status: 'success or pending'});
}
现在我想使用单元测试
child process.js
,但是当我从规范中调用方法
init()
时,
jasmine node
抛出一个错误:

TypeError: Object #<process> has no method 'send'
TypeError:对象#没有方法“send”
有没有办法模拟
过程
变量?换句话说,我如何对这个场景进行单元测试

有没有办法模拟流程变量?换句话说,我如何对这个场景进行单元测试

无需导出
子进程.js中的
进程.send()
功能。 您可以将
process.send()
放在正文中的任何位置,以便与
main process.js进行通信

我已使用Jasmine成功运行以下代码:

main process.js

var childProcess = require('child_process');

// set an infinite loop that keeps main-process running endlessly
setInterval(() => null, 5000)

// spawn child process
var job = childProcess.spawn('node', ["child-process.js"], {
  detached: true,
  stdio: ['ipc']
});

// listen to the events returned by child process
job.on('message', (code, signal) =>
  console.log('[MAIN] received the ff from child process:', {code, signal}, 'at', new Date())
)

// export the job object, which is an event emitter that can be tested by Jasmine pkg
module.exports = job
const message = {
    message: `I'm working on it...`,
    status: 'success or pending'
}

// send a message to the parent every half second
setInterval(() => process.send(message), 500)
const main = require('../main-process')

describe('main process', () =>
    it('should receive messages from spawned child process', (done) => {
        let eventCount = 0
        main.on('message', (code, signal) => {
            console.log('[JASMINE] received the event')
            eventCount++
            if (eventCount >= 5) {
                done()
            }
        })
    })
)
子进程.js

var childProcess = require('child_process');

// set an infinite loop that keeps main-process running endlessly
setInterval(() => null, 5000)

// spawn child process
var job = childProcess.spawn('node', ["child-process.js"], {
  detached: true,
  stdio: ['ipc']
});

// listen to the events returned by child process
job.on('message', (code, signal) =>
  console.log('[MAIN] received the ff from child process:', {code, signal}, 'at', new Date())
)

// export the job object, which is an event emitter that can be tested by Jasmine pkg
module.exports = job
const message = {
    message: `I'm working on it...`,
    status: 'success or pending'
}

// send a message to the parent every half second
setInterval(() => process.send(message), 500)
const main = require('../main-process')

describe('main process', () =>
    it('should receive messages from spawned child process', (done) => {
        let eventCount = 0
        main.on('message', (code, signal) => {
            console.log('[JASMINE] received the event')
            eventCount++
            if (eventCount >= 5) {
                done()
            }
        })
    })
)
spec.js

var childProcess = require('child_process');

// set an infinite loop that keeps main-process running endlessly
setInterval(() => null, 5000)

// spawn child process
var job = childProcess.spawn('node', ["child-process.js"], {
  detached: true,
  stdio: ['ipc']
});

// listen to the events returned by child process
job.on('message', (code, signal) =>
  console.log('[MAIN] received the ff from child process:', {code, signal}, 'at', new Date())
)

// export the job object, which is an event emitter that can be tested by Jasmine pkg
module.exports = job
const message = {
    message: `I'm working on it...`,
    status: 'success or pending'
}

// send a message to the parent every half second
setInterval(() => process.send(message), 500)
const main = require('../main-process')

describe('main process', () =>
    it('should receive messages from spawned child process', (done) => {
        let eventCount = 0
        main.on('message', (code, signal) => {
            console.log('[JASMINE] received the event')
            eventCount++
            if (eventCount >= 5) {
                done()
            }
        })
    })
)
输出

$ npm test

> so-jasmine-test@1.0.0 test C:\Users\jonathanlopez\nodejs\so-jasmine-test
> jasmine

Randomized with seed 29172
Started
[MAIN] received the ff from child process: { code:
   { message: 'I\'m working on it...',
     status: 'success or pending' },
  signal: undefined } at 2018-10-17T08:50:51.559Z
[JASMINE] received the event
[MAIN] received the ff from child process: { code:
   { message: 'I\'m working on it...',
     status: 'success or pending' },
  signal: undefined } at 2018-10-17T08:50:52.060Z
[JASMINE] received the event
[MAIN] received the ff from child process: { code:
   { message: 'I\'m working on it...',
     status: 'success or pending' },
  signal: undefined } at 2018-10-17T08:50:52.562Z
[JASMINE] received the event
[MAIN] received the ff from child process: { code:
   { message: 'I\'m working on it...',
     status: 'success or pending' },
  signal: undefined } at 2018-10-17T08:50:53.064Z
[JASMINE] received the event
[MAIN] received the ff from child process: { code:
   { message: 'I\'m working on it...',
     status: 'success or pending' },
  signal: undefined } at 2018-10-17T08:50:53.565Z
[JASMINE] received the event
.


1 spec, 0 failures
Finished in 2.736 seconds
Randomized with seed 29172 (jasmine --random=true --seed=29172)