aggiunga abilità di fingere richieste http nei test

This commit is contained in:
Enrico Fasoli 2015-04-27 21:34:03 +02:00
parent 7e6c60dd61
commit 580279cd0a
2 changed files with 34 additions and 6 deletions

View File

@ -7,20 +7,21 @@ TextMessage = require("hubot/src/message").TextMessage
before = (done) ->
robot = new Robot null, "mock-adapter", no, 'asjon'
robot.adapter.on 'connected', ->
#robot.loadFile (path.resolve path.join 'scripts/memoria.coffee'), 'memoria.coffee'
# Initialize mocked environment
user = robot.brain.userForId "1", { name: 'mocha', room: '#mocha' }
adapter = robot.adapter
send = (s) -> robot.adapter.receive new TextMessage user, s
receive = (f) -> robot.adapter.on 'send', f
after = -> robot.shutdown()
clear = -> robot.adapter.removeAllListeners(); robot.httpListener = []
# Mock http calls
###
clear = -> robot.adapter.removeAllListeners(); robot.httpListener = ->
# Intercept hubot's http calls
robot.onHttp = (f) -> robot.httpListener = f
robot.http = (addr) ->
get: ->
robot.onHttp addr,
###
(cb) -> robot.httpListener 'GET', addr, cb
post: ->
(cb) -> robot.httpListener 'POST', addr, cb
# Done
done
robot: robot, user: user, send: send, receive: receive
TextMessage: TextMessage, Robot: Robot

27
test/asjon-mock.coffee Normal file
View File

@ -0,0 +1,27 @@
expect = require("chai").should()
Asjon = require '../asjon-testing.coffee'
asjon = undefined
describe 'hubot mock', ->
before (done) ->
# Inizializzo robot
Asjon (assa) ->
asjon = assa
after asjon.after
afterEach asjon.clear
require('../scripts/memoria.coffee')(asjon.robot)
done()
it 'should intercept hubot\'s HTTP calls', (done) ->
asjon.robot.onHttp (method, addr, cb) ->
addr.should.equal 'http://ddg.gg'
method.should.equal 'GET'
cb null, 200, 'mock body'
asjon.robot.http('http://ddg.gg')
.get() (err, res, body) ->
if err then throw err
res.should.equal 200
body.should.equal 'mock body'
done()