Last active
August 29, 2015 13:56
-
-
Save keerts/8848172 to your computer and use it in GitHub Desktop.
cucumber.js, a more functional example (world.js)
This file contains 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 req = require('superagent'); | |
var Q = require('q'); | |
var app, db, config | |
var World = function World(callback) { | |
// app = ... insert code here to start up your express / whatever server and cleanse your database | |
var self = this | |
this.get = function(path) { | |
var deferred = Q.defer() | |
req.get("http://localhost:4000/api" + path) | |
.set({ Accept : 'application/json' }) | |
.end(function(err, res) { | |
if (err) { | |
deferred.reject(new Error(err)) | |
} else { | |
deferred.resolve(res.body) | |
} | |
}) | |
return deferred.promise | |
} | |
this.get = function(path) { | |
var deferred = Q.defer(); | |
req.get("http://localhost:4000/api" + path) | |
.set({ Accept : 'application/json' }) | |
.end(function(err, res) { | |
if (err) { | |
deferred.reject(new Error(err)); | |
} else { | |
deferred.resolve(res.body); | |
} | |
}) | |
return deferred.promise; | |
} | |
this.post = function(path, json) { | |
var deferred = Q.defer(); | |
req.post("http://localhost:4000/api" + path) | |
.send(json) | |
.end(function(err, res) { | |
if (err) { | |
deferred.reject(new Error(err)); | |
} else if (res.error) { | |
deferred.reject(new Error(res.body[0].stack)); | |
} { | |
deferred.resolve(res.body); | |
} | |
}) | |
return deferred.promise; | |
} | |
}; | |
exports.World = World; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment