Created
April 13, 2013 21:55
-
-
Save jkresner/5380230 to your computer and use it in GitHub Desktop.
node.js testing an express mongoose REST api with supertest
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
| module.exports = [ | |
| { "name": "Brunch.io", "shortName": "brunch", "soId": "brunch", "_id": "514825fa2a26ea020000000b", "__v": 0 }, | |
| { "name": "C#", "shortName": "c#", "soId": "c#", "_id": "514825fa2a26ea020000000e", "__v": 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
| require './../../test-setup' | |
| require './../../test-http-setup' | |
| data = | |
| skills: require './data/skills-data' | |
| api_skills = require './../../lib/api/skills' | |
| app.get '/api/skills', api_skills.list | |
| app.get '/api/skills/:id', api_skills.detail | |
| app.post '/api/skills', api_skills.post | |
| testNum = 0 | |
| describe "REST api with mongoose", -> | |
| before (done) -> | |
| mongoose.connect "mongodb://localhost/airpair_test", done | |
| beforeEach (done) -> | |
| testNum = testNum + 1 | |
| @skillNum = data.skills[testNum] | |
| @skill = request(app) | |
| .post('/api/skills') | |
| .send( @skillNum ) | |
| .expect(200, done) | |
| it "should get first skill", (done) -> | |
| request(app) | |
| .get('/api/skills') | |
| .expect( [ @skillNum ] ) | |
| .end done | |
| after (done) -> | |
| mongoose.connection.db.executeDbCommand { dropDatabase:1 }, (err, result) -> | |
| mongoose.connection.close 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
| global.mongoose = require 'mongoose' | |
| global.request = require 'supertest' | |
| global.express = require 'express' | |
| global.app = express() | |
| app.configure -> | |
| app.use express.static(__dirname + '/public') | |
| app.use express.bodyParser() | |
| app.use express.cookieParser() |
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
| global.sinon = require 'sinon' | |
| chai = require 'chai' | |
| chai.use require 'sinon-chai' | |
| global.sinon = sinon | |
| global.expect = chai.expect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment