diff --git a/src/utils/redis-subpub.util.ts b/src/utils/redis-subpub.util.ts index b1b2255d..296500de 100644 --- a/src/utils/redis-subpub.util.ts +++ b/src/utils/redis-subpub.util.ts @@ -1,9 +1,18 @@ import IORedis from 'ioredis' import { REDIS } from '~/app.config' +import { isTest } from './tool.util' class RedisSubPub { public pubClient: IORedis.Redis public subClient: IORedis.Redis constructor(private channelPrefix: string = 'mx-channel#') { + if (!isTest) { + this.init() + } else { + !process.env.CI && console.error('[RedisSubPub] 在测试环境不可用!') + } + } + + public init() { const pubClient = new IORedis({ host: REDIS.host, port: REDIS.port }) const subClient = pubClient.duplicate() this.pubClient = pubClient diff --git a/test-setup.js b/test-setup.js index 71386c78..b958e498 100644 --- a/test-setup.js +++ b/test-setup.js @@ -4,3 +4,5 @@ const globals = { $, chalk, cd, consola: console } for (const key in globals) { global[key] = globals[key] } + +process.env.TEST = true diff --git a/test/src/modules/user/user.controller.spec.ts b/test/src/modules/user/user.controller.spec.ts index 2a56b673..e17aedec 100644 --- a/test/src/modules/user/user.controller.spec.ts +++ b/test/src/modules/user/user.controller.spec.ts @@ -3,6 +3,7 @@ import { registerGlobal } from 'test/register-global' import { AuthService } from '~/modules/auth/auth.service' import { UserController } from '~/modules/user/user.controller' import { UserService } from '~/modules/user/user.service' +import { CacheService } from '~/processors/cache/cache.service' registerGlobal() describe('test UserModule controller', () => { let userController: UserController @@ -10,7 +11,11 @@ describe('test UserModule controller', () => { beforeEach(async () => { const module = await Test.createTestingModule({ controllers: [UserController], - providers: [UserService, AuthService], + providers: [ + UserService, + AuthService, + { provide: CacheService, useValue: {} }, + ], }) .overrideProvider(UserService) .useValue({