hubot-test-helper icon indicating copy to clipboard operation
hubot-test-helper copied to clipboard

testing exec replies

Open simbo1905 opened this issue 6 years ago • 1 comments

With a spec:

describe 'hello-world', ->
  beforeEach ->
    @room = helper.createRoom(httpd: false)
  afterEach ->
    @room.destroy()

  context 'user says hi to hubot', (done) ->
    beforeEach ->
      co =>
        yield @room.user.say 'alice', '@hubot hi'
        yield @room.user.say 'bob',   '@hubot hi'

    it 'should reply to user', ->
      console.log(@room.messages)
      expect(@room.messages).to.eql [
        ['alice', '@hubot hi']
        ['hubot', '@alice hi']
        ['bob',   '@hubot hi']
        ['hubot', '@bob hi']
      ]

Then a synchronous reply works as expected:

     robot.respond /hi$/i, (msg) -> 
       msg.reply('hi')

If I change the response to be the stdout of an require('child_process').exec such as:

     robot.respond /hi$/i, (msg) -> 
       @exec = require('child_process').exec
       command = "echo hi"
       @exec command, { shell: '/bin/bash' } , (error, stdout, stderr) ->
         if error
            msg.send "@#{msg.message.user.name} " + "Ops! Not able to run "+ command +" ```" + stderr + "```"
         else
            msg.reply stdout

Then the test fails with only two messages in the room with no responses from hubot. Yet if I run the hubot then the output of @exec command appears as expected.

How do I test code that uses @exec command to generate hubot responses?

simbo1905 avatar May 26 '18 20:05 simbo1905

I met a similar problem, but the README taught me the answer. https://github.com/mtsmfm/hubot-test-helper#manual-delay

thinca avatar Sep 24 '18 17:09 thinca