2012-10-19 42 views
0
class ThingWithRedis 
    constructor: (@config) -> 
    @redis = require('redis').createClient() 

    push: (key, object) -> 
    @redis.set(key, object) 
    fetch: (key, amount) -> 
    @redis.get key, (err, replies) -> 
     console.log "|#{replies}|" 

module.exports = ThingWithRedis 

#if you uncomment these lines and run this file, redis works 

#twr = new ThingWithRedis('some config value') 
#twr.push('key1', 'hello2') 
#twr.fetch('key1', 1) 
#twr.redis.quit() 

但是从测试:为什么redis命令不能在我的咖啡脚本文件的mocha测试中工作?

ThingWithRedis = require '../thing_with_redis' 

assert = require('assert') 

describe 'ThingWithRedis', -> 
    it 'should return the state pushed on', -> 

    twr = new ThingWithRedis('config') 
    twr.push('key1', 'hello1') 
    twr.fetch('key1', 1) 

    assert.equal(1, 1) 

你永远看不到 'hello1' 进行打印。

但是,当我直接用底线对咖啡thing_with_redis.coffee运行未注释时,您确实看到了'hello2'打印。

这是当我运行:

摩卡咖啡--compilers:咖啡脚本

Redis的似乎只是停止工作。有任何想法吗?

回答

0

这可能是Redis的连接尚未建立。在运行测试之前,请尝试等待“准备好”事件。

describe 'ThingWithRedis', -> 
    it 'should return the state pushed on', -> 

    twr = new ThingWithRedis('config') 
    twr.redis.on 'ready', -> 
     twr.push('key1', 'hello1') 
     twr.fetch('key1', 1) 

重要的是要注意的是node_redis增加了“准备”事件队列之前调用命令,然后在建立连接时对其进行处理。在Redis“准备好”之前,摩卡可能正在退出。

https://github.com/mranney/node_redis#ready

相关问题