Last active
December 23, 2015 05:58
-
-
Save tommystanton/6590337 to your computer and use it in GitHub Desktop.
Learning TDD in Node.js using Mocha and should (and journey, to simulate a web application).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ npm install mocha should journey | |
| $ node_modules/.bin/mocha --ui tdd --reporter tap server.checkHello.test.js | |
| 1..1 | |
| ok 1 Response from /hello URI should return JSON containing "hello" | |
| # tests 1 | |
| # pass 1 | |
| # fail 0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var server = require('./server'), | |
| request = require('request'), | |
| should = require('should'), | |
| util = require('util'); | |
| var port = 9090, | |
| testUrl = util.format("http://localhost:%d", port), | |
| s = server.createServer(port); | |
| suite("Response from /hello URI", function () { | |
| var url = testUrl + "/hello"; | |
| test("should return JSON containing \"hello\"", function (done) { | |
| request(url, function(err, res, body) { | |
| res.should.have.status(200).and.be.json; | |
| res.body.should.match(/hello/); | |
| done(); | |
| }); | |
| }); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var http = require('http'), | |
| journey = require('journey'); | |
| var router = new(journey.Router); | |
| router.map(function () { | |
| this.get('/hello').bind(function (request, response, data) { | |
| response.send(200, {}, { 'hello': 'world' }); | |
| }); | |
| }); | |
| exports.createServer = function(port) { | |
| var s = http.createServer(function (request, response) { | |
| var body = ''; | |
| request.addListener('data', function (chunk) { body += chunk; }); | |
| request.addListener('end', function() { | |
| router.handle(request, body, function (result) { | |
| response.writeHead(result.status, result.headers); | |
| response.end(result.body); | |
| }); | |
| }); | |
| }); | |
| s.port = port; | |
| //s.url = 'http://localhost:' + port; | |
| s.listen(s.port); | |
| return s; | |
| }; |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I mentioned this Gist in a comment on Stack Overflow.