我试图通过创建一个对象来模拟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 = {};
},
setData(key, 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, //
});
});