Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nothing stored in brain #31

Open
yangeorget opened this issue May 16, 2016 · 2 comments
Open

nothing stored in brain #31

yangeorget opened this issue May 16, 2016 · 2 comments

Comments

@yangeorget
Copy link

yangeorget commented May 16, 2016

Hello,

I'd like to test the content of the brain but nothing seems to be stored in it when I run my tests.

packages.json:
{
"name": "...",
"version": "0.1.0",
"private": true,
"author": "...,
"description": "...",
"scripts": {
"test": "mocha --compilers coffee:coffee-script/register tests"
},
"dependencies": {
"hubot": "latest",
"hubot-brain-inspect": "latest",
"hubot-redis-brain": "latest",
"log4js": "latest",
"natural": "latest"
},
"devDependencies": {
"bluebird": "latest",
"chai": "latest",
"coffee-script": "^1.9.3",
"coffeelint": "latest",
"hubot-test-helper": "latest",
"mocha": "latest"
},
"engines": {
"node": "6.1.0"
}
}

test.coffee:
Helper = require('hubot-test-helper')
helper = new Helper('./../scripts')

Promise = require('bluebird')
co = require('co')
expect = require('chai').expect

describe 'hello-world', ->

beforeEach ->
@room = helper.createRoom()

afterEach ->
@room.destroy()

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

yield @room.user.say('alice', '@...bot trip')

it 'should reply to user', ->
  expect(@room.messages).to.eql [
    ['alice', '@...bot hi']
    ['hubot', '@alice Hello!']
    ['hubot', '@alice Using this bot, you can search for a trip or publish a trip.']
    ['hubot', '@alice If lost: type Help or Stop.']
  ]

STDOUT:
...
[2016-05-16 22:05:15.249] [DEBUG] Message - log_user: brain {"users":{},"_private":{}}
...

@IAmPatrickB
Copy link

IAmPatrickB commented Sep 2, 2016

It's important to consider the test script as a new version of your Hubot, everytime it's run. It doesn't use your existing Hubot or existing Brain, it's all fake and local to the test script only. So as part of your 'beforeEach' you also have to tell it what should be in the brain when you run it, if something is required.

i.e, with my PING command I require you to be an Admin. Thus, I need to load my auth.coffee, as it's part of the ping.coffee script. I also need to have an environmental variable for the auth.coffee file to know who is the Admin, and it also needs to check the brain for the user who is talking, get their ID, and check it against the admin list:

Helper = require('hubot-test-helper')

expect = require('chai').expect
helper = new Helper('../scripts/ping.coffee')

describe 'PING test', ->
  beforeEach ->
    @room = helper.createRoom()

    @room.robot.brain.userForName "Patrick",
      name: "Patrick"
      id: 1

    @room.robot.brain.userForId "Alice",
      name: "Alice"
      id: 2


    @room.robot.brain.userForId "Mike",
      name: "Mike"
      id: 3

    helper.constructor ('../scripts/auth.coffee')

  afterEach ->
    @room.destroy()

  it 'Set PING role for user', ->
    @room.user.say('Patrick', '@hubot I have ping role').then =>
      expect(@room.messages).to.eql [
        ['Patrick','@hubot I have ping role']
        ['hubot','OK, Patrick has the PING role.']]

  it 'ping user requests ping', ->
    @room.user.say('Patrick', '@hubot ping').then =>
      expect(@room.messages).to.eql [
        ['Patrick','@hubot ping']
        ['hubot','PONG']]

@yangeorget
Copy link
Author

Hi,

Thank you!

Cheers,
Yan

Yan Georget

On Fri, Sep 2, 2016 at 5:34 PM, Patrick B [email protected] wrote:

It's important to consider the test script as a new version of your Hubot,
everytime it's run. It doesn't use your existing Hubot or existing Brain,
it's all fake and local to the test script only. So as part of your
'beforeEach' you also have to tell it what should be in the brain when you
run it, if something is required.

i.e, with my PING command I require you to be an Admin. Thus, I need to
load my auth.coffee, as it's part of the ping.coffee script. I also need to
have an environmental variable for the auth.coffee file to know who is the
Admin, and it also needs to check the brain for the user who is talking,
get their ID, and check it against the admin list:

`Helper = require('hubot-test-helper')

expect = require('chai').expect
helper = new Helper('../scripts/ping.coffee')

describe 'PING test', ->
beforeEach ->
@room https://github.com/room = helper.createRoom()

@room.robot.brain.userForName "Patrick",
name: "Patrick"
id: 1

@room.robot.brain.userForId "Alice",
name: "Alice"
id: 2

@room.robot.brain.userForId "Mike",
name: "Mike"
id: 3

helper.constructor ('../scripts/auth.coffee')

afterEach ->
@room.destroy()

it 'Set PING role for user', ->
@room.user.say('Patrick', '@hubot https://github.com/hubot I have ping
role').then =>
expect(@room.messages).to.eql [
['Patrick','@hubot https://github.com/hubot I have ping role']
['hubot','OK, Patrick has the PING role.']]

it 'ping user requests ping', ->
@room.user.say('Patrick', '@hubot https://github.com/hubot ping').then
=>
expect(@room.messages).to.eql [
['Patrick','@hubot https://github.com/hubot ping']
['hubot','PONG']]`


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#31 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/ABREo0mBr48QwUws0QaUx6UDDqpoLwhmks5qmEIEgaJpZM4IfqG0
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants