[英]Redis mock in nodejs cannot stub non-existent property createClient
[英]How does one mock the createClient method of the node redis module
我試圖通過在創建新客戶端時使用redis-mock模塊代替redis模塊來避免對正在運行的redis服務器的依賴。 到目前為止,我發現不可能模擬相關方法: createClient
我閱讀了有關存根的sinon文檔,並通過了一個示例 (在Googling上找到后找到),並基於這些示例進行了設置:
一個示例應用
// 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});
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.