Skip to content

Instantly share code, notes, and snippets.

@keerts
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keerts/8848172 to your computer and use it in GitHub Desktop.
Save keerts/8848172 to your computer and use it in GitHub Desktop.
cucumber.js, a more functional example (world.js)
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