Created
November 1, 2012 20:16
-
-
Save dangerbell/3996194 to your computer and use it in GitHub Desktop.
Example code for Node Testing with Mocha, SuperTest, and Nock
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
express = require 'express' | |
app = express(); | |
# Configure | |
require('./config')(app) | |
# Routes | |
require('./routes')(app) | |
module.exports = app |
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
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 |
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
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 |
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
http = require 'http' | |
app = require './app' | |
http.createServer(app).listen app.get('port'), -> | |
console.log "Express server listening on port " + app.get('port') |
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
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() |
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
describe "Calculator", -> | |
describe "addition", -> | |
it "should add two numbers", -> | |
add(2, 2).should.equal 4 |
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
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() |
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
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