diff --git a/asjon-testing.coffee b/asjon-testing.coffee index e985a1c..ebd05b4 100644 --- a/asjon-testing.coffee +++ b/asjon-testing.coffee @@ -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 diff --git a/test/asjon-mock.coffee b/test/asjon-mock.coffee new file mode 100644 index 0000000..c6d2653 --- /dev/null +++ b/test/asjon-mock.coffee @@ -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() +