javascript - 如何模拟节点redis模块的createClient方法
问题描述
在创建新客户端时,我试图通过使用redis-mock模块代替redis模块来避免对正在运行的redis-server的依赖。到目前为止,我发现无法模拟相关方法:createClient
我已经浏览了关于存根的 sinon 文档和一个运行示例(在一些谷歌搜索后找到),并基于这些我设置了:
一个示例应用
// src/app.js
'use strict';
// import modules
const express = require('express')
, bluebird = require('bluebird')
, redis = require('redis')
;
// promisify redis
bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);
// define constants
const app = express()
, client = redis.createClient()
, port = 3000
;
// set some values
client
.setAsync('12345', JSON.stringify({vacancyId:12345}))
.catch(err => console.log(`[ERROR]: error setting value - ${err}`));
// define routes
app.get('/api/vacancy/:vacancyId', (req, res) => {
client
.getAsync(req.params.vacancyId)
.then(val => res.send(val))
.catch(err => console.log(`[ERROR]: error getting value - ${err}`))
});
// listen on port
app.listen(port);
// export the app
module.exports = app;
和相应的测试
// test/app.js
'use strict';
// import modules
const chai = require('chai')
, chaiAsPromised = require('chai-as-promised')
, chaiHttp = require('chai-http')
, redis = require('redis')
, redisMock = require('redis-mock')
, sinon = require('sinon')
, app = require('../src/app.js')
;
// configure chai
chai.use(chaiAsPromised);
chai.use(chaiHttp);
// define constants
const expect = chai.expect
, response = JSON.stringify({vacancyId:12345})
;
// now test
describe.only('App', function() {
before(function() {
sinon
.stub(redis.RedisClient.prototype, 'createClient')
.callsFake(function() {
console.log('[TEST]: i never get here :(');
return redisMock.createClient();
});
});
describe('/api/vacancy/:vacancyId', function() {
it('should return the expected response', function() {
return expect(chai.request(app).get('/api/vacancy/12345'))
.to.eventually
.have.include({status:200})
.and
.nested.include({text:response});
});
});
});
我希望测试能够通过(当我删除存根并指向正在运行的 redis 服务器时它会通过):
> scratch-node@1.0.0 test /Users/nonyiah/.src/scratch-node
> mocha --exit
App
/api/vacancy/:vacancyId
✓ should return the expected response
1 passing (41ms)
但相反,我收到以下错误:
> scratch-node@1.0.0 test /Users/nonyiah/.src/scratch-node
> mocha --exit
App
1) "before all" hook in "App"
0 passing (10ms)
1 failing
1) App
"before all" hook in "App":
TypeError: Cannot stub non-existent own property createClient
at Sandbox.stub (node_modules/sinon/lib/sinon/sandbox.js:308:19)
at Context.<anonymous> (test/app.js:26:8)
npm ERR! Test failed. See above for more details.
实现这种模拟的正确方法是什么?
解决方案
原来我是在加载应用程序后创建存根的。我需要移动应用程序的实例化:
// , app = require('../src/app.js')
在创建存根之后:
describe('/api/vacancy/:vacancyId', function() {
it('should return the expected response', function() {
let app = require('../src/app.js');
return expect(chai.request(app).get('/api/vacancy/12345'))
.to.eventually
.have.include({status:200})
.and
.nested.include({text:response});
});
});
推荐阅读
- cakephp - 如何将普通查询转换为 cakephp 标准
- google-apps-script - 从另一个单元格中减去一个单元格中的值的脚本
- list - 如何在列表中找到比前一个值增加不到 10% 的值
- qt - 连接自定义滚动条和 Flickable
- php - 为什么这个 PDO 准备好的语句不更新记录?
- django - 导航到不同的页面而不是 Ajax 调用
- asp.net-core - Hangfire - DisableConcurrentExecution - 如果在方法参数中传递相同的值,则防止并发执行
- plantuml - 如何用plantUML显示堆叠的卡片结构
- java - UnsupportedClassVersionError:org/sonar/api/utils/SonarException:不支持的major.minor版本52.0
- java - Rest Assured 的 Kotlin 扩展在 Then 块执行期间产生 java.lang.IllegalAccessError