为什么redis命令不会在我的mocha测试中用于咖啡脚本文件?

时间:2012-10-19 15:50:45

标签: node.js coffeescript redis mocha

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()

但是从测试开始:

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'被打印出来。

我跑的时候:

mocha - 编译咖啡:咖啡脚本

redis似乎停止了工作。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

我放弃了摩卡,并通过这篇文章转移到茉莉花:

http://braddickason.com/jasmine-and-nodejs-testing/

答案 1 :(得分: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将在'ready'事件之前调用的命令添加到队列,然后在建立连接时处理它们。在redis“准备好”之前,mocha可能正在退出。

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