LoginSignup
9
9

More than 5 years have passed since last update.

hubotでのbot開発とテスト環境整備まで

Last updated at Posted at 2016-05-27

hubotの開発環境構築とプロジェクト作成

  • hubotのドキュメントを読んでプロジェクト作成
    • yo hubotまで
  • テストで使うライブラリをdevDependenciesに追加
  "devDependencies": {
    "mocha": "^2.5.3",
    "chai": "^3.5.0",
    "coffee-script": "^1.10.0",
    "hubot-test-helper": "^1.4.4"
  },

  • npm installでライブラリをインストール

テストコードを書いてからスクリプトを書く

Helper = require('hubot-test-helper')
expect = require('chai').expect
helper = new Helper('./../scripts/kanban.coffee')

describe 'kanban', ->
  room = null

  beforeEach ->
    room = helper.createRoom()

  afterEach ->
    room.destroy()

  context 'user asks Hubot to add a task to kanban', ->
    beforeEach ->
      room.user.say 'yuki', 'kanban add task1'

    it 'should add a task to brain', ->
      expect(room.robot.brain.data.kanban).to.eql ['task1']

  • 実際のスクリプト
# kanban bot script

module.exports = (robot) ->
  robot.brain.data.kanban = []
  robot.hear /kanban add (.*)$/i, (res) ->
    robot.brain.data.kanban.push(res.match[1])
    res.reply 'Added kanban to' + res.match[1]

  • package.jsonでタスクを追加
  "scripts": {
    "test": "mocha --compilers coffee:coffee-script/register tests"
  },
  • test実行
$ npm test

> kanban-bot@0.0.0 test /workspace/nodeProject/kanban-bot
> mocha --compilers coffee:coffee-script/register tests



  kanban
    user asks Hubot to add a task to kanban
[Fri May 27 2016 20:55:05 GMT+0900 (JST)] INFO /workspace/nodeProject/kanban-bot/scripts/kanban.coffee is using deprecated documentation syntax
      ✓ should add a task to brain


  1 passing (287ms)

coモジュールを使う

  • 複数メッセージをやりとりしたときのテストで、beforeEachに数行設定しておく場合はcoモジュールを使う
  • 使わないと書き込みとbotの応答が非同期処理なのでroom.messageに交互に入ってくれなくてテストしにくい
  • devDependenciesにcoを追加
  "devDependencies": {
    "mocha": "^2.5.3",
    "chai": "^3.5.0",
    "coffee-script": "^1.10.0",
    "hubot-test-helper": "^1.4.4",
    "co": "^4.6.0"
  },
  • テストコード側でモジュールを読み込む
co = require('co')
  • beforeEachでcoモジュールを使う
    beforeEach ->
      room.robot.brain.data.kanban = ['task1']
      co =>
        yield room.user.say 'yuki', 'kanban add task2'
        yield room.user.say 'yuki', 'kanban list'

  • room.messagesが書き込みとbotの応答が交互に入るようになる
    it 'should return task list', ->
      expect(room.robot.brain.data.kanban).to.eql ['task1', 'task2']
      expect(room.messages).to.eql [
        ['yuki', 'kanban add task2']
        ['hubot', '@yuki Added kanban to task2']
        ['yuki', 'kanban list']
        ['hubot', '@yuki \n1. task1\n2. task2\n']
      ]

参考資料

9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9