问题描述
我试图通过创建一个对象来模拟Redis服务器,该对象包含充当高速缓存的对象以及用于设置和获取数据的功能。我已将此模拟文件放置在与要在 mocks 目录下进行模拟的文件相同的目录中。但是没有调用模拟函数。
这是要模拟的文件
const redis = require('redis');
const { promisify } = require('util');
const RedisApi = {
connect() {
this.client = redis.createClient(6379);
this.client.get = promisify(this.client.get);
},setData(key,value) {
this.client.set(key,value);
},getData(key) {
return this.client.get(key);
},};
module.exports = RedisApi;
这是我制作的模拟文件
const RedisApi = {
connect() {
this.cache = {};
},value) {
this.cache[key] = value;
},getData(key) {
return this.cache(key);
},};
module.exports = RedisApi;
这是测试文件
beforeEach(() => {
jest.mock('../../src/database/helpers/redisSetup');
newUser = {
name: faker.name.findName(),email: faker.internet.email().toLowerCase(),password: 'password1',};
});
test('should return 201 and successfully register user if request data is ok',async () => {
const res = await request(app).post('/v1/student/signup').send(newUser).expect(httpStatus.CREATED);
expect(res.body.data.student).not.toHaveProperty('password');
expect(res.body.data.student).toEqual({
studentId: expect.anything(),name: newUser.name,email: newUser.email,id: expect.anything(),isVerified: false,//
});
});
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)