Created
October 27, 2012 21:03
-
-
Save mrchrisadams/3966250 to your computer and use it in GitHub Desktop.
I'm really struggling with running a simple test asyncronously with should.js, and node-redis.
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
setting up… | |
done setting up… | |
inside findByDevice: | |
✓ User #findByDevice should do something useful: 1ms | |
1) User #findByDevice should do something useful | |
inside device fetch | |
true | |
✖ 1 of 1 test failed: | |
1) User #findByDevice should do something useful: | |
TypeError: Cannot read property 'should' of undefined | |
at /Users/chrisadams/Code/herenow-2012/test/herenow/users.js:29:12 | |
at User.findByDevice (/Users/chrisadams/Code/herenow-2012/herenow/db/user.js:67:10) | |
at Context.<anonymous> (/Users/chrisadams/Code/herenow-2012/test/herenow/users.js:27:9) | |
at Test.Runnable.run (/Users/chrisadams/Code/herenow-2012/node_modules/mocha/lib/runnable.js:187:15) | |
at Runner.runTest (/Users/chrisadams/Code/herenow-2012/node_modules/mocha/lib/runner.js:307:10) | |
at Runner.runTests.next (/Users/chrisadams/Code/herenow-2012/node_modules/mocha/lib/runner.js:353:12) | |
at next (/Users/chrisadams/Code/herenow-2012/node_modules/mocha/lib/runner.js:235:14) | |
at Runner.hooks (/Users/chrisadams/Code/herenow-2012/node_modules/mocha/lib/runner.js:244:7) | |
at next (/Users/chrisadams/Code/herenow-2012/node_modules/mocha/lib/runner.js:192:23) | |
at Runner.hook (/Users/chrisadams/Code/herenow-2012/node_modules/mocha/lib/runner.js:207:7) | |
at done (/Users/chrisadams/Code/herenow-2012/node_modules/mocha/lib/runnable.js:178:5) | |
at Runnable.run.duration (/Users/chrisadams/Code/herenow-2012/node_modules/mocha/lib/runnable.js:190:9) | |
at try_callback (/Users/chrisadams/Code/herenow-2012/node_modules/redis/index.js:484:9) | |
at RedisClient.return_reply (/Users/chrisadams/Code/herenow-2012/node_modules/redis/index.js:555:13) | |
at RedisReplyParser.RedisClient.init_parser (/Users/chrisadams/Code/herenow-2012/node_modules/redis/index.js:256:14) | |
at RedisReplyParser.EventEmitter.emit (events.js:88:17) | |
at RedisReplyParser.send_reply (/Users/chrisadams/Code/herenow-2012/node_modules/redis/lib/parser/javascript.js:281:18) | |
at RedisReplyParser.execute (/Users/chrisadams/Code/herenow-2012/node_modules/redis/lib/parser/javascript.js:135:22) | |
at RedisClient.on_data (/Users/chrisadams/Code/herenow-2012/node_modules/redis/index.js:440:27) | |
at Socket.<anonymous> (/Users/chrisadams/Code/herenow-2012/node_modules/redis/index.js:70:14) | |
at Socket.EventEmitter.emit (events.js:88:17) | |
at TCP.onread (net.js:396:14) |
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
setting up… | |
done setting up… | |
inside findByDevice: | |
inside device fetch | |
true | |
User: user | |
1) User #findByDevice should do something useful | |
✖ 1 of 1 test failed: | |
1) User #findByDevice should do something useful: | |
Error: timeout of 2000ms exceeded | |
at Object.<anonymous> (/Users/chrisadams/Code/herenow-2012/node_modules/mocha/lib/runnable.js:158:14) | |
at Timer.list.ontimeout (timers.js:101:19) |
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
require('../setup') | |
require('should') | |
var db = require('../../lib/db') | |
var User = require('../../herenow/db/user') | |
describe('User', function() { | |
describe('#findByDevice', function() { | |
beforeEach(function(done) { | |
console.log('setting up…') | |
db.hmset("mrchrisadams", { | |
name: "Chris Adams", | |
username: "mrchrisadams", | |
devices: ["00:1e:c2:a4:d3:5e"], | |
email_address: "wave@chrisadams.me.uk" | |
}, done); | |
console.log('done setting up…') | |
}) | |
it('should do something useful', function(done) { | |
var u = new User(); | |
u.findByDevice('00:1e:c2:a4:d3:5e', function(err, chris) { | |
done() | |
chris.should.have.property('username') | |
console.log("logging a spec from inside u.findByDevice") | |
}); | |
}) // close it should do something useful spec | |
}) // close describe #findByDevice function | |
}) |
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
// Example User attributes | |
// { | |
// name: "Chris Adams" | |
// username: "mrchrisadams" | |
// devices: ["00:1e:c2:a4:d3:5e"] | |
// email_address: wave@chrisadams.me.uk | |
// } | |
var sys = require('util'), | |
fs = require('fs'), | |
events = require('events'), | |
db = require('../../lib/db'); // db is a module wrapper for redis.createClient(); | |
// basic boiler plate | |
function User() { | |
if(false === (this instanceof User)) { | |
return new User(); | |
} | |
events.EventEmitter.call(this); | |
} | |
sys.inherits(User, events.EventEmitter); | |
User.prototype.toString = function() { | |
return "User" | |
} | |
// Tries to find the corresponding user for the device mac passed in, after checking we have this mac | |
// registered first | |
// interface_name - the String giving an interface name, like `en1` or `en0` | |
// device_mac - the String representing the mac address we're using as a key, like `'00:1e:c2:a4:d3:5e'` | |
User.prototype.findByDevice = function(device_mac, callback) { | |
// look in redis for mac address, using hget with the device's mac as the key | |
// then return the user keying on the user's username | |
var user = db.hgetall(device_mac, function (err, device) { | |
// console.log("User: device") | |
// console.log(device) | |
if (device.hasOwnProperty('mac')) { | |
db.hgetall(device.owner, function (err, user) { | |
console.log("User: user") | |
// console.log(user) | |
return user | |
}) | |
} | |
// | |
console.log("inside device fetch") | |
console.log(user) | |
return user | |
}); | |
console.log("inside findByDevice:") | |
return user | |
// calling callback(user) returns from the function, but doesn't return the user object | |
// I want to return. how do I return it? | |
} | |
module.exports = User; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment