Tag: st st

如何从外部范围调用一个方法到被testing的函数?

我有一个使用node_redis库创build的Redis客户端( https://github.com/NodeRedis/node_redis ): var client = require('redis').createClient(6379, 'localhost'); 我有一个方法,我想testing其目的是为Redis设置和发布一个值,所以我想testing,以确保set和publish方法调用或不按照我的期望调用。 棘手的是我希望这个testing不需要启动一个Redis服务器的实例,所以我不能创build客户端,因为如果它不能检测到Redis,它会抛出错误。 因此,我需要存根createClient()方法。 示例方法: // require('redis').createClient(port, ip) is called once and the 'client' object is used globally in my module. module.exports.updateRedis = function (key, oldVal, newVal) { if (oldVal != newVal) { client.set(key, newVal); client.publish(key + "/notify", newVal); } }; 我已经尝试了几种testing设置和发布的方法是否使用期望的键和值来调用,但是不成功。 如果我试图窥探这些方法,我可以通过运行debugging器来告诉我的方法正在调用,但是对于我来说,调用一次被标记为true。 如果我存根createClient方法返回假客户端,如: { set: function () […]