2012-10-19 40 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

相關問題