Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example code for Node Testing with Mocha, SuperTest, and Nock
express = require 'express'
app = express();
# Configure
require('./config')(app)
# Routes
require('./routes')(app)
module.exports = app
express = require 'express'
config = (app) ->
# Configuration
app.set 'port', process.env.PORT || 3000
app.use express.favicon()
app.use express.methodOverride() # Allows the use of HTTP 'DELETE' AND 'PUT' methods.
app.use express.logger()
app.use app.router
app.use express.errorHandler()
module.exports = config
Example = require './controllers/example'
router = (app) ->
# Examples
app.get '/example/:id', Example.show
app.delete '/example/:id', Example.del
app.get '/example', Example.index
app.post '/example', Example.create
module.exports = router
http = require 'http'
app = require './app'
http.createServer(app).listen app.get('port'), ->
console.log "Express server listening on port " + app.get('port')
describe "Google", ->
describe "stuff", ->
it "should return success", (done) ->
options =
host: "www.google.com"
method: "GET"
google = http.request options, (res) ->
res.should.have.status 200
done()
google.end()
describe "Calculator", ->
describe "addition", ->
it "should add two numbers", ->
add(2, 2).should.equal 4
request = require 'supertest'
scope = require 'nock'
app = require process.cwd() + '/app.coffee'
backend = require process.cwd() + 'lib/backend.coffee'
URL =
describe 'Collections', ->
describe 'show', ->
it "should return a 404 if the object doesn't exist", (done) ->
scope = nock(backend.url)
.filteringPath(/^\/backend\.php.*$/, "/backend.php") # ignore any GET params
.get("/backend.php")
.reply(404)
request(app)
.get("/connectors/#{connectorId}/collections/#{collectionId}")
.set( 'Authorization', "MoverApi app_id=embiggen app_secret=cromulent" )
.end (err, res) ->
res.should.have.status(404)
scope.done()
done()
request = require 'supertest'
app = require process.cwd() + '/app.coffee'
describe 'Connectors', ->
describe 'create', ->
it "should return a 400 error if there is no type specified", (done) ->
request(app)
.post("/connectors")
.set( 'Authorization', "MoverApi app_id=embiggen app_secret=cromulent" )
.send( {} )
.expect(400, {
status: "Missing Parameter",
msg: "Must specify a type to create a connector. See documentation at http://mover.io/docs" },
done
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment