【问题标题】:why won't redis commands work from within my mocha test for coffee script files?为什么 redis 命令不能在我的 mocha 咖啡脚本文件测试中工作?
【发布时间】:2012-10-10 06:51:16
【问题描述】:
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 --compilers coffee:coffee-script

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

【问题讨论】:

    标签: node.js coffeescript redis mocha.js


    【解决方案1】:

    可能是 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' 事件之前调用的命令添加到队列中,然后在建立连接时处理它们。 mocha 可能在 redis '准备好'之前就退出了。

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

    【讨论】:

      猜你喜欢
      • 2018-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-08
      相关资源
      最近更新 更多